Bonjour à tous, j'essaie de reproduire un flipper, mais j'ai un problème lorsque je bouge l'un des flips qui n'est pas repaint correctement donc donne l'impression de ne pas bouger, voici le code si qqun a le courage :
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 
public Flipper()
    {
        Runnable runner = new Runnable()
        {
            public void run()
            {
                backgroundPanel = new BackgroundPanel();
                setLayout(new BorderLayout());
                getContentPane().add(backgroundPanel, BorderLayout.CENTER);
                addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        System.exit(0);
                    }
                });
                setSize(400, 400);
                setVisible(true);
            }
        };
        SwingUtilities.invokeLater(runner);
    }
 
 public static void main(String[] args)
    {
        Flipper frame = new Flipper();
    }
}
 
class BackgroundPanel extends JPanel
{
    private List lesObstacles;
 
    /** Creates a new instance of BackgroundPanel */
    public BackgroundPanel()
    {
        lesObstacles = new ArrayList();
        //Parois gauche
        lesObstacles.add(new Parois(30, 0, 2, 340));
        //Parois haut
        lesObstacles.add(new Parois(30, 0, 350, 2));
        //Parois droite
        lesObstacles.add(new Parois(380, 0, 2, 340));
        //Parois bas gauche
        lesObstacles.add(new Parois(30, 340, 80, 2));
        //Parois bas droite
        lesObstacles.add(new Parois(275, 340, 105, 2));
        lesObstacles.add(new Flip(110, 340, 60, 4, 110, 280));
        lesObstacles.add(new Flip(250, 340, 60, 4, 310, 280));
 
 
        Action actionRightArrowPressed = new AbstractAction()
        {
            public void actionPerformed(ActionEvent e)
            {
                ((Flip)lesObstacles.get(6)).bouge();
                System.out.println("right kkey pressed");
                repaint();// <== ne repaint pas correctement
            }
        };
 
        Action actionLeftArrowPressed = new AbstractAction()
        {
            public void actionPerformed(ActionEvent e)
            {
                ((Flip)lesObstacles.get(5)).bouge();
                System.out.println("left key pressed");
                repaint();// <== ne repaint pas correctement
            }
        };
 
        Action actionRightArrowRelease = new AbstractAction()
        {
            public void actionPerformed(ActionEvent e)
            {
                ((Flip)lesObstacles.get(6)).release();
                System.out.println("right key release");
                repaint();// <== ne repaint pas correctement
            }
        };
 
        Action actionLeftArrowRelease = new AbstractAction()
        {
            public void actionPerformed(ActionEvent e)
            {
                ((Flip)lesObstacles.get(5)).release();
                System.out.println("left key released");
                repaint();// <== ne repaint pas correctement
            }
        };
 
        ActionMap actionMap = this.getActionMap();
        actionMap.put("right arrow pressed", actionRightArrowPressed);
        actionMap.put("left arrow pressed", actionLeftArrowPressed);
        actionMap.put("right arrow released", actionRightArrowRelease);
        actionMap.put("left arrow released", actionLeftArrowRelease);
        this.setActionMap(actionMap);
 
        KeyStroke rightArrowPressed = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false);
        InputMap inputMap = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(rightArrowPressed, "right arrow pressed");
 
        KeyStroke leftArrowPressed = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false);
        inputMap.put(leftArrowPressed, "left arrow pressed");
 
        KeyStroke rightArrowReleased = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true);
        inputMap.put(rightArrowReleased, "right arrow released");
 
        KeyStroke leftArrowReleased = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true);
        inputMap.put(leftArrowReleased, "left arrow released");
    }
 
    public void paintComponent(Graphics g)
    {
        ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        paintLesObstacles(lesObstacles, g);
    }
 
    private void paintLesObstacles(List lesObstacles, Graphics g)
    {
        for(int i = 0; i < lesObstacles.size(); i++)
        {
            ((ObstaclesFlipper)lesObstacles.get(i)).dessineToi(g);
        }
    }
}
 
class Parois
{
    protected Rectangle region;
    private Color maCouleur;
    /** Creates a new instance of Parois */
    public Parois(int x, int y, int width, int height)
    {
        region = new Rectangle(x, y, width, height);
        maCouleur = Color.YELLOW;
    }
 
    public void setMaCouleur(Color maCouleur)
    {
        this.maCouleur = maCouleur;
    }
 
    public void dessineToi(Graphics g)
    {
        g.setColor(maCouleur);
        g.fillRect(region.x, region.y, region.width, region.height);
    }
}
 
class Flip extends Parois
{
    private int fx, fy;
    private boolean pressed;
    /** Creates a new instance of Flip */
    public Flip(int x, int y, int width, int height, int fx, int fy)
    {
        super(x, y, width, height);
        setMaCouleur(Color.BLACK);
        this.fx = fx;
        this.fy = fy;
        pressed = false;
    }
 
    public void bouge()
    {
        if(!pressed)
        {
            int a, b;
            a = fx;
            b = fy;
            fx = region.x;
            fy = region.y;
            region.x = a;
            region.y = b;
            a = region.width;
            b = region.height;
            region.width = b;
            region.height = a;
            pressed = true;
        }
    }
 
    public void release()
    {
        if(pressed)
        {
            int a, b;
            a = fx;
            b = fy;
            fx = region.x;
            fy = region.y;
            region.x = a;
            region.y = b;
            a = region.width;
            b = region.height;
            region.width = b;
            region.height = a;
            pressed = false;
        }
    }
}
Le problème provient de repaint() dans la BackgroundPanel dans mes actions. Désolé pour la longueur du code, mais il n'est pas difficile à comprendre je l'ai un peu adapté afin de le rendre le plus compact possible tout en illustrant mon pb. Normalement il devrait s'exécuter sans modification.
Merci.