Bonjour,
je souhaite créer une application dans laquelle on peut éditer nos propres map de plateforme. Pour tout ce qui est interface j'utilise donc jpanel tout marche bien, mais je veux afficher une image qu'elle suive la sourie et que lorsque que l'on clique elle se place sur la bonne case, un éditeur quoi, niveau code pas de problème j'ai déjà fait ça auparavant mais sans interface.
Lors de chaque repaint() mon image clignote comme si ça affiché l'image puis ré affiché les panels ... mon code :
Main :
Fenetre :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Editeur e = new Editeur(); } }
L'image :
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.util.ArrayList; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.ScrollPaneConstants; import javax.swing.Timer; public class Editeur extends JFrame implements ActionListener, MouseMotionListener{ int x,y; Herbe herbe; boolean h = false; Timer t; int width = 1300; int height = 1000; int cube = 64; int cubeW = 15; int cubeH = 12; int boutonH = 50; Dimension boutonD = new Dimension(200, boutonH); JPanel j = new JPanel(); JPanel k = new JPanel(); //option JPanel l = new JPanel(); //gamepanel JPanel l1 = new JPanel(); JPanel m = new JPanel(); //selecteur JPanel m1 = new JPanel(); JScrollPane scroll = new JScrollPane(m1); //contient panel m1 JButton dirt = new JButton("terre"); JButton eau = new JButton("eau"); JButton brique = new JButton("brique"); JButton caisse = new JButton("caisse"); JButton zombie = new JButton("zombie"); JButton ile = new JButton("ile"); JButton passerelle = new JButton("passerelle"); public Editeur(){ this.setTitle("Editeur"); this.setSize(width,height); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); init(); t = new Timer(25, this); t.start(); addMouseMotionListener(this); setFocusable(true); } public void init(){ //initialisation this.setContentPane(j); j.setLayout(new BorderLayout()); k.setLayout(new BorderLayout()); l.setLayout(new FlowLayout()); l1.setLayout(new FlowLayout()); m.setLayout(new FlowLayout()); m1.setLayout(new FlowLayout()); //modification j.setBackground(Color.black); k.setBackground(Color.black); l.setBackground(Color.black); m.setBackground(Color.black); m1.setBackground(Color.black); //l1.setBackground(Color.white); k.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.white),"Option")); m1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.white),"Bloc")); k.setPreferredSize(new Dimension(width,150)); l1.setPreferredSize(new Dimension(cube * cubeW , cube * cubeH)); m1.setPreferredSize(new Dimension(width - (cube * cubeW) - 20 , cube * cubeH)); dirt.setPreferredSize(boutonD); eau.setPreferredSize(boutonD); zombie.setPreferredSize(boutonD); brique.setPreferredSize(boutonD); passerelle.setPreferredSize(boutonD); ile.setPreferredSize(boutonD); caisse.setPreferredSize(boutonD); dirt.addActionListener(this); //ajout m1.add(dirt); m1.add(eau); m1.add(ile); m1.add(caisse); m1.add(zombie); m1.add(brique); m1.add(passerelle); //mise en page j.add(k, BorderLayout.NORTH); j.add(l, BorderLayout.WEST); l.add(l1, FlowLayout.LEFT); j.add(m, BorderLayout.EAST); m.add(scroll, FlowLayout.LEFT); } public void paint(Graphics g){ super.paint(g); if(h){ herbe.paint(g); } } public void actionPerformed(ActionEvent e) { if(e.getSource() == dirt){ herbe = new Herbe(50,500); System.out.println("terre"); h = true; } if(e.getSource() == t){ repaint(); } } public void mouseDragged(MouseEvent arg0) { } public void mouseMoved(MouseEvent arg0) { x = arg0.getX() - (cube/2); y = arg0.getY() - (cube/2); } }
Je veux donc simplement éviter le clignotement qui est du je pense à cause de Jpanel.
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 import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import javax.swing.ImageIcon; import javax.swing.JPanel; public class Herbe extends JPanel { int x,y ; Rectangle box ; ImageIcon iherbe = new ImageIcon("image/herbe.png") ; Image herbe = iherbe.getImage() ; public Herbe(int sx, int sy) { x = sx ; y = sy ; } public void paint(Graphics g){ super.paint(g); Graphics2D g2d = (Graphics2D) g ; g2d.drawImage(herbe, x, y, null) ; } public Rectangle getBox(){ box = new Rectangle(x,y,64,64) ; return box ; } }
Ps: Je suis débutant en interface graphique il se peut que mon code ne soit pas très très propre ..
Merci d'avance !
Partager