Bonjour à tous j'ai actuellement un gros soucis, je souhaite faire déplacer une boule a chaque fois que l'on appuie sur la touche droite du clavier, tout marche sauf le déplacement ... Il me redessine en faite juste à côté la nouvelle balles, ce qui me donne une ligne rouge au finale ...
Voici le code de mon application :
UIDebug.java :
Jbackground.java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public void tracingWindows() { setTitle("Batak"); this.width = (int)this.getToolkit().getScreenSize().getWidth() / 2; this.height = (int)this.getToolkit().getScreenSize().getHeight() / 2; this.setSize(width, height); Jbackground Jback = new Jbackground(); Jback.setPreferredSize(new Dimension(width, height)); Jback.setBackground(Color.WHITE); this.setDefaultCloseOperation(EXIT_ON_CLOSE); IComponent c = new PlayerFrame(new Point(50, height - 100), new Dimension(50, 50), Jback); Jback.drawComponentList(c); this.setContentPane(Jback); this.addKeyListener(new keyboardListener()); pack(); setVisible(true); }
Icomponent.java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 public class Jbackground extends JPanel{ private List<IComponent> components = new LinkedList<IComponent>(); public static PlayerFrame pFrame; //Un seul personnage dans le jeux public void drawComponentList(IComponent c) { components.add(c); } public void paintComponent(Graphics g) { super.paintComponents(g); for (Iterator<IComponent> i = components.iterator(); i.hasNext();) { IComponent c = (IComponent) i.next(); c.draw(g); } } }
PlayerFrame.java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 public interface IComponent { public void draw(Graphics g); }
et keyboardListener.java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class PlayerFrame implements IComponent{ private Point point; private Dimension dim; private Jbackground jpanelCurrent; public PlayerFrame(Point aPoint, Dimension aDim, Jbackground aJpanelCurrent) { point = aPoint; dim = aDim; Jbackground.pFrame = this; jpanelCurrent = aJpanelCurrent; } public void draw(Graphics g) { g.setColor(Color.RED); g.fillOval(point.x, point.y, dim.width, dim.height); } public void move(Point newPoint) { point = newPoint; } public Point getPosition() { return point; } public Jbackground getJpanelCurrent() { return jpanelCurrent; } }
Voilà en espérant que vous puissiez m'aider
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class keyboardListener implements KeyListener{ public void keyPressed(KeyEvent arg0) { parseKeyCode(arg0.getKeyCode()); } public void keyReleased(KeyEvent arg0) { } public void keyTyped(KeyEvent arg0) { } private void parseKeyCode(int code) { switch(code) { case 39: startingThreedMove('r'); break; case 37: startingThreedMove('l'); break; } } private void startingThreedMove(final char direction) { Thread t = new Thread() { public void run() { Point ac = Jbackground.pFrame.getPosition(); Point newPoint = new Point(ac.x + 5, ac.y); Jbackground.pFrame.move(newPoint); Jbackground.pFrame.getJpanelCurrent().repaint(); this.interrupt(); } }; t.start(); } }merci
Partager