我对此非常陌生,所以我很抱歉,如果这是一件非常容易让我失踪的事情!
我正在完成一项任务,我应该根据用户提供的命令制作一个在屏幕上移动的昆虫。我遇到的问题是当我使用JButton来使用move()方法时。如果我每次都打印出它的位置,我知道它正确地调用了move()方法,但我不能每次都重新绘制它。所以,基本上,我点击我的移动按钮,我的错误只是坐在那里。我希望有人能指出我正确的方向让它重新粉刷。
这个第一堂课(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);
}
}
有没有人对我如何重拍有任何见解?我知道程序本身并不完全正常,但我相信一旦我弄清楚这部分,我就可以完成它。
答案 0 :(得分:1)
首先,你没有覆盖函数paintComponent,因为你没有扩展swing接口的任何摆动,所以没有必要给这个函数命名。您当然可以使用适当的Graphics
处理程序来绘制Bug
,从应用程序的主Swing界面中获取。{1}}。
您需要有一个更新计时器或循环,并在其中更新位置和其他位置绘制Bug
的位置。
答案 1 :(得分:0)
我认为您的代码中存在两个问题。首先,不使用Component中的两个内部类。因此,如果单击按钮,则不会发生任何事情,因为它们未链接到任何代码。我建议在你的按钮上添加一个ActionListener:
JButton moveButton = new JButton("Move");
moveButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
testBug.move();
repaint();
}
});
JButton turnButton = new JButton("Turn");
turnButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
testBug.turn();
repaint();
}
});
我已添加repaint()
。那个bug开始移动了。