无法在Java中绘制两个矩形

时间:2018-11-28 12:02:15

标签: java swing awt draw rectangles

我试图绘制2个矩形,但是当我运行程序时,我看到一个空框架。我的代码有什么问题? 头等舱:

public class Rettangolo extends JComponent {
    public void PaintComponent(Graphics g) 
    {
        Graphics2D g2 = (Graphics2D) g;
        Rectangle r1 = new Rectangle (5,10,20,30);
        g2.draw(r1);
        r1.translate(15, 25);
        g2.draw(r1);
    }
}

第二堂课:

public class VisualizzatoreRettangoli {
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();

        final int FRAME_WIDTH = 300;
        final int FRAME_HEIGHT = 400;

        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setTitle("Disegniamo rettangoli !");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Rettangolo componente = new Rettangolo();

        frame.add(componente);
        frame.setVisible(true);
    }
}

输出: Output

0 个答案:

没有答案