gpt4 book ai didi

java - 重新绘制图形问题

转载 作者:行者123 更新时间:2023-12-01 13:05:26 25 4
gpt4 key购买 nike

我对此还很陌生,所以如果我错过了一些 super 简单的东西,我很抱歉!

我正在做一项任务,我应该制作一种根据用户给出的命令在屏幕上移动的昆虫。我遇到的问题是当我使用 JButton 来使用 move() 方法时。如果我每次都打印出它的位置,我知道它正在正确调用 move() 方法,但我无法每次都重新绘制它。所以,本质上,我点击了移动按钮,我的 bug 就坐在那里。我希望有人能给我指出正确的方向,以便重新粉刷它。

第一个类(BugTester)有我的主类,并且是运行整个程序的:

public class BugTester {

public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(700, 700);
frame.setTitle("Final Project - Grant Cooper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Bug testBug = new Bug(10);
final JComponent i = new JComponent() {

public void paintComponent(final Graphics g) {
testBug.paintComponent(g);
JPanel panel = new JPanel();
JButton moveButton = new JButton("Move");
JButton turnButton = new JButton("Turn");
panel.add(turnButton);
panel.add(moveButton);
frame.add(panel);
class ClickListenerMove implements ActionListener {

public void actionPerformed(ActionEvent event) {
testBug.move();

System.out.println(testBug.getPosition());

}
}
class ClickListenerTurn implements ActionListener {

public void actionPerformed(ActionEvent event) {
testBug.turn();

}
}
}

};

frame.add(i);
frame.setVisible(true);

}

程序的其余部分在这里: 封装SecondAttempt;

import java.awt.*;
import java.awt.geom.*;
public class Bug {

public int leftRight;
public int direction;

//Bug constructor
public Bug(int initialPosition) {
leftRight = initialPosition;
direction = 1;

}

public void turn() //Turn Bug around - currently only moves in 2 dimensions, need to add up and down
{
switch (direction) {
case 1:
direction = 1;
case 2:
direction = -1;
}

}

public void move() {
getPosition();
getDirection();
if (direction > 0) {
leftRight++; //move Bug right
System.out.println(getPosition());
}
if (direction < 0) {
leftRight--; //move Bug left
System.out.println(getPosition());
}
}

public int getPosition() {

return leftRight;
}

public int getDirection() {

return direction;
}

//create Bug
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;

//create Bug body
//(X Coord of body, Y Coord of body, left/right stretch, up/down stretch)
Ellipse2D.Double body = new Ellipse2D.Double(300 + getPosition(), 310, 85, 25);
g2.draw(body); //draw Bug body
g2.setColor(Color.RED);
g2.fill(body); //color Bug body
g2.setColor(Color.BLACK); //reset color

//create Bug head
//(X coord head, y coord head, left/right stretch, up/down stretch)
Ellipse2D.Double head = new Ellipse2D.Double(380 + getPosition(), 312, 25, 20);
g2.draw(head); //draw Bug head
g2.setColor(Color.GREEN);
g2.fill(head); //color Bug head
g2.setColor(Color.BLACK); //reset color

//create Bug legs
//First set of legs
//(Top part of leg x position, top y position, bottom x position, bottom y position)
g2.setColor(Color.YELLOW);
Line2D.Double leg1_left = new Line2D.Double(365 + getPosition(), 295, 365 + getPosition(), 310);
g2.draw(leg1_left);
g2.setColor(Color.YELLOW);
Line2D.Double leg1_right = new Line2D.Double(365 + getPosition(), 333, 365 + getPosition(), 349);
g2.draw(leg1_right);
g2.setColor(Color.YELLOW);
//Second set of legs
Line2D.Double leg2_left = new Line2D.Double(341 + getPosition(), 292, 341 + getPosition(), 310);
g2.draw(leg2_left);
g2.setColor(Color.YELLOW);
Line2D.Double leg2_right = new Line2D.Double(341 + getPosition(), 336, 341 + getPosition(), 354);
g2.draw(leg2_right);
g2.setColor(Color.YELLOW);
//Third set of legs
Line2D.Double leg3_left = new Line2D.Double(320 + getPosition(), 295, 320 + getPosition(), 310);
g2.draw(leg3_left);
g2.setColor(Color.YELLOW);
Line2D.Double leg3_right = new Line2D.Double(320 + getPosition(), 333, 320 + getPosition(), 349);
g2.draw(leg3_right);
g2.setColor(Color.YELLOW);

//create Bug antennae
//(left x, left y, right x, right y)
Line2D.Double antenna1 = new Line2D.Double(403 + getPosition(), 315, 410 + getPosition(), 315);
g2.draw(antenna1);
g2.setColor(Color.YELLOW);
Line2D.Double antenna2 = new Line2D.Double(403 + getPosition(), 329, 410 + getPosition(), 329);
g2.draw(antenna2);
g2.setColor(Color.YELLOW);

}
}

有人知道如何重新绘制它吗?我知道程序本身还不能完全发挥作用,但我有信心一旦弄清楚这部分就可以完成它。

最佳答案

首先,您没有重写函数paintComponent,因为您没有扩展swing 接口(interface)的任何swing,因此没有必要这样命名该函数。当然,您必须使用适当的 Graphics 处理程序来绘制您的 Bug,您可以从应用程序的主 Swing 界面获取该处理程序。

您需要有一个更新计时器或循环,并在其中更新一个位置和另一个绘制 Bug 的位置。

关于java - 重新绘制图形问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23301617/

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