Home > other >  Java: Drawing two rectangles breaks
Java: Drawing two rectangles breaks

Time:07-31

I filled two rectangles in this way:

graphics.setColor(Color.RED);
graphics.translate(0, 150);
graphics.fillRect(0,0,65,65); // First rect
graphics.dispose(); // If remove this line, nothing will change
graphics.translate(0, 150);
graphics.fillRect(0,150,100,65); // Second rect

For some reason, only one rectangle is rendered :(

see the screenshot

CodePudding user response:

First, translates are additive. So your panel may not be tall enough to show the second rectangle. See the following code and comments.

graphics.setColor(Color.RED);
graphics.translate(0, 150);         // 0,0 is now at 0,150
graphics.fillRect(0,0,65,65); 
graphics.translate(0, 150);         // 0,0, is now at 0,300
graphics.fillRect(0,150,100,65);    // 0,150 is drawing at 
                                    // 0,450 with a size of 100,65

Since you are new to painting I will provide more information that may be of use.

  • don't subclass JFrame. It is a top level class meant to hold other components. Subclass JPanel and add that to it. Or create another class that subclasses JPanel.
  • override paintComponent(Graphics g) for doing your painting.
  • call super.paintComponent(g) first to process parent method bookkeeping
  • As a general rule, I find it helps to fill the object first then do the outline/border. It will simply overwrite the original figure with the outline.
  • it is considered standard practice to override getPreferredSize so other areas may not arbitrarily change the size.
  • call frame.pack() to size the frame and layout the components.
public class RectanglesExample extends JPanel {
    
    JFrame f = new JFrame("Rectangles");
    
    public static void main(String[] args) {

            SwingUtilities.invokeLater((()-> 
                    new RectanglesExample()));
           
        }
    
    public RectanglesExample() {
        
        setBackground(Color.WHITE);
        f.add(this);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
    
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 700);
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g.create();
        graphics.setColor(Color.RED);
        graphics.translate(0, 150);
        graphics.fillRect(0,0,65,65); // First rect at 0,150
        graphics.translate(0, 150);
        graphics.fillRect(0,150,100,65); // Second rect at 0,450
        graphics.dispose(); 
        
    
}

CodePudding user response:

I went ahead and drew you two rectangles.

enter image description here

and the full code: (pay attention, you should not start translate from 0 as it will cut the edges.)

package firstProject;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class DrawRectangle extends JFrame {

  @Override
  public void paint(Graphics graphics) {
      graphics.setColor(Color.RED);
      graphics.translate(150, 150);
      
      graphics.fillRect(0,0,65,65);
      graphics.fillRect(100,0,65,65);
  }

  public static void main(String[] args) {
    DrawRectangle frame = new DrawRectangle();
    frame.setSize(500, 500);
    frame.setVisible(true);
  }
}
  • Related