在另一个函数中声明的画布上绘图

时间:2014-02-13 01:40:28

标签: java swing object paintcomponent

我有一个具有swing用户界面类的应用程序,它具有将变量发送到canvas类的按钮;

public class createWindow extends JFrame implements ActionListener
{
    createCanvas canvas = new createCanvas(10, 10);
    JPanel mainPanel = new JPanel();
    public createWindow()
    {
        mainPanel.add(canvas, BorderLayout.CENTER);
    }
}

createCanvas是一个声明paintComponent;

的类
public class createCanvas extends JPanel
{
    int xValue;
    int yValue;
    int xCoordinate;
    int yCoordinate;

    public createCanvas(int x, int y)
    {
        xValue = x;
        yValue = y;
    }

    public void setXCoordinate(int x)
    {
        xCoordinate = x;
    }

    public void setYCoordinate(int y)
    {
        yCoordinate = y;
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        drawGrid(g, this.xValue, this.yValue);
        g.fillArc(xCoordinate, yCoordinate, 6, 6, 0, 360);
    }

    public drawGrid(Graphics g, int xLength, int yLength)
    {
        //creates a grid that is xLength x yLength
    }
}

但是,我还有一些对象,我想要一个.draw()函数,它可以使用createCanvas中的paintComponent。 问题是,当我需要在网格上绘制节点时,我可以设置坐标,但是如何在createWindow中声明的画布上显示节点?

public class Node()
{
    int xCoordinate;
    int yCoordinate;

    //Suitable constructors, sets, gets
    public void draw()
    {
        createCanvas canvas = new createCanvas();
        canvas.setXCoordinate(this.xCoordinate);
        canvas.setYCoordinate(this.yCoordinate);
        canvas.repaint();
    }
}

所以,我想知道是否有办法让我在createWindow中保留我在画布上绘制的内容,以及我在Object类中绘制的内容。

感谢。

1 个答案:

答案 0 :(得分:1)

您要做的是让对象中的draw方法采用Graphics参数。此Graphics对象与Graphics方法中的paintComponent上下文相同。您可以在JPanel课程中创建对象。像这样的东西

public class Circle {
    int x, y;

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

    public void drawCirlce(Graphics g) {
        g.fillRect(x, y, 50, 50);
    }
}

然后在你JPanel

public class CirclePanel extends JPanel {
    Circle circle = new Circle(100, 100);

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        circle.drawCircle(g);
    }
}

setter课程中的xy可以Circle。您应该在JPanel课程的某处更改它们,然后再调用repaint()。您可以通过按键移动它,也可以使用java.util.Timer为其设置动画。例如

使用Timer

public class CirclePanel extends JPanel {
    Circle circle = new Circle(100, 100);

    public CirclePanel() {
        Timer timer = new Timer(50, new ActionListener(){
            @Override
            public void actionPerfomed(ActionEvent e) {
                circle.x += 10;
                repaint();
            }
        });
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       circle.drawCircle(g);
    }
}

使用key binding

public class CirclePanel extends JPanel {
    Circle circle = new Circle(100, 100);

    public CirclePanel() {
        InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED_IN_WINDOW);
        inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "moveRight");
        getActionMap().put("moveRight", new AbstractAction(){
            public void actionPerformed(ActionEvent e) {
                circle.x += 10;
                repaint();
            } 
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       circle.drawCircle(g);
    }
}

使用button press

public class CirclePanel extends JPanel {
    Circle circle = new Circle(100, 100);

    public CirclePanel() {
        JButton button = new JButton("Move Right");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                circle.x += 10;
                repaint();
            } 
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       circle.drawCircle(g);
    }
}