Bonjour,
J'ai un programme tout bête qui m'affiche une image en fond d'un Jframe, et un rectangle qui sert de curseur. J'arrive à le déplacer par clavier... cependant....
quelle que soit les dimensions de mon Jframe, le rectangle devient "invisible" au delà d'une certaine distance du point 0;0... (Je dirais à la moitié de l'axe des x et aux 2/3 de l'axe des y).
Pour être plus précis, j'ai println() les coordonés du rectangle et il continue en fait sa course même si je ne le vois plus.
Voici le code :
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 package jeux2; import javax.swing.JFrame; import java.awt.Dimension; public class Main { public static Fond fond = new Fond(); public static void main(String[] args){ JFrame fenetre = new JFrame("Test"); fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fenetre.setSize(new Dimension(1920, 1200)); fenetre.setLocationRelativeTo(null); fenetre.setResizable(false); fenetre.setAlwaysOnTop(false); fenetre.setUndecorated(true); fenetre.setContentPane(fond); fenetre.setVisible(true); } }
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 package jeux2; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class clavier implements KeyListener{ //@Override public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_Z){ Main.fond.setDy(-5); } if(e.getKeyCode() == KeyEvent.VK_S){ Main.fond.setDy(5); } if(e.getKeyCode() == KeyEvent.VK_Q){ Main.fond.setDx(-5); } if(e.getKeyCode() == KeyEvent.VK_D){ Main.fond.setDx(5); } } @Override public void keyReleased(KeyEvent arg0) { Main.fond.setDx(0); Main.fond.setDy(0); } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }
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
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
87
88
89
90
91
92
93 package jeux2; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JPanel; public class Fond extends JPanel { private static final long serialVersionUID = 2290215993065771323L; public int x ; public int y ; public int dx ; public int dy ; public ImageIcon icoFond; public Image fond; public Fond(){ super(); icoFond = new ImageIcon(getClass().getResource("img/earth_map_ocean_continents_1920x1200.jpg")); this.fond = this.icoFond.getImage(); this.setFocusable(true); this.addKeyListener(new clavier()); Thread chrono = new Thread(new Chrono()); chrono.start(); } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics g2 = (Graphics2D)g; g2.drawImage(this.fond,0,0,null); g2.setColor(Color.RED); g2.drawRect(this.getX(),this.getY(),32,32); System.out.println(this.getX()); this.deplacement(); } public void deplacement(){ this.x = this.x + this.dx ; this.y = this.y + this.dy; } // Getters et Setters public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getDx() { return dx; } public void setDx(int dx) { this.dx = dx; } public int getDy() { return dy; } public void setDy(int dy) { this.dy = dy; } public Image getFond() { return fond; } public void setFond(Image fond) { this.fond = fond; } public static long getSerialversionuid() { return serialVersionUID; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package jeux2; public class Chrono implements Runnable{ private int PAUSE = 3; @Override public void run() { while(true){ Main.fond.repaint(); try { Thread.sleep(PAUSE); } catch (InterruptedException e) {} } } }
Partager