본문 바로가기

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
package univ.inu.embedded;

import java.awt.*;

public class Ball 
{
	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;
	
	public Ball()
	{
		
	}
	public Ball(int _x, int _y, int _size, 
			int _xSpeed, int _ySpeed, Color _myColor)
	{
		x = _x;
		y = _y;
		size = _size;
		xSpeed = _xSpeed;
		ySpeed = _ySpeed;
		myColor = _myColor;
	}
	
	public void update()
	{
		x = x + xSpeed;
		if (x > PoolballPanel.WIDTH || x < 0)
		{
			xSpeed = -xSpeed;
		}
		
		y = y + ySpeed;
		if (y > PoolballPanel.HEIGHT || y < 0)
		{
			ySpeed = -ySpeed;
		}
	}
	
	public void draw (Graphics g)
	{
		g.setColor(myColor);
		g.fillOval(x, y, size, size);
	}
	
	
}
 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
package univ.inu.embedded;

import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class PoolballPanel extends JPanel 
{

	public static int WIDTH = 600;
	public static int HEIGHT = 400;
	private Ball[] ball = null;

	
	public PoolballPanel()
	{
		ball = new Ball[10];
		Random r = new Random();
		for (int i = 0; i < 10; i++)
		{
			ball[i] = new Ball(r.nextInt(WIDTH),
					r.nextInt(HEIGHT),
					30,
					5*(r.nextBoolean() ? 1 : -1), 
					5*(r.nextBoolean() ? 1: -1), 
					Color.yellow);
		}
		
		this.setBackground(Color.RED);
		PoolballThread pt = new PoolballThread(this);
		pt.start();
		
	}
	
	@Override
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		for (int i = 0; i < 10; i++)
		{
			ball[i].update();
		}
		for (int i = 0; i < 10; i++)
		{
			ball[i].draw(g);
		}
	}
	
}
 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
package univ.inu.embedded;

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

	public PoolballThread(PoolballPanel _pp)
	{
		pp = _pp;
	}
	
	public void run()
	{
		
		while (true)
		{
			pp.repaint();
			try {
				Thread.sleep(50);
			} 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);
	}
}
package univ.inu.embedded; public class Test { public static void main(String[] args) { GameFrame gf = new GameFrame(); } }
반응형
LIST

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

네트워킹 예제  (0) 2016.05.13
혼자 탁구 소스코드  (0) 2016.04.29
Timer : TmerLogic and Test program  (0) 2016.04.08
Timer Source Code: TimerUI  (0) 2016.04.08
계산기 만들기 숙제  (0) 2016.03.25