java 자바 기초 타이머 문제
-
게시물 수정 , 삭제는 로그인 필요
일단 문제는 이 밑에 프로그램에서 방향버튼을 누르면 저 까만 원반이 그 방향으로 움직여야합니다!
한번 방향키가 눌리면 다른 버튼이 눌릴때까지 쭉~ 그 방향으로 움직여야해서 타이머를 쓰라는데.. 타이머 쓰는방
법을 모르네요...ㅠㅠ
쓸데없는 일부분은 지운 코드입니다:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class SaucerPanel extends JPanel implements ActionListener{
private final static int saucerMoveIncrement = 5;//how far to move the saucer each tick
private final static int initialSaucerXpos = 10;
private final static int initialSaucerYpos = 50;
private String saucerHoveringStatus = "hovering...";
private String saucerLandedStatus = "landed!";
private String saucerStatus = saucerHoveringStatus;
private int saucerXpos = initialSaucerXpos;
private int saucerYpos = initialSaucerYpos;
private boolean hovering = true;// state of saucer
private String direction; //direction of saucer
//GUI components
private JButton upBtn = new JButton("Up");
private JButton downBtn = new JButton("Down");
private JButton leftBtn = new JButton("Left");
private JButton rightBtn = new JButton("Right");
private JButton resetBtn = new JButton("Reset");
// used to generate random dots as "debris" from a crash
private Random r = new java.util.Random();
// Timer object with a delay of 100ms
private Timer timer = new Timer(100,this);
public SaucerPanel(){
setPreferredSize(new Dimension(panelWidth, panelHeight));
setBackground(skyColor);
setOpaque(true);
this.add(upBtn);
this.add(downBtn);
this.add(leftBtn);
this.add(rightBtn);
this.add(resetBtn);
upBtn.addActionListener(this);
downBtn.addActionListener(this);
leftBtn.addActionListener(this);
rightBtn.addActionListener(this);
resetBtn.addActionListener(this);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
drawGround(g);
drawBuildings(g);
if (hovering)
drawSaucer(g, saucerXpos, saucerYpos);
else
drawCrash(g);
}
private void drawSaucer(Graphics g, int xPos, int yPos){
g.setColor(saucerColor);
g.fillOval(xPos, yPos, saucerWidth, saucerHeight);
//15pixels added to yPos+saucerHeight to make status appear under saucer
g.drawString(saucerStatus, xPos, (yPos+saucerHeight+15));
}
//draw a bunch of points to simulate debris
private void drawCrash(Graphics g){
g.setColor(Color.black);
for(int i = 0; i < 400; i++)
ranPoints(10,350,g);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == resetBtn){
saucerXpos = initialSaucerXpos;
saucerYpos = initialSaucerYpos;
}
if (e.getSource() == leftBtn){
saucerXpos -= saucerMoveIncrement;
}
if (e.getSource() == rightBtn){
saucerXpos += saucerMoveIncrement;
}
if (e.getSource() == upBtn){
saucerYpos -= saucerMoveIncrement;
}
if (e.getSource() == downBtn){
saucerYpos += saucerMoveIncrement;
}
if (saucerTouchingBuilding(saucerXpos, saucerYpos)||saucerXpos == 0 || saucerXpos == 410){
hovering = false;
}
if(saucerYpos+saucerHeight == groundYpos){
if(landedInBounds(saucerYpos)){
saucerStatus = saucerLandedStatus;
hovering = true;
}
else hovering = false;
}
repaint();
}
}
다른 부분은 건들이지않고 저 연노란 actionPerformed 부분만 고치면되는데요...
제가 지금 타이머를 쓸 줄 몰라서
지금은 버튼 한번 누르면 그 방향으로 한번만 움직입니다...
타이머를 어떻게 써야 버튼한번눌렀을때 그 방향으로 쭈욱 갈까요?
일단 문제는 이 밑에 프로그램에서 방향버튼을 누르면 저 까만 원반이 그 방향으로 움직여야합니다!
한번 방향키가 눌리면 다른 버튼이 눌릴때까지 쭉~ 그 방향으로 움직여야해서 타이머를 쓰라는데.. 타이머 쓰는방
법을 모르네요...ㅠㅠ
![](https://img-api.cboard.net/img_n.php?image_url=https://kin-phinf.pstatic.net/20151216_134/1450271945153Tyi1f_JPEG/saucer.jpg)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class SaucerPanel extends JPanel implements ActionListener{
private final static int saucerMoveIncrement = 5;//how far to move the saucer each tick
private final static int initialSaucerXpos = 10;
private final static int initialSaucerYpos = 50;
private String saucerHoveringStatus = "hovering...";
private String saucerLandedStatus = "landed!";
private String saucerStatus = saucerHoveringStatus;
private int saucerXpos = initialSaucerXpos;
private int saucerYpos = initialSaucerYpos;
private boolean hovering = true;// state of saucer
private String direction; //direction of saucer
//GUI components
private JButton upBtn = new JButton("Up");
private JButton downBtn = new JButton("Down");
private JButton leftBtn = new JButton("Left");
private JButton rightBtn = new JButton("Right");
private JButton resetBtn = new JButton("Reset");
// used to generate random dots as "debris" from a crash
private Random r = new java.util.Random();
// Timer object with a delay of 100ms
private Timer timer = new Timer(100,this);
public SaucerPanel(){
setPreferredSize(new Dimension(panelWidth, panelHeight));
setBackground(skyColor);
setOpaque(true);
this.add(upBtn);
this.add(downBtn);
this.add(leftBtn);
this.add(rightBtn);
this.add(resetBtn);
upBtn.addActionListener(this);
downBtn.addActionListener(this);
leftBtn.addActionListener(this);
rightBtn.addActionListener(this);
resetBtn.addActionListener(this);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
drawGround(g);
drawBuildings(g);
if (hovering)
drawSaucer(g, saucerXpos, saucerYpos);
else
drawCrash(g);
}
private void drawSaucer(Graphics g, int xPos, int yPos){
g.setColor(saucerColor);
g.fillOval(xPos, yPos, saucerWidth, saucerHeight);
//15pixels added to yPos+saucerHeight to make status appear under saucer
g.drawString(saucerStatus, xPos, (yPos+saucerHeight+15));
}
//draw a bunch of points to simulate debris
private void drawCrash(Graphics g){
g.setColor(Color.black);
for(int i = 0; i < 400; i++)
ranPoints(10,350,g);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == resetBtn){
saucerXpos = initialSaucerXpos;
saucerYpos = initialSaucerYpos;
}
if (e.getSource() == leftBtn){
saucerXpos -= saucerMoveIncrement;
}
if (e.getSource() == rightBtn){
saucerXpos += saucerMoveIncrement;
}
if (e.getSource() == upBtn){
saucerYpos -= saucerMoveIncrement;
}
if (e.getSource() == downBtn){
saucerYpos += saucerMoveIncrement;
}
if (saucerTouchingBuilding(saucerXpos, saucerYpos)||saucerXpos == 0 || saucerXpos == 410){
hovering = false;
}
if(saucerYpos+saucerHeight == groundYpos){
if(landedInBounds(saucerYpos)){
saucerStatus = saucerLandedStatus;
hovering = true;
}
else hovering = false;
}
repaint();
}
}
#java 자바스크립트 실행 #java 자바 #west java 자바 #자바 import java.util.* #자바 java.lang.nullpointerexception #자바 java.lang.arrayindexoutofboundsexception #자바 환경변수 java_home #자바스크립트 java 함수 호출 #자바 java.util.nosuchelementexception #자바 java.util.date