# 2
Ok here is the code :
class Flip
public 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;
}
}
}
class Parois :
public class Parois implements ObstaclesFlipper
{
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);
}
}
and my class extending JPanel
public 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();
}
};
Action actionLeftArrowPressed = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
((Flip)lesObstacles.get(5)).bouge();
System.out.println("left key pressed");
repaint();
}
};
Action actionRightArrowRelease = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
((Flip)lesObstacles.get(6)).release();
System.out.println("right key release");
repaint();
}
};
Action actionLeftArrowRelease = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
((Flip)lesObstacles.get(5)).release();
System.out.println("left key released");
repaint();
}
};
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);
g.setColor(Color.YELLOW);
g.fillRect(LeFlipper.largeurDuFlipper - 50, LeFlipper.hauteurDuFlipper - 90, 30, 30);
g.setColor(Color.RED);
g.fillOval(LeFlipper.largeurDuFlipper - 50, LeFlipper.hauteurDuFlipper - 90, 30, 30);
g.setColor(Color.BLACK);
g.fillOval(0, 80, 30, 30);
g.setColor(Color.RED);
g.drawOval(2, 120, 26, 26);
g.drawOval(0, 118, 30, 30);
g.drawString("100", 2, 140);
g.drawOval(2, 160, 26, 26);
g.drawOval(0, 158, 30, 30);
g.drawString("200", 2, 180);
g.drawOval(0, 200, 30, 30);
g.drawString("100", 2, 220);
g.drawOval(0, 240, 30, 30);
g.drawString("200", 0, 260);
g.setColor(Color.GREEN);
g.fillRect(0, 280, 30, 5);
g.drawLine(0, 283, 30, 285);
g.drawLine(30, 285, 0, 287);
g.drawLine(0, 287, 30, 289);
g.drawLine(30, 289, 0, 291);
paintLesObstacles(lesObstacles, g);
}
private void paintLesObstacles(List lesObstacles, Graphics g)
{
for(int i = 0; i < lesObstacles.size(); i++)
{
((ObstaclesFlipper)lesObstacles.get(i)).dessineToi(g);
}
}
}
The other problem i got is that when i triger those key events its calling repaint so it does repaint everything which is not what i want. I d like to have just the flip repainted. How could i separate it nicely ? Thank you;
# 4
Hello, i still havent found why it doesnt repaint properly so here is SSCCE, well it should be, i cannot make it more simple :
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();// <== does not repaint properly
}
};
Action actionLeftArrowPressed = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
((Flip)lesObstacles.get(5)).bouge();
System.out.println("left key pressed");
repaint();// <== does not repaint properly
}
};
Action actionRightArrowRelease = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
((Flip)lesObstacles.get(6)).release();
System.out.println("right key release");
repaint();// <== does not repaint
}
};
Action actionLeftArrowRelease = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
((Flip)lesObstacles.get(5)).release();
System.out.println("left key released");
repaint();// <== does not repaint
}
};
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;
}
}
}
I'm sorry that its long but the code is not hard to understand.
Message was edited by:
JusteUneQuestion