set different colors to some different circles - java

i have two circles (want to have some more in the future) and want them to have different colors. At the moment, the color is set with:
public static Paint p = new Paint();
p.setColor(Color.GREEN);
The Problem is that every new Circle I create has this color too.
How can I set new colors for different new circles?
This is where I instantiate the circles (in the constructor):
c1 = new Circle (165, 350, 33);
c2 = new Circle (200, 200, 33);
p.setColor(Color.GREEN);
p1.setColor(Color.YELLOW);
And this is where I call the draw in my "onDraw()":
canvas.drawCircle(lerpX, lerpY, c1.getR(), p);
canvas.drawCircle(c2.getX(), c2.getY(), c2.getR(), p);
Thanks in advance

This is a simple way to do it. Almost procedural but it will work.
Define your color
p.setColor(Color.GREEN);
//draw shape
Define next color
p.setColor(Color.RED);
//draw next shape

Related

customize the stroke to have a border and a fill

How can I draw a stroke with a fill color and a (different color) border?
e.g. I want something like this:
I tried creating 2 paints - one with a Stroke style and one with a Fill style, but calling
strokePaint = new Paint();
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setColor(Color.parseColor("#A3A3A3"));
fillPaint = new Paint();
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setColor(Color.WHITE);
canvas.drawPath(totalPath, strokePaint);
canvas.drawPath(totalPath, fillPaint);
doesn't create the intended effect and looks quite bad.
Is it even possible?
Figured it out. The trick is to draw it twice, once as a background layer that is 1-2 pixels thicker, and then the foreground layer.
i.e. :
strokePaintBackground = new Paint(Paint.ANTI_ALIAS_FLAG);
strokePaintBackground.setStyle(Paint.Style.STROKE);
strokePaintBackground.setColor(Color.BLACK);
strokePaintBackground.setStrokeWidth(8);
strokePaintBackground.setPathEffect(new DashPathEffect(new float[]{30, 15}, 0));
strokePaintBackground.setStrokeCap(Paint.Cap.ROUND);
strokePaintBackground.setStrokeJoin(Paint.Join.ROUND);
strokePaintForground = new Paint(Paint.ANTI_ALIAS_FLAG);
strokePaintForground.setStyle(Paint.Style.STROKE);
strokePaintForground.setColor(Color.WHITE);
strokePaintForground.setStrokeWidth(6);
strokePaintForground.setPathEffect(new DashPathEffect(new float[]{30, 15}, 0));
strokePaintForground.setStrokeCap(Paint.Cap.ROUND);
strokePaintForground.setStrokeJoin(Paint.Join.ROUND);
canvas.drawPath(totalPath, strokePaintBackground);
canvas.drawPath(totalPath, strokePaintForground);

How to fade out the edges of a Java2D Area object?

I have a method that creates shadows based on where things are on the screen and the final product of that method is an Area, which I then draw onto the screen and it contains all shadows on screen in the same Area. This is because the drawn shadows are a low opacity so if it is not all one thing they will overlap and the opacity will make it look weird.
The issue that I want the shadows to look like they fade out, but I have absolutely no idea how, or if that would even be possible. Is there a way to soften the edges of the Area or make them gradient fade out? I tried to do graphics2D.setPaint(new GradientPaint[a gradient effect]) but it did nothing.
Thanks in advance
EDIT: Here is a screenshot of the program making a shadow around a 'building' rectangle. The green rectangle is to show the effect of the shadow. The end result I want is instead of the shadow abruptly ending, it should fade out.
i guess you have problems setting up the proper colors for the gradient:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillRect(30, 30, 50, 20);
Graphics2D g2d = (Graphics2D) g;
Polygon p = new Polygon(new int[]{10,20,50,50,40,10}, new int[]{10,10,40,50,50,20}, 6);
Area a = new Area(p);
Color b = new Color(0xFF000000, true); //using colors with transparency!!!
Color w = new Color(0x00FFFFFF, true); //using colors with transparency!!!
//try to use proper values for the x/y values - both, start and end
GradientPaint gradient = new GradientPaint(0, 0, b, 40, 40, w);
g2d.setPaint(gradient);
g2d.fill(a); // fill your area!
}
result is a gradient with alpha (Transparency) ...
Area drawn in red <==> Area drawn with Gradient
don't forget to set the alpha value 0xAARRGGBB to FF for 100% opaque (0xFF000000 is black 100%opaque) to 0 (0x00FFFFFF white, 100% transparent)

How to draw a circle on a bitmap without filling it?

I am ddrawing a circle on the bitmap and setting this bitmap to ImageView. the circle is drawn correctly but i dont want it to be a filled circle is there any way to make the filled area transparent?
i am using the following code
Bitmap bmp = RasterImageConverter.convertToBitmap(_loadedImage, ConvertToImageOptions.NONE.getValue());
Canvas c = new Canvas(bmp);
c = new Canvas(bmp);
myimgview.draw(c);
Paint p = new Paint();
p.setColor(Color.RED);
float x=(float) circleX;
float y=(float) circleY;
float Tx=(float) textX;
float Ty=(float)textY;
// c.drawLine(x, y, xend, yend, p);
c.drawCircle(300, 300, 200, p);
c.drawText(myText, Tx, Ty, p);
myimgview.setImageBitmap(bmp);
You need to inform the paint that you dont' use a fill style, but a stroke style.
The default is FILL
The Style specifies if the primitive being drawn is filled, stroked, or both (in the same color). The default is FILL.
So your code must be:
Paint p = new Paint();
p.setStyle(Paint.Style.STROKE);
Here are explained the differences between the different styles.
STROKE
Geometry and text drawn with this style will be stroked, respecting the stroke-related fields on the paint.
FILL
Geometry and text drawn with this style will be filled, ignoring all stroke-related settings in the paint.
FILL_AND_STROKE
Geometry and text drawn with this style will be both filled and stroked at the same time, respecting the stroke-related fields on the paint.
You need to change the Paint style to stroke:
Paint p = new Paint();
p.setStyle(Paint.Style.STROKE);
Use the setStyle(Paint.Style.STROKE) method on your paint p instance.
You are almost there... you need to set the style of the Paint object...
by invoking the method
p.setStyle(Paint.Style.STROKE);
The options are:
Example:
Bitmap bmp = RasterImageConverter.convertToBitmap(_loadedImage, ConvertToImageOptions.NONE.getValue());
Canvas c = new Canvas(bmp);
c = new Canvas(bmp);
myimgview.draw(c);
Paint p = new Paint();
p.setColor(Color.RED);
p.setStyle(Paint.Style.STROKE);

Fill a shape with a random color in java

In this program I am doing, I need to be able to fill shapes with random colors. I am confused on how to actually fill in the shape. I am able to generate a random color. I have looked around online and found some sources talk about implementing the Paint interface and use the method setPaint() on the shape you are wishing to draw and then invoke the fill method. I tried this but was unsuccessful. Maybe I just had it wrong. Here is what I had.
Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color (r, g, b);
This was in a constructor of a superclass where I actually have randomColor as an attribute of the class. So to access that element in the subclass, I provide a basic getter that just returns the Color.
In the subclass I have this:
Rectangle2D.Double rectangle = new Rectangle2D.Double(getX(), getY(), getWidth(), height);
rectangle.setPaint(getColor());
rectangle.fill();
g2.draw(rectangle);
The error I am getting is something about typecasting rectangle but any typecast I try it's not working. I'm not exactly sure how to fix this issue. Any ideas? Or is there an easier/better way to fill a shape with a random color? Thanks.
You should be calling the methods you are calling on the rectangle on the Graphics2D
Rectangle2D.Double rectangle = new Rectangle2D.Double(getX(), getY(), getWidth(), height);
g2.setPaint(getColor());
g2.fill(rectangle);
g2.draw(rectangle);
I need to be able to fill shapes with random colors
Then you should create a class (ColoredShape) that contains two properties:
Shape
Color
Then you can create an ArrayList to hold instances of this class.
ArrayList<ColoredShape> shapes = new ArrayList<ColoredShape>();
shapes.add( new ColoredShape(aShape, generateRandomColor());
shapes.add( new ColoredShape(anotherShape, generateRandomColor());
Then in the paintComponent() method of your panel you iterate through the ArrayList and paint all the shapes:
for (ColoredShape shape: shapes)
{
g2.setColor(shape.getColor());
g2.draw(shape.getShape());
}
For a working example of this approach check out the DrawOnComponent example found in Custom Painting Approaches.

Issue creating borders with draw() / drawPolygon() in Graphics2D

More adventures with Graphics2D!
This time I'm investigating the various paint / colour modes and ways to paint complex borders. The typical method is to fill the Shape or Polygon, and then draw the same Shape over the top (in a different colour). I'm familiar with setting Strokes and so forth to vary border thickness and other bits.
My latest attempt was to use RadialGradientPaint as a way of defining multiple bands of colour, but this doesn't appear to apply to the draw / drawPolygon call (or the extra colours aren't showing up.
The draw call is drawing a thickness 3 empty Polygon, but as a uniformly-blue strip. Below is the gradient / paintComponent code:
g2d.setStroke(new BasicStroke(h.getHexBorderWidth())); // this is set to 3
Point2D center = new Point2D.Float(50, 50);
float radius = 1;
float[] dist = {0.0f, 0.5f, 1.0f};
Color[] colors = {Color.RED, Color.WHITE, Color.BLUE};
// the float value is defined as 0.0 in the constructor
RadialGradientPaint p =
new RadialGradientPaint(center, radius, new Point2D.Float(), dist, colors,
CycleMethod.NO_CYCLE);
g2d.setPaint(p);
if(((BasicHexagon) h).isAnimating()
&& ((BasicHexagon) h).getCurrentTransform() != null) { // just general null checks
g2d.draw(((BasicHexagon) h).getCurrentTransform()); // this is a transformed Shape
} else {
g2d.drawPolygon(h.getBase()); // this is a Polygon
}
Any better ways I'm missing out on, or any way that this isn't being called correctly?

Categories