public class CustomBorderDrawable extends ShapeDrawable { private Paint fillpaint, strokepaint; private static final int WIDTH = 3; public CustomBorderDrawable(Shape s) { super(s); fillpaint = this.getPaint(); strokepaint = new Paint(fillpaint); strokepaint.setStyle(Paint.Style.STROKE); strokepaint.setStrokeWidth(WIDTH); strokepaint.setARGB(255, 0, 0, 0); } @Override protected void onDraw(Shape shape, Canvas canvas, Paint fillpaint) { shape.draw(canvas, fillpaint); shape.draw(canvas, strokepaint); } public void setFillColour(int c){ fillpaint.setColor(c); } }
public static void main(String[] args){ int x = getFromPropertyFile(); Shape shape = getShape(x); // Shape obtained from a factory method shape.draw(); //Runtime polymorphism }
public void draw(List<Shape> shape) { for(Shape s: shape) { s.draw(this); } }
public void draw() { for (Shape shape : shapes) { shape.draw(); } }
public void drawAll(List<Shape> shapes) { for (Shape s: shapes) { s.draw(this); } }
public void drawNext(Shape s) { s.draw(); //this will call draw() method of Square if you call drawNext(new Square()) and draw() method of Triangle in case drawNext(new Triangle()) and so on.. }
public <T extends Shape> void mergeThenDraw(List<T> shapes1, List<T> shapes2) { List<T> mergedList = new ArrayList<T>(); mergedList.addAll(shapes1); mergedList.addAll(shapes2); for (Shape s: mergedList) { s.draw(this); } }
Class clazz = Class.forName("fully.qualyfied.ClassName"); Shape instance = (Shape)clazz.newInstance(); instance.draw()
public void draw(Image im) { Graphics g=im.getGraphics(); for (int i=0; i < shapeArr.size(); i++) { Shape s = shapeArr.get(i); s.draw(g); } if (inProgress != null) inProgress.draw(g); }
public void someMethod(Class<? extends Shape> type, File shapeFile) { ThirdPartyClass foo = new ThirdPartyClass(); @SuppressWarnings("unchecked") Class<? extends Shape[]> arrayType = (Class<? extends Shape[]>) Array.newInstance(type, 0).getClass(); assert Shape[].class.isAssignableFrom(arrayType); Shape[] shapes = foo.someMajicMethod(arrayType, shapeFile); for (Shape shape: shapes) shape.draw(); }
void drawAll() { for (Shape s : heterogeneousCollection) { s.draw(); // common function present in Shape also s.doSomethingSpecific(); // specific to each implementation } }
@Override public void draw(Canvas canvas){ if(mMask != null){ canvas.save(); canvas.translate(-getBounds().left, -getBounds().top); mMask.draw(canvas, mPaint); canvas.restore(); } }
class Canvas{ public static void main(String[] args){ List<Shape> shapes = new ArrayList<>(); shapes.add(new Circle()); shapes.add(new Square()); // clean and neat code for(Shape shape : shapes){ shape.draw(); } } }
private void makeShapeBitmap() { int w = getMeasuredWidth(); int h = getMeasuredHeight(); if (w == 0 || h == 0) return; releaseBitmap(mShapeBitmap); mShapeBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(mShapeBitmap); Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); p.setColor(Color.BLACK); mShape.draw(c, p); }
List<Shape> lstShape = new ArrayList<Shape>(); lstShape.add(new Circle()); lstShape.add(new Triangle()); ... for (Shape s: lstShape) { s.draw(); }