java - 在另一个函数中声明的 Canvas 上绘图

标签 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中声明的 Canvas 上呢?

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 中在 Canvas 上绘制的内容,以及我在 Object 类中绘制的内容。

谢谢。

最佳答案

您想要做的是让对象中的 draw 方法采用 Graphics 参数。此 Graphics 对象将与您的 paintComponent 方法中的 Graphics 上下文相同。您可以在 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);
    }
}

您可以在Circle 类中为xy 设置一个setter。您应该在 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);
    }
}

关于java - 在另一个函数中声明的 Canvas 上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21743588/

相关文章:

java - 如何使用 java 休息来使用 Angular 渲染 nv3d 烛台图?

java - 已发送带有确认链接的 PHP 电子邮件,但确认链接不起作用

java - JGraphX 图形未显示在 JPanel 中

javascript - 使用变量动态访问对象属性

javascript - 检查键是否不存在或应在过滤方法中具有特定值

javascript - 如何将对象数组映射到具有重复值标识符的数组?

java - Eclipse 中的 Jar 文件(项目)

java - 父RecyclerView的子RecyclerView的项目点击事件

java - JPanel 中的重叠组件

java - MouseListener 帮助 Java