키패드를 통해 제어되는 모양이 있으며 타이머와 연결되어 있습니다. 모양이 특정 좌표 점에 도달하면 타이머가 중지됩니다. 그렇다면 타이머를 멈춘 곳의 도형을 그대로 남겨두고도 원래의 시작 위치로 되돌려 놓는 방법은 어떻게 될까요? 게시물의 코드에서 잘못된 너무 많은 일이있을 때다른 모양이 원하는 x 및 y 좌표에 도달 한 후에 동일한 모양을 표시하는 방법은 무엇입니까?
public class ForStack extends JPanel implements KeyListener,ActionListener{
Timer t = new Timer(800, this);
int count = 0;
double x = 0, y = 0, velX = 0, velY = 0;
public ForStack(){
t.start();
addKeyListener(this);
setFocusable(true);
}
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle2D rectangle = new Rectangle2D.Double(x,y,50, 50);
g2.setColor(Color.BLACK);
g2.fill(rectangle);
int counter = 0;int x = 0; int y = 0;
g.setColor(Color.RED);
if (counter == 0){
do{
g.drawRect(x,y,50,50);
x += 50;
counter++;
}while(counter != 6);
}
x=0;
y=0;
if (counter == 6){
do{
g.drawRect(x,50,50,50);
x += 50;
counter++;
}while(counter != 12);
}
x=0;
y=0;
if (counter == 12){
do{
g.drawRect(x,100,50,50);
x += 50;
counter++;
}while(counter != 18);
}
x=0;
y=0;
if (counter == 18){
do{
g.drawRect(x,150,50,50);
x += 50;
counter++;
}while(counter != 24);
}
x=0;
y=0;
if (counter == 24){
do{
g.drawRect(x,200,50,50);
x += 50;
counter++;
}while(counter != 30);
}
x=0;
y=0;
if (counter == 30){
do{
g.drawRect(x,250,50,50);
x += 50;
counter++;
}while(counter != 36);
}
x=0;
y=0;
if (counter == 36){
do{
g.drawRect(x,300,50,50);
x += 50;
counter++;
}while(counter != 42);
}
x=0;
y=0;
if (counter == 42){
do{
g.drawRect(x,350,50,50);
x += 50;
counter++;
}while(counter != 48);
}
x=0;
y=0;
if (counter == 48){
do{
g.drawRect(x,400,50,50);
x += 50;
counter++;
}while(counter != 54);
}
x=0;
y=0;
if (counter == 54){
do{
g.drawRect(x,450,50,50);
x += 50;
counter++;
}while(counter != 60);
}
}
@Override
public void actionPerformed(ActionEvent e){
if(x == 0 & y == 400){
t.stop();
}
repaint();
x += velX;
y += velY;
}
@Override
public void keyPressed(KeyEvent e){
int code = e.getKeyCode();
if (code == KeyEvent.VK_DOWN){
velY += 50;
velX += 0;
}
else if (code == KeyEvent.VK_LEFT){
velX -= 50;
velY = 0;
}
else if (code == KeyEvent.VK_RIGHT){
velX += 50;
velY = 0;
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e){}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new ForStack());
frame.setVisible(true);
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // end of main
} // end of class
&가 :(난 정말 이해가 안되지 & &를 사용하고 싶습니다. 어떻게 내가 잘못하고있는 중이 야? –
더 도움이 필요하면 더 빨리,이 [MCVE]를 게시 (HTTP : //stackoverflow.com/help/mcve) (최소한의 완전하고 검증 가능한 예제) –