반응형


package 점프;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Jump extends JFrame{
private int height = 570;
private int velocity =0;
private int gravity = 1;
public Jump() {
this.setTitle("Jump High");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300,600);
this.setVisible(true);
JButton button = new JButton("점프");
button.setBackground(Color.blue);
button.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
velocity+=20;
}@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}});
button.setBounds(0,270,60,30);
add(button);
while(true) {
try {Thread.sleep(18);}catch(InterruptedException e) {}
if(height>570 && velocity<0) {
height = 570;
velocity=0;
}
else {
height-=velocity;
velocity-=gravity;
}
repaint();
}
}
public void paint(Graphics g) {
super.paint(g);
g.fillRect(100, height, 30,30);
}
public static void main(String[] args) {
new Jump();
}
}반응형
'java > swing' 카테고리의 다른 글
| java swing 예제- 중력 구현 (0) | 2022.01.08 |
|---|---|
| java swing 예제 - Up Down 게임 (0) | 2022.01.08 |
| java swing 예제 - 키보드로 사각형 움직이기 (0) | 2022.01.08 |
| 명품 JAVA - Open Challenge 12 아바타와 괴물 게임 만들기 - swing,thread (0) | 2022.01.08 |
| 명품 JAVA - Open Challenge 10 버튼 클릭으로 좌우로 이미지 넘기기 - swing (0) | 2022.01.08 |