-2

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

  • Can you please share more details/code? It seems, you're drawing the same rectangle to the exact same position twice. – Tivi Jul 30 '22 at 14:42
  • I changed the size of the rectangle, if I change the color of the second rectangle, nothing will change. If you remove the first rectangle, the second one starts working if anything – Jenka 20091 Jul 30 '22 at 14:45
  • 1
    Please share the whole code you have. – Tivi Jul 30 '22 at 14:49
  • The code and picture you posted makes no sense. If you paint at (0. 0) then the rectangle should be at the top/left of the panel, not somewhere in the middle vertically. This tells me you are using some kind of layout (that we can't guess) and your panel is too small so you can't see the second rectangle. – camickr Jul 30 '22 at 15:28
  • Post a proper [mre] demonstrating the problem. A few random lines doesn't give us enough context on how the code is used. Also, start by reading the Swing tutorial on [Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for working examples and a better way to structure your code. – camickr Jul 30 '22 at 15:32
  • @camickr, Pay attention to the line "graphics.translate(0, 150);", it means that any object will be drawn on the coordinates that are specified in this line ( x=0, y=150 ) – Jenka 20091 Jul 30 '22 at 15:41
  • 1
    @Jenka20091 check my answer for the solution..you might overly complicated this. – Tivi Jul 30 '22 at 16:02
  • @Jenka20091, I know what translate does. Do you understand my comment about the rectangle being drawn out of the bounds of the panel? I was trying to get you to think about what two translates in a row will do. Also what effect would (0, 150) in addition to the translate do. You have asked multiple questions in the past. **Every** question should include an [mre] so we have all the information. I'm still waiting for your MRE. – camickr Jul 30 '22 at 16:57
  • @camickr, no, the position is absolute and does not go over the edges – Jenka 20091 Jul 30 '22 at 17:41

1 Answers1

0

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(); 
        
    
}
WJS
  • 36,363
  • 4
  • 24
  • 39
  • I need to use "translate" twice – Jenka 20091 Jul 30 '22 at 17:42
  • @DontKnowMuchButGettingBetter Probably because I made some assumptions about how the OP might be doing this which were not part of question. So I went ahead and added the correct way. This would avoid future questions. – WJS Jul 30 '22 at 17:42
  • @Jenka20091 is that a requirement? If so then fine. But that wasn't mentioned in the question. – WJS Jul 30 '22 at 17:43
  • @WJS, uou use "translate" once and then draw two rectangles by setting them the x coordinate. And I need to set these coordinates only using "translate". There is a reason for this – Jenka 20091 Jul 30 '22 at 17:47
  • @Jenka20091 At what coordinates do you want to draw both rectangles? I will do that using translate. – WJS Jul 30 '22 at 17:53
  • I've redone your code look what happened: https://pastebin.com/dw0nzVuy https://i.ibb.co/v3G2WW4/image.png – Jenka 20091 Jul 30 '22 at 17:56
  • @Jenka20091. Remember that subsequent translates are additive So translate(0,150). 0,0, draws at 0,150. translate(0,150), 0,0 now draws at 0,300. drawRect(0,150, 150, 65). draws starting at 0, 450. Did you know that? Your panel may be to small to see the second rectangle. – WJS Jul 30 '22 at 17:59
  • @WJS, Then what do you say to that? https://pastebin.com/nGsgRtwD – Jenka 20091 Jul 30 '22 at 18:11
  • I modified my answer to increase the window size and show you the rectangles as you draw them. You can change the locations by using signed values in the translate method. That should solve your problem. I hope this helped you. – WJS Jul 30 '22 at 18:14
  • I didn't understand you, you can see again what mistake I made in my new code. I did **translate** at coordinates **0, 150** and then returned it back ( **0, -150 which should eventually return 0, 0** ) – Jenka 20091 Jul 30 '22 at 18:23
  • 1
    @Jenka20091 *I've redone your code look what happened* - Don't post code on pastebin. The code should be posted with your question. The code here is an example of an MRE. So you can see how easy it is to post short code. Update your question. Your statement that you need to use translate makes no sense since your drawRect(...) statement uses a y value. – camickr Jul 30 '22 at 18:27
  • 1
    I REMOVED "graphics.dispose()" AND EVERYTHING WORKED, HOORAY. Thanks @WJS for the help and others who tried to help me – Jenka 20091 Jul 30 '22 at 18:29