I have a problem with Java As I understood not come Draw Geometric figures in Java, the code and the Following you can help me?
This is the code:
public class Gioco {
public static void main (String args [])
{
PaintRect();
}
public static void PaintRect() {
g.drawRect(100,100,300,300);
g.drawLine(100,100,100,100);
g.setBackground(Color.BLACK);
System.out.println("Trasut");
credits.setText("Kitebbiv");
credits.setBackground(null);
credits.setEditable(false);
credits.setFocusable(false);
credits.setBounds(0,0,100,100);
credits.setForeground(Color.BLACK);
panel.add(credits);
g.getPaint();
}
How can I create a JFrame
the triangle, square and rectangle? correct my code thanks
Before I even start writing my answer I need to encourage you to read carefully to: How to create a valid Minimal, Complete and Verifiable Example and a Short, Self Contained, Correct Example.
From your (now deleted) code, I see you haven't gone through the Swing Tutorial on Custom Painting yet or you didn't even pay attention to it, this line will cause you problems
The excessive use of
static
modifier will harm you,static
isn't a cross-method magic word to make your variables be accessible anywhere in your program, you should instead create an instance of your program and call the methods from there (they not being static), see Why are static variables considered evil?, and you should really go back and learn the essentials before adding more complexity to your learning with a GUI and even more with Swing custom painting.You're making use of
setBounds()
method, which suggests (and I can confirm in your deleted code) that you're using anull-layout
:You should really consider checking the layout managers
You're making use of a deprecated method
JFrame#show()
instead you should be usingJFrame#setVisible()
method.You're manually setting the size of your
JFrame
, you should instead use a layout manager and call the methodJFrame#pack()
which will calculate the preferred size for yourJFrame
or override your component'sgetPreferredSize()
.On your deleted code, you had a
MouseListener
attached to aJButton
, instead you need to use anActionListener
, see How to use Actions to learn this.You're not placing your program on the Event Dispatch Thread (EDT) which could make your program to freeze, because Swing is not Thread safe. You can correct this by writing your
main
method as follows:You should be more polite: "correct my code thanks" sounds like an order, I would have said that like could you help me? which sounds like a request / petition for someone to give you a hand, because they can, not because they must help you, all the above points correct your code.
After all the above has being said (and which you should read carefully) we can continue to the coding part:
In order to draw a rectangle we need to learn something about a rectangle:
drawRect(x, y, width, height)
draw(Shape)
of theGraphics2D
method whereShape
would be an instance ofRectangle2D
To draw a square we need to know that:
drawRect(x, y, width, height)
draw(Shape)
of theGraphics2D
method whereShape
would be an instance ofRectangle2D
You must be saying... "But the method you're using to draw the square is the same as the rectangle!", well... yep, we are, the difference lies in that we're going to pass a
width
andheight
equal size for the square and different size for the rectangle.To draw a triangle you need to know that:
drawTriangle
in Swing, but we havedrawPolygon(xPoints, yPoints, nPoints)
draw(Shape)
of theGraphics2D
method, which will draw a Polygon ofnPoints
(3 in this case), taking the coords from each array element ofxPoints
for theX
coords andyPoints
for theY
coords and whereShape
would be an instance ofPolygon
Now, putting all that together we should have all that code in an overridden method of our
JPanel
calledpaintComponent()
as shown in the tutorial (See point #1). It should look like this:But we also need to override another method
getPreferredSize()
on ourJPanel
, (see: Should I avoid the use of setPreferred|Maximum|MinimumSize in Swing? the general consensus says yes), otherwise ourJFrame
will be smaller than what we want, so it should look like this:Don't forget to call
@Override
in those methods!With only those methods we have completed our program to draw the shapes, but I know that if I don't post the whole code you'll end up writing the above methods in a place that won't work or cause you compilation errors, so the whole code (which in fact is a MCVE or SSCCE (see the very first paragraph in this answer to see what each means)) that produces the below output is:
I really hope you read every link I posted here, because it's worth it
And if you need to fill the shapes then call
fillRect
andfillPolygon
instead ofdrawRect
anddrawPolygon
:Edit
As per @MadProgrammer's comment:
Here's the updated
paintComponent
method using the Shapes API:Which produces the following output: