Comment dessiner une table de jeu en java

mahouch -  
 Utilisateur anonyme -
Bonjour,
je suis entrain d'écrire un code de jeu de billard en utilisant la langage de programmation java,je suis bloqué dans l'etape du dessin de la table verte
pouvez vous m'aidez a résoudre ce probleme
merci d'avance
A voir également:

1 réponse

Utilisateur anonyme
 
Salut,


voici un début. A toi de l'adapter.

PS: j'ai inclus tout dans une seule classe (la vue, le modèle et le contrôle) par facilité pour toi. Mais il vaut mieux séparer la vue du modèle et du contrôleur.

package hacktrack.games;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class BillardDemo extends JFrame {
	private Set<Ball> balls;
	private Dimension billardSize;
	private int ballSize;
	private int billardBorderWidth;
	private Rectangle playZone;
	private JPanel billard;

	public BillardDemo() {
		super("Demo billard");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		init();
	}

	private void init() {
		Container c = getContentPane();
		billardSize = new Dimension(640, 360);
		billardBorderWidth = 20;
		ballSize=20;
		playZone = new Rectangle(billardBorderWidth, billardBorderWidth,
				(int) billardSize.getWidth() - billardBorderWidth * 2,
				(int) billardSize.getHeight() - billardBorderWidth * 2);
		balls = new HashSet<BillardDemo.Ball>();
		balls.add(createBall(Color.RED));
		balls.add(createBall(Color.WHITE));
		balls.add(createBall(Color.WHITE));

		billard = new JPanel() {
			@Override
			public void paint(Graphics g) {
				g.setColor(new Color(128, 64, 0));
				g.fillRect(0, 0, (int) billardSize.getWidth(),
						(int) billardSize.getHeight());
				g.setColor(new Color(0, 128, 0));
				g.fillRect((int) playZone.getX(), (int) playZone.getY(),
						(int) playZone.getWidth(), (int) playZone.getHeight());
				if (balls != null) {
					for (Ball ball : balls) {
						g.setColor(new Color(0,83,0));
						g.fillOval(ball.getX()+2, ball.getY()+2, ballSize, ballSize);
						g.setColor(ball.getColor());
						g.fillOval(ball.getX(), ball.getY(), ballSize, ballSize);
					}
				}
			}

		};

		billard.setPreferredSize(billardSize);
		c.add(billard);
		new MoverThread().start();
	}

	private Ball createBall(Color color) {
		int x = (int) (playZone.getX()+ballSize + Math.random() * playZone.getWidth()-ballSize);
		int y = (int) (playZone.getY()+ballSize  + Math.random() * playZone.getHeight()-ballSize);
		double direction = Math.random() * Math.PI * 2;
		double speed = Math.random() * 2 + 2;
		return new Ball(color, x, y, direction, speed);
	}

	class Ball {
		private Color color;
		private int x;
		private int y;
		private double direction;
		private double speed;

		public Ball(Color color, int x, int y, double direction, double speed) {
			this.color = color;
			this.x = x;
			this.y = y;
			this.direction = direction;
			this.speed = speed;
		}

		public Color getColor() {
			return color;
		}

		public void setX(int x) {
			this.x = x;
		}

		public void setY(int y) {
			this.y = y;
		}

		public int getX() {
			return x;
		}

		public int getY() {
			return y;
		}

		public double getDirection() {
			return direction;
		}

		public double getSpeed() {
			return speed;
		}

	}

	class MoverThread extends Thread {
		private boolean isRunning;

		public MoverThread() {
			isRunning = false;
		}

		@Override
		public void run() {
			isRunning = true;
			while (isRunning) {
				moveBalls();
				billard.repaint();
				try {
					Thread.sleep(20);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

		private void moveBalls() {
			for (Ball ball : balls) {
				int newX = (int) (ball.getX() + Math.cos(ball.getDirection())
						* ball.getSpeed());
				int newY = (int) (ball.getY() + Math.sin(ball.getDirection())
						* ball.getSpeed());

				if (playZone.contains(newX, newY) &&playZone.contains(newX+ballSize, newY+ballSize)) {// && playZone.contains(newX-ballSize, newY-ballSize)
					ball.setX((int) newX);
					ball.setY((int) newY);
				}else{
					//Changer la direction de la balle en focntion du bord qu'elle a touché.
					//Un bel exercice que je te laisse faire... ;-) Hacktrack
				}
			}

		}
	}

	public static void main(String[] args) {
		BillardDemo demo = new BillardDemo();
		demo.pack();
		demo.setVisible(true);
	}
}


;-)
HackTrack
0