I want to draw a straight line in JFrame using Line2D.Double, also I want do it with constructor (not through method). How I must declare variable g like in example on docs.oracle.com?
public void paint (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
...
}
Painting is always done in a method. In particular custom painting in Swing is done in the paintComponent() method of a Swing component like JComponent or JPanel.
Read the Swing tutorial on Custom Painting for a better explanation and examples.
1) I want to draw a straight line in JFrame using Line2D.Double, that not possible you have to put there JLabel, JPanel or plain JComponent, example here
2) if you want to paint directly to the JFrame, then you have to (use method paint()) to the GlassPane or RootPane
3) for Swing is there method painComponent(as mentioned both posters), not paint
The drawing/painting occurs event driven. That is when a part of the JFrame has to be (re-)drawn paint and paintComponent will be called.
You can merely add a component to the content pane of the JFrame in the constructor. That would then draw the line by overriding paintComponent.
You could create a JPanel, which has a:
List<Shape> shapes;
shapes.add(new Line2D.Double(...));
and then in paintComponent draw all shapes. But that is overdone.
You mean how to get a Graphics2D object from with a constructor? That is not the way to do it, Graphics2D is an interface and the implementation classes are not in the API. However, you can get a graphics object from any component with
(Graphics2D)component.getGraphics()
Related
so when i try to create a paint method i.e.
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
}
my JFrame window will go completely black when I run my program. If I remove the paint method from the code, my normal images will appear in my JFrame window. I know FOR SURE that I am not using a .paint() method anywhere else in my code because I just added this method to my code now. Does anybody know why this is happening?
update, I am using eclipse
Start by taking a look at Performing Custom Painting and Painting in AWT and Swing to be better understand how painting works in Swing.
Basically, the paint subsystem is calling your paint method as required, when it decides that the component needs to be updated.
Because you've overridden it to basically do nothing, then nothing is painted.
As a general piece of advice, you should avoid overriding paint of top level containers like JFrame, because they are actually composite components. That is, they are actually made of a series of layers which the UI is built on
See How to use root panes for more details, but basically...
I have a canvas and I want to draw a rectangle based on a JButton click.
So in other words
private void jb_drawActionPerformed(ActionEvent evt) {
// draw a rectangle method
}
Basically, how do I encorporate the pain(Graphics g) thingamagic in that method? or should I make the rectangle an object and call a "render" method from that object? If so, can someone link a tut?
private void jb_drawActionPerformed(ActionEvent evt) {
myrectange.render(x,y); // ????
}
General Comments and Recommendations
One way: Draw in a BufferedImage getting your Graphics object from the BufferedImage, and then draw the BufferedImage in the JComponent's (JPanel's?) paintComponent method.
If you do it this way, you will use the Graphics object directly obtained from the BufferedImage to do your drawing.
Don't forget to dispose of this Graphics object when done with it.
The actual drawing though is done in the JPanel's paintComponent(...) method (see below).
Another way: change a class field, and have the JPanel's paintComponent method use that field when painting. For instance, if you want to paint multiple Rectangles, create an ArrayList<Rectangle> add to it in your ActionListener, call repaint() and have the paintComponent(...) method iterate through the List, drawing rectangles held by it.
Note that the paintComponent(...) method is never called directly but rather you suggest to the JVM that it call it by calling repaint().
Never dispose of a Graphics object that was given to you by the JVM, for instance the one passed into the paintComponent(Graphics g) parameter.
Links
Basic Tutorial: Lesson: Performing Custom Painting
More advanced concepts: Painting in AWT and Swing
Referring to my earlier question:
Based on your valuable answers, I choose Java 2D library to cater my requirements.
I completely read the above said library and have full understanding of dealing with the graphics stuff, like what to draw and how to draw etc.Now i only left with one question that how do i draw my required shapes inside JPanel and after drawing all those shapes how do i place that JPanel inside a JScrollPane?
Override paintComponent(Graphics g) method.
Cast Graphics to Graphics2D and use drawShape() method passing all your Shapes
You will typically draw your stuff inside a subclass of JComponent, say, MyComponent.
Create an instance of MyComponent, MyComponent myc = new
MyComponent();
Put that inside a JScrollPane, e.g. JScrollPane jsp =
new JScrollPane(myComponent);
Then add the JScrollpane to the
JPanel (exact code depends on the layout manager)
i have a jrame on which i add some JComponent objects.
Each JComponent has a list of containers i add by using JComponent.add( Component).
Now in my main JComponent class, called MyComponent, i override the protected method paintComponent where i can paint things, which works pretty fine.
But i dont want to paint on the main JComponent, i only want to paint on the containers i have added to my main JComponent.
So in MyComponent in paintComponent i do the following.
protected void paintComponent( Graphics g) {
super.paintComponent( g);
Graphics page_g = this.getPage( "main").getGraphics();
page_g.setColor( Color.RED);
page_g.drawRect( 10, 10, this.getWidth() - 20, this.getHeight() - 20);
page_g.setColor( Color.BLUE);
page_g.drawString( "HELLO WORLD", this.getWidth() / 2, this.getHeight() / 2);
}
The line this.getPage( "main").getGraphics(); takes the Graphics object from one of my containers added to the MyComponents list of containers and of course to the main component list using JComponents add method. The container is set to visible by calling the setVisible( true); method.
But nothing happens. The screen is empty. When i replace page_g with g, then painting works, because its painting on my JComponent (MyComponent), but i want to paint on the container which is a children of MyComponent in this case.
I often heard "Never use getGraphics()". But how else can ONLY draw on sub components of a parent component when the parents paintComponent method gets called?
Really the best bet is to have the classes that are actually doing the custom painting override their own paintComponent() method. Let the AWT worry about the graphics contexts.
inside a paintcomponent. it takes g as parameter, and g can be graphics or graphics2d. the class extends jpanel. then:
super.paintComponent(g);
this.setBackground( Color.BLACK );
if g is graphics it works, but if it's graphics2d it doesn't. it compiles with both, but graphics2d doesn't change the background color. how come?
JPanel (which is a subclass of JComponent) only has a paintComponent(Graphics) method. It does not have a method with the signature paintComponent(Graphics2D).
Overriding the paintComponent(Graphics) method can be accomplished by the following:
public void paintComponent(Graphics g)
{
// Do things.
}
However, defining a method with the signature with paintComponent(Graphics2D) like the following is legal, but it won't ever be called, as it is not overriding any method that is defined in JComponent:
public void paintComponent(Graphics2D g)
{
// Do things.
// However, this method will never be called, as it is not overriding any
// method of JComponent, but is a method of the class this is defined in.
}
The Java API specifications for the JComponent class (which is the superclass of JPanel) has a method summary which lists all the methods that are part of the class.
More information about painting in Swing;
Lesson: Performing Custom Painting from The Java Tutorials
Painting in AWT and Swing