본문 바로가기

Java프로그래밍/예제문제

혼자 탁구 소스코드

반응형
SMALL
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package univ.inu.embedded;

public class PoolballThread extends Thread 
{
	private PoolballPanel pp = null;
	private boolean stopped = false;

	public PoolballThread(PoolballPanel _pp)
	{
		pp = _pp;
	}
	
	public void stopUpdate()
	{
		stopped = true;
	}
	
	public void restartUpdate()
	{
		stopped = false;
	}
	
	public void stopAndGo()
	{
		if (stopped == false)
		{
			this.stopUpdate();
		}
		else
		{
			this.restartUpdate();
			synchronized(pp)
			{
				pp.notifyAll();
			}
		}
	}
	
	public void run()
	{
		while (true)
		{
			while (stopped == false)
			{
				pp.repaint();
				try {
					Thread.sleep(25);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			synchronized(pp)
			{	
				try {
					pp.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
	}
	
	
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package univ.inu.embedded;

import javax.swing.JFrame;

public class GameFrame extends JFrame 
{
	public GameFrame()
	{
		this.setSize(PoolballPanel.WIDTH, PoolballPanel.HEIGHT);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		PoolballPanel pp = new PoolballPanel();
		this.add(pp);
		this.setVisible(true);
	}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package univ.inu.embedded;

public class Test 
{

	public static void main(String[] args) 
	{
		GameFrame gf = new GameFrame();
	}

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package univ.inu.embedded;

import java.awt.*;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

public class Racquet 
{
	int x;
	int y;
	int xspeed = 0;
	Color color;
	static int WIDTH = 300;
	static int HEIGHT = 20;

	public Rectangle getDimension()
	{
		return new Rectangle(x,y-3,WIDTH, 3);
	}
	
	public void resetSpeed(KeyEvent e)
	{
		if (e.getKeyCode() == KeyEvent.VK_LEFT ||
			e.getKeyCode() == KeyEvent.VK_RIGHT)
		{	
			xspeed = 0;
		}
	}
	public void setSpeed(KeyEvent e)
	{
		if (e.getKeyCode() == KeyEvent.VK_LEFT)
		{
			xspeed = -10;
		}
		else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			xspeed = 10;
		}
	}
	
	public void move()
	{
		if ((x+xspeed) >= 0 && 
			(x+xspeed) < (PoolballPanel.WIDTH - WIDTH))
		{
			x = x + xspeed;
		}
	}
	
	public void draw(Graphics g)
	{
		g.setColor(this.color);
		g.fillRect(x, y, WIDTH, HEIGHT);
	}
	
	public Racquet()
	{
		
	}
	
	public Racquet(int _x, int _y, Color _color)
	{
		x = _x;
		y = _y;
		color = _color;
	}
	
	
	
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package univ.inu.embedded;

public class PoolballThread extends Thread 
{
	private PoolballPanel pp = null;
	private boolean stopped = false;

	public PoolballThread(PoolballPanel _pp)
	{
		pp = _pp;
	}
	
	public void stopUpdate()
	{
		stopped = true;
	}
	
	public void restartUpdate()
	{
		stopped = false;
	}
	
	public void stopAndGo()
	{
		if (stopped == false)
		{
			this.stopUpdate();
		}
		else
		{
			this.restartUpdate();
			synchronized(pp)
			{
				pp.notifyAll();
			}
		}
	}
	
	public void run()
	{
		while (true)
		{
			while (stopped == false)
			{
				pp.repaint();
				try {
					Thread.sleep(25);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			synchronized(pp)
			{	
				try {
					pp.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
	}
	
	
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package univ.inu.embedded;

import java.awt.*;

public class Ball 
{
	private boolean noMoreUpdate = false;
	private boolean visible = true;
	private int x = 100;
	private int y = 100;
	private int size = 30;
	private int xSpeed = 5;
	private int ySpeed = 5;
	private Color myColor = Color.YELLOW;
	private Racquet racquet = null;
	
	public void setVisible(boolean _visible)
	{
		visible = _visible;
	}
	
	public boolean getVisible()
	{
		return visible;
	}
	
	public Ball()
	{
		
	}
	public Ball(int _x, int _y, int _size, 
			int _xSpeed, int _ySpeed, 
			Color _myColor, Racquet _racquet)
	{
		x = _x;
		y = _y;
		size = _size;
		xSpeed = _xSpeed;
		ySpeed = _ySpeed;
		myColor = _myColor;
		racquet = _racquet;
	}
	
	public void update()
	{
		if (noMoreUpdate == true)
		{
			return;
		}
		x = x + xSpeed;
		if (x > PoolballPanel.WIDTH || x < 0)
		{
			xSpeed = -xSpeed;
			//setVisible(false);
		}
		
		y = y + ySpeed;
		
		if (y > PoolballPanel.HEIGHT)
		{
			noMoreUpdate = true;
			visible = false;
		}
		else if (y < 0)
		{
			ySpeed = -ySpeed;
			//setVisible(false);
		}
		
		Rectangle myDimension = new Rectangle(x,y,size,size);
		if (myDimension.intersects(racquet.getDimension()) == true)
		{
			ySpeed = -ySpeed;
		}
		
	}
	
	public void draw (Graphics g)
	{
		if (getVisible() == true)
		{
			g.setColor(myColor);
			g.fillOval(x, y, size, size);
		}
	}
}


반응형
LIST

'Java프로그래밍 > 예제문제' 카테고리의 다른 글

network pingpong client  (0) 2016.05.17
네트워킹 예제  (0) 2016.05.13
당구프로그램 예제  (0) 2016.04.15
Timer : TmerLogic and Test program  (0) 2016.04.08
Timer Source Code: TimerUI  (0) 2016.04.08