About positions a Rectangle on the screen - java

I have a JFrame and a class extending JPanel and overriding paintComponent() (named DrawingPanel). An instance of DrawingPanel is added to the JFrame.
This is the paintComponent() method of DrawingPanel:
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Rectangle rect = new Rectangle(80,180,50,50);
g2d.fill(rect);
}
I ran the program, and it seems that the coordinats 80, 180 specified when creating rect, refer to the JFrame that contains the instance of DrawingPanel. I would expect that these coordinates would refer to the JPanel where the rectangle is actually painted.
1) Is it supposed to be like that?
2) Is it possible to change this?
Thanks

I would expect that these coordinates would refer to the JPanel where the rectangle is actually painted.
They do refer to the JPanel, not the frame.
Why do you think they refer to the frame? Post your SSCCE that demonstrates the problem.

Related

I'm trying to draw shapes in a JPanel extending a JComponent and then putting the component inside the panel but it won't work

I'm trying to draw shapes in a JPanel extending a JComponent and then putting the component inside the panel but it won't work. I've got a JFrame (500, 500) and I need the right half of it to have things drown inside. I'm clearly doing something wrong though!
This is the code I've used:
public class Componente extends JComponent{
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape linea = new Rectangle2D.Float(50, 50, 50, 50);
Shape cerchio = new Ellipse2D.Double(100, 100, 50, 50);
g2.setPaint(Color.BLUE);
g2.draw(linea);
g2.draw(cerchio);
}
}
public class PannelloDx extends JPanel{
PannelloDx(){
this.setBackground(Color.CYAN);
this.setSize(Esercitazione_Berni1703.finestra.getWidth()/2, Esercitazione_Berni1703.finestra.getHeight());
this.setLocation(Esercitazione_Berni1703.finestra.getWidth()/2, 0);
this.add(new Componente());
}
}
Now, the output shows the Cyan panel into the JFrame in the right half as it's supposed to. It won't show anything though!
By default a JPanel uses a FlowLayout and a FlowLayout respects the size of the component added to it. Your custom component has a preferred size of (0, 0) so there is nothing to paint.
You need to override the getPreferredSize() method of your custom panel to return the appropriate size.
Read the section from the Swing tutorial on Custom Painting for more information and examples. Check out the rest of the tutorial for Swing basics as well.

Should I extend JPanel for custom class rendering in Swing?

I have a custom class representing spaceships that will rotate and fly around on the screen. However, I realized that if I want to use Swing, I have to render the ships via paintComponent().
This is the method I was going to use for rendering:
public void render(Graphics g){
AffineTransform original = ((Graphics2D) g).getTransform();
Graphics2D g2d = (Graphics2D) g;
g2d.rotate(orientation, getGamePosition().x, getGamePosition().y);
g2d.drawImage(spriteDefault, position.x, position.y, null);
g2d.setTransform(original);
}
So in order to use Swing, should I extend JPanel so that I can override paintComponent and put this code inside that method, or should it be JComponent, or some other way?
I just want to make sure I'm on the right track here...

draw graphics outside of paint method

private void draw_shape() {
Graphics g = getGraphics();
g.drawLine(0, 0, 100, 100);
repaint();
}
In paint method only those graphics are drawn which is a part of paint method because of which
I wanted to draw shapes outside of paint method.
This code draws the line but it immediately disappeares, I don't understand why this is happening. please help
This doens't work because you are getting the current Graphics outside of the Swing repaint thread. Basically:
you get the current Graphics
you draw something on it
then you call repaint() that will call the paint() of the component thus discarding all you did
To make it work you should override the paint (paintComponent for Swing) method of your object:
#Override
public void paint(Graphics g) {
super.paint(g); // if you have children to the component
g.drawLine(..)
}
and then just call repaint() when something has been modified.
The line disappears because Swing (or AWT) will call paint(Graphics) or paintComponent(Graphics g) in order to pain the component.
What you need to do is to put your drawing logic on the paint(Graphics) or paintComponent(Graphics g) method. The latter is more advisable.
If you really need to draw things using another method, store an image as a class field and draw this image on the paint or paintComponent methods.
Because the paint method also paints stuff. You should not draw graphics outside the paint method. You should instead override the paint method, like this:
#Override public void paint (Graphics g) {
super.paint(g);
g.drawLine(0, 0, 100, 100);
}
Thanks for the help found the answer
BufferedImage image = (BufferedImage) createImage(300, 300);
image.getGraphics().drawLine(0, 0, 300, 300);
jLabel1.setIcon( new ImageIcon(image ));

How to make a graphic line into JFrame?

I use Eclipse, and I want make a graphic line into JFrame by following code:
public void Makeline () {
Graphics g=new Graphics(); // has error
Graphics2D g2 = (Graphics2D) g;
g2.draw(new Line2D.Double(0, 0, 20, 20));
}
but give followin error:
Cannot instantiate the type Graphics
Graphics is an abstract class, defining the requirements of the overall API.
Painting in Swing is done within the context of the paint chain. This is typically performed within the paintComponent method of components that extend from JComponent
Take a look at Perfoming Custom Painting for more details
You can also use a BufferdImage to generate a Graphics context, but you still need somewhere to draw the image, so it comes down what your are trying to achieve.
The solution is to overwrite the paintComponent method, but the JFrame isn't a JComponent, so instead of JFrame, use JPanel and then add the JPanel to the JFrame.
paintComponent(Graphics g) {
super.paintComponent(g)
//here goes your code
Graphics2D g2 = (Graphics2D) g;
...
}
Graphics is an abstract class. You can't instantiate following way.
Graphics g=new Graphics();
To access Graphics2D, first you need to override paint(Graphics) method.
#Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
}

Drawing graphics in java - NetBeans IDE

I created a new JApplet form in NetBeans:
public class UI extends javax.swing.JApplet {
//generated code...
}
And a JPanel in design mode named panou:
// Variables declaration - do not modify
private javax.swing.JPanel panou;
How do I get to draw a line on panou? I've been searching for this for 5 hours now so a code snippet and where to place it would be great. Using Graphics2D preferably.
Go to design mode
Right Click on the panel "panou"
Click "Costumize code"
In the dialog select in the first combobox "costum creation"
add after = new javax.swing.JPanel() this, so you see this:
 
panou = new javax.swing.JPanel(){
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g); // Do the original draw
g.drawLine(10, 10, 60, 60); // Write here your coordinates
}
};
Make sure you import java.awt.Graphics.
The line that you will see is always one pixel thick. You can make it more "line" by doing the following:
Create this method:
public static final void setAntiAliasing(Graphics g, boolean yesno)
{
Object obj = yesno ? RenderingHints.VALUE_ANTIALIAS_ON
: RenderingHints.VALUE_ANTIALIAS_OFF;
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, obj);
}
And add after super.paintComponent(g); (in your costum creation) this:
setAntiAlias(g, true);
Edit:
What you are doing wrong is: you paint the line once (by creating the frame).
When you paint the line the frame is also invisible. The first draw is happening when the frame becomes visible. The frame will be Repainted, so everything from the previous paint will disappear.
Always you resize the frame, everything will be repainted. So you have to make sure each time the panel is painted, the line also is painted.
To do custom painting in a JPanel, one would need to make a subclass of a JPanel, and then overload the paintComponent method:
class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
// Perform custom painting here.
}
}
In the example above, the MyPanel class is a subclass of JPanel, which will perform whatever custom painting is written in the paintComponent method.
For more information on how to do custom painting in Swing components, Lesson: Performing Custom Painting from The Java Tutorials have some examples.
If one wants to do painting with Java2D (i.e. using Graphics2D) then one could do some painting on a BufferedImage first, then draw the contents of the BufferedImage onto the JPanel:
class MyPanel extends JPanel {
BufferedImage image;
public MyPanel() {
Graphics2D g = image.createGraphics();
// Do Java2D painting onto the BufferedImage.
}
public void paintComponent(Graphics g) {
// Draw the contents of the BufferedImage onto the panel.
g.drawImage(image, 0, 0, null);
}
}
Further reading:
Painting in AWT and Swing
Trail: 2D Graphics

Categories

Resources