나는 이것에 상당히 익숙하다. 그래서 이것이 내가 놓치고있는 매우 쉬운 무엇인가라면 나의 사과!그래픽 문제 다시 그리기
저는 사용자가 지정한 명령을 기반으로 화면에서 움직이는 곤충을 만들기로되어 있습니다. 내가 가지고있는 문제는 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);
}
}
누구에게 내가 다시 칠할 수 있는지에 대한 통찰력이 있습니까? 나는 프로그램 자체가 아직 완전히 기능적이지는 않지만, 일단이 부분을 이해하면 끝낼 수 있다고 확신한다.
업데이트는 이벤트 수신기를 기반으로하므로 버그를 타이머를 사용하여 업데이트해야합니다. –
정말 고마워, 이건 정말 도움이 !! 그것은 나를 위해 움직이고 있습니다! – user3574245