调整窗口后,防止对象消失

时间:2018-02-15 00:37:06

标签: java user-interface paintcomponent

我需要创建一个程序,允许用户通过单击要添加形状的位置来向窗口添加形状。但是,我一直有一个问题是调整窗口大小导致形状被清除。

简单地说,在我调整窗口后如何保留先前绘制的对象?

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.Locale;

import javax.swing.*;

public class GUIApp 
{
    public static void main(String[] args)
    {
        JFrame f = new AppFrame("GUI Frame Practice");
    }
}

class AppFrame extends JFrame
{
    JPanel pnlText, pnlGraphics;

    public AppFrame(String title)
    {
        super(title);
    }

    public void frameInit()
    {
        super.frameInit();
        this.setLayout(new GridLayout(1, 2));
        this.add(createContainer());
        this.setSize(1000, 800); // first width then height
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private Component createContainer()
    {
        JPanel container = new JPanel();
        JPanel buttons = new ButtonsPanel();

        container.setLayout(new BorderLayout());
        container.add(buttons, BorderLayout.CENTER);
        return container;
    }
}

class ShapeFactory
{
    static GeoShape createShape(String kind, int x, int y,int size)
    {   
        if(kind.equalsIgnoreCase("rect"))
            return new Rect(x, y, size);
        else if(kind.equalsIgnoreCase("circle"))
            return new Circle(x, y, size);
        else if(kind.equalsIgnoreCase("triangle"))
            return new Triangle(x, y, size);
        else 
            return null;
    }

}   



abstract class GeoShape
{
    int x, y, size;

    public GeoShape(int x, int y, int size)
    {
        this.x = x;
        this.y = y;
        this.size = size;
    }

    public abstract void draw(Graphics g);
}

class Rect extends GeoShape
{   
    public Rect(int x, int y, int size)
    {
        super(x, y, size);
    }

    @Override
    public void draw(Graphics g)
    {
        g.drawRect(x, y, size, size);
    }

}

class Circle extends GeoShape
{   
    public Circle(int x, int y, int size) 
    {
        super(x, y, size);
    }

    @Override
    public void draw(Graphics g)
    {
        g.drawOval(x, y, size, size);
    }   
}

class Triangle extends GeoShape
{

    public Triangle(int x, int y, int size)
    {
        super(x,y,size);
    }

    @Override
    public void draw(Graphics g)
    {
        g.drawLine(x, y, (x+size), y);
        g.drawLine(x, y, x+(size/2), (y-size));
        g.drawLine((x+size), y, x+(size/2), (y-size));
    }
}

class ButtonsPanel extends JPanel
{
    JPanel graphics;

    JButton btnTri, btnRect, btnCirc;
    JPanel pnlButtons;
    String Type;

    public ButtonsPanel()
    {
        super();
        this.addMouseListener(new MsListener());
        Type = "Circle";
        setup();    
    }

    private void setup()
    {
        graphics = new JPanel();

        btnTri = new JButton("Triangle");
        btnRect = new JButton("Rectangle");
        btnCirc = new JButton("Circle");
        pnlButtons = new JPanel();

        this.setLayout(new BorderLayout());
        pnlButtons.add(btnTri);
        pnlButtons.add(btnRect);
        pnlButtons.add(btnCirc);
        this.add(pnlButtons, BorderLayout.SOUTH);

        btnTri.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e) 
            {
                Type = "triangle";
            }
        });

        btnRect.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e) 
            {
                Type = "rect";
            }
        });


        btnCirc.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e) 
            {
                Type = "circle";
            }
        });     
    }

class MsListener extends MouseAdapter
{ //instead of implements MouseListener

    @Override
    public void mousePressed(MouseEvent e) 
    {
        GeoShape s = ShapeFactory.createShape(Type, e.getX(), e.getY(), 50);
        s.draw(getGraphics());  
    }   
}


}

我知道他们有几种方法可以完成这项任务,但是我还没有找到一个可以在我的代码中使用的方法。我也知道我不应该使用getGraphics(),而是使用paintComponent()。但是,我不完全确定如何实现这种方法。

0 个答案:

没有答案