1

I'm trying to draw 2 triangles with Graphics 2d. When running the first segment, it's totally correct. But if I run the second segment that I add a panel below the "Graph", the triangles disappear. Could anybody help me with the issue?

Here's the code.

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;

public class Draw_A_Triangle extends JPanel {
    public Draw_A_Triangle() {}
    
    public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    work(g2d, Color.RED,30,45,60,90,60,90);
    work(g2d, Color.blue,15,22.5,30,45,30,45);

    }
    // set a method to draw the 2 triangles
    public void work(Graphics2D g2d, Color c, double x1, double x2, double x3, double y1, double y2, double y3) {

        Path2D.Double p = new Path2D.Double();
        g2d.setColor(c);
        p.moveTo(x1, y1);
        p.lineTo(x2, y2);
        p.lineTo(x3, y3);
        g2d.fill(p);
        g2d.draw(p);
    }

    public static void main(String[] args) {
        //set the JFrame
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Draw Triangle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.white);
        frame.setSize(200, 200);

//        //segment 1
//        Draw_A_Triangle panel = new Draw_A_Triangle();
//        frame.add(panel);
//        frame.setVisible(true);

//        segment 2
        Draw_A_Triangle panel = new Draw_A_Triangle();
        JPanel p = new JPanel();
        p.add(panel);
        frame.add(p);
        frame.setVisible(true);

    }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Hertz
  • 11
  • 1
  • You have added a *superb* example of a [mre]. – Andrew Thompson Apr 13 '22 at 04:53
  • As to *why* the first panel disappears, see [this answer](https://stackoverflow.com/questions/13881444/only-one-component-shows-up-in-jframe/13881481#13881481). But the core of the solution is that **one** panel should have access to a list of all shapes to paint, then iterate the list in the `paintComponent(Graphics)` method. – Andrew Thompson Apr 13 '22 at 05:02

1 Answers1

4

I recommend the following changes.

  • don't set the size of the frame. Set the size of the JPanel to eliminate the frame border from taking up panel space. This is overriding the method in the JPanel instance.
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500,500);
    }

Then do the following:

  • frame.pack() - size the frame based on layout requirements and size preferences of its components.
  • frame.setLocationRelativeTo(null) - centers frame on screen.
  • You're already subclassing JPanel so no need to create a new one. Just add panel to frame.
  • And the first thing in overidding paintComponent(Graphics g) should be super.paintComponent(g); Otherwise, required functionality like setting the background color of the panel won't work.
  • and if you want to set the background of the panel, use panel.setBackground(<someColor>)
Draw_A_Triangle panel = new Draw_A_Triangle();
 panel.setBackground(Color.white);
 frame.add(panel);
 frame.pack();
 frame.setLocationRelativeTo(null);
 frame.setVisible(true);

I recommend you check out the Java Tutorials for more on painting. Go to Really Big Index and search the TOC for painting

WJS
  • 36,363
  • 4
  • 24
  • 39
  • I would `frame.setLocationByPlatform(true);` rather than `frame.setLocationRelativeTo(null);` for most windows. A splash screen (for counter-example) should be centered on-screen. Good answer! – Andrew Thompson Apr 13 '22 at 04:48
  • 1
    Thanks, it works! I added it to the project perfectly. – Hertz Apr 14 '22 at 02:14
  • Yes. But then other classes/methods can change it. This is to prevent that and the recommended way of doing it. [This](https://stackoverflow.com/questions/10866762/use-of-overriding-getpreferredsize-instead-of-using-setpreferredsize-for-fix) may prove interesting. But ultimately it's up to the programmer. – WJS Apr 14 '22 at 16:55
  • In the past I have wanted to subclass String. But it's final so I can't. Them's the breaks. – WJS Apr 14 '22 at 18:31