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?
Related
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.
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
In my game, I'm using different kinds of objects that implement the Paint interface including GradientPaint for the background. When I try to paint the background, it mostly leaves a grey border on the right and bottom sides of the screen; I don't want that border there. How can I get rid of it?
I notice that when I set my JFrame to be resizable instead of not, the background paints okay as want, but then people will be able to resize the window and distort the visuals needed to play the game.
What I am using to draw graphics is the outdated AWT, using both Graphics and Graphics2D. All the backgrounds I'm painting consist of either one, two, or four Rectangle2D.Float objects, with the appropriate sizes. For a full background, it would be 0, 0, gamePanel.getPreferredSize().width and gamePanel.getPreferredSize().height.
my java application contains a JPanel on which I draw certain shapes. Now I would like to label these shapes with some kind of tooltips.
Therefore I tried to create my own "Tooltips" by using the drawString, setBackground, setColor method.:
public void drawToolTip(Graphics2D graphics, String text, Point2D position) {
graphics.setBackground(Color.RED);
graphics.setColor(Color.GREEN);
graphics.drawString(text, (float) position.getX(), (float) position.getY());
}
Unfortunately the setBackground method does not seem to work. The text background remains transparent although I set it to red. setColor and drawString just work fine.
My questions are:
What could be the reason that the setBackground method does not work?
Is there a possibility to draw a boarder arround the text without drawRect?
If I want to use "drawRect" method as a substitude to draw the text background and border: How can I make it automatically fit to the written text? Or in other words how can I get the dimensions of a specific text?
Regards Marc
Graphics2D.drawString() does not draw a background by default. You will have to do this yourself.
You can use drawRect() to draw a line border or fillRec() to draw a solid rectangle.
Oracle has a great tutorial on measuring String widths. Essentially, you need to create a java.awt.Font then get its FontMetrics and use that to calculate the width and height of your string.
A simple implementation would involve drawing onto the Graphics object of a JLabel's icon. And then simply adding the tool tip text to the Swing component.
For more information, see How to Use Tool Tips.
You can not change background color the way you expect using graphics.setBackground(..) call. Setting background color in the Graphics2D only affects the clearRect or fillRect kind of calls and not the background color of the Component.
For drawing a rectangle at a location you wish, with specific back ground, you will have to relay on following steps:
Define a rectangle - r
grpahics.setPaint() for background and
graphics.fill(r) graphcis.setPaint() for border and
graphics.draw(r) to draw border
now, comes the difficult part of drawing text in to the rectangle which involves computation of height etc. based on FontMetrics of the font you would set for drawing the text.
I googled and found an example for you here
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.