Hi guys,
Ok so I have to do a Pong type game for school.
So far, I am doing it with a mousemotion listener. But the thing is, I have 2 classes, Raquette and Ball.
the Raquette class contains the mousemotion listener for the paddle. The Ball gets refreshed using a thread.
So I made a isHiT() method in my Ball class to see if the raquette and teh Ball comes in contact.
But the problem is that the getY() keeps returning 0 even tough I am refresshing it....
Here is the code:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * Travail fait par Aravinthan Sivaneswaran * Date: 17-Nov-2011 * Nom du projet: */ import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.io.*; import java.awt.event.MouseMotionListener; import java.awt.event.MouseEvent; //classe démarrage public class main { public static void main(String[] args) { JFrame frame = new BounceThreadFrame(); frame.setVisible(true); } } //classe de la fenêtre graphique class BounceThreadFrame extends JFrame { private JPanel canvas; int x = 0; private int addY = 0; public BounceThreadFrame() { setSize(400, 400); setResizable(false); setLocationRelativeTo(null); //laisse l'OS placer la fenêtre setTitle("Bounce"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); canvas = new JPanel(); getContentPane().add(canvas, "Center"); JButton b = new JButton("Lancer balle"); getContentPane().add(b, "North"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if (x == 0) { Ball b = new Ball(canvas); Raquette r = new Raquette(canvas); b.start(); //pour exécuter la méthode run() du thread r.draw(); r.start(); } x++; } }); } } //classe Balle hérite de Thread class Ball extends Thread { private JPanel box; //panneau supportant la balle private static final int XSIZE = 20, YSIZE = 20;//dimensions balle private int x = 0, y = 0; //position initiale balle private int dx = 4; //déplacement balle (horizontal) private int dy = 4; public boolean notout = true; Raquette r = new Raquette(box); public Ball(JPanel b) { box = b; } // exécution du thread (déclenché par start) public void run() { try { while (notout) { sleep(20); //attendre 5 ms move(); //déplacer balle } } catch (InterruptedException e) { } } //méthode déplacer balle public void move() { Graphics g = box.getGraphics(); Dimension d = box.getSize(); g.setXORMode(box.getBackground());//pour que les pixels retrouvent leur couleur d'origine lors du 2ème dessin (qui suit) g.fillOval(x, y, XSIZE, YSIZE); // on dessine la balle x += dx; //on augmente l'abcisse pour déplace la balle horizontalement y += dy; if (x < 1) { dx = -dx; } if (y < 1) { dy = -dy; } if ((x + XSIZE) > d.width) { //.showMessageDialog(null, "La balle est sorti. Vous perdez 1 vie", "Vie perdu", JOptionPane.INFORMATION_MESSAGE); //notout = false; dx = -dx; } if ((y + YSIZE) > d.height) { dy = -dy; } //System.out.println(isHIT()); if(isHIT()){ dx = -dx; dy = -dy; } g.fillOval(x, y, XSIZE, YSIZE); //on redessine } public boolean isHIT() { int raquettey; raquettey = r.getY(); System.out.println(raquettey); if((y>raquettey && y<(raquettey+r.getYSIZE())) && (x>=box.getWidth()-r.getXSIZE())) return true; else return false; } } //fin classe balle //classe Balle hérite de Thread class Raquette extends Thread implements MouseMotionListener { private JPanel box; //panneau supportant la balle private static final int XSIZE = 10, YSIZE = 50;//dimensions balle private int x = 385, y = 0; //position initiale balle private int dx = 2; //déplacement balle (horizontal) private int dy = 2; private int addY; public Raquette(JPanel r) { box = r; } // exécution du thread (déclenché par start) public void run() { try { while (true) { sleep(10); //attendre 5 ms move(); //déplacer balle } } catch (InterruptedException e) { } } //méthode déplacer balle public void draw() { Graphics g = box.getGraphics(); Dimension d = box.getSize(); g.setXORMode(box.getBackground());//pour que les pixels retrouvent leur couleur d'origine lors du 2ème dessin (qui suit) g.fillRect(x, y, XSIZE, YSIZE); // on dessine la balle } //méthode déplacer balle public void move() { box.addMouseMotionListener(this); Graphics g = box.getGraphics(); Dimension d = box.getSize(); g.setXORMode(box.getBackground());//pour que les pixels retrouvent leur couleur d'origine lors du 2ème dessin (qui suit) g.fillRect(x, y, XSIZE, YSIZE); // on dessine la balle y = addY; if (y <= 0) { y = 0; } if ((y + (YSIZE)) > d.height) { y = d.height - YSIZE; } setY(y); g.fillRect(x, y, XSIZE, YSIZE); //on redessine } void eventOutput(String eventDescription, MouseEvent e) { addY = (e.getY()); } public void mouseMoved(MouseEvent e) { eventOutput("Mouse moved", e); } public void mouseDragged(MouseEvent e) { eventOutput("Mouse dragged", e); } public void setY(int y){ this.y = y; } public int getY(){ return this.y; } public int getXSIZE(){ return this.XSIZE; } public int getYSIZE(){ return this.YSIZE; } } //fin classe raquette