gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:03:37 24 4
gpt4 key购买 nike

我有一个具有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com