Changing the border color in polygon - java

Using Polygon class, I have created a triangle.
By using setStroke(Color.AQUA), I have changed the border color of that polygon. Triangle has 3 borders. How to set different color for every border?

There is no predefined method in Polygon for giving each border an individual color. You have to implement your own class or use a combination of 3 independent lines to make triangle.

Related

How to get the color beneath an opaque JPanel?

Is there is a way to get the color underneath an opaque JPanel?
I have two JPanels, a red one and a blue one (they are animated). I want to get the color of a surface according to the JPanel that passes over it.
In following images, the small borderd square is an opaque JPanel, and I want to retrieve the color of the JPanel beneath.
Not really sure what you are asking. But assuming you know the x/y location of the rectangle you could use:
The getComponentAt(...) method of the Container class. Once you get the parent panel you use the getBackground()` method to get the Color
Or, you can use the Robot class and the getPixelColor(...)` method.

Drawing a border around a shape with swing

Is there any way in java to draw a border around the last-drawn shape? e.g. if I have code that uses graphics.fillwhatever to draw a shape, is there are method I can call after that without knowing what type of fill shape I drew that surrounds said shape with a border?

Java AWT bounds

I have an assignment to draw a certain number of circles using java.awt.Graphics.
Drawing the circles is fairly simple, but I am only supposed to draw the circle if it appears within the visible area. I know I can call method getClipBounds() to determine the drawing area, but I'm having trouble finding a java implementation of a way to determine if a circle intersects a Rectangle.
Is that the right way to go about determining if the circle I want to draw will be completely visible or is there a simpler way?
Don't use the Graphics.fillOval(...) method to do the painting.
Instead you can use the Graphics2D.fill(Shape) method. You can create oval Shape objects using the Ellipse2D class.
but I'm having trouble finding a java implementation of a way to determine if a circle intersects a Rectangle.
The Shape object has a method that will allow you to get the rectangular bounds of the Shape. Then you can use the Rectangle.contains(...) method of the your Graphics area to determine if the Shape is fully contained within your panel.
Check out Playing With Shapes for more information and ideas.
use Ellipse2D.Float to instanciate an object for example:
Shape circle = new Ellipse2D.Float(100.0f, 100.0f, 100.0f, 100.0f);
basically the parameters,from left to right, are: Height, Width, X of the Top left and Y of the top left, and by keeping the X and Y both greater or equal to zero, your circle will always be visible.
the parameters of the Float(...) are documented for the Ellipse2D.Float in Java SE 7 in
http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Ellipse2D.Float.html

How to make a rotated panel in java

I want to know if there is some way to rotate a panel without changing its shape. I mean I am able to rotate the drawings inside the panel using rotate() given in graphics2D but the rectangular drawings become diamond shaped. Is there some soln for it?? I mean can I avoid the drawing from becoming diamond shape. The problem is more evident when u change the resolution of the screen.
Yes, rotate the image around its center, as shown in this example.

How to place rectangle in grid order?

So far, my program draws many rectangles and the locations of the rectangles are random (not nice). How can I place the rectangle in grid order so that it look appealing?
Use a grid manager like GridLayout

Categories