repaint() JPanel

Hello i have a class extending JPanel. In my panel i draw 2 flips which can move using keystrokes :

Action actionRightArrowPressed =new AbstractAction()

{

publicvoid actionPerformed(ActionEvent e)

{

((Flip)lesObstacles.get(6)).bouge();

System.out.println("right kkey pressed");

repaint();

}

};

Action actionRightArrowRelease =new AbstractAction()

{

publicvoid actionPerformed(ActionEvent e)

{

((Flip)lesObstacles.get(6)).release();

System.out.println("right key release");

repaint();

}

};

The problem is that my flip do not get repainted properly on the JPanel. Only when i resize the window i get it repaint() properly for exemple. Is repaint() the correct method to use ? Where do i have to call it ?

Thank you for your help

[1316 byte] By [JusteUneQuestiona] at [2007-11-27 6:22:57]
# 1

Based on the code provided we have no idea what your program is doing.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 17:40:24 > top of Java-index,Desktop,Core GUI APIs...
# 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;

JusteUneQuestiona at 2007-7-12 17:40:24 > top of Java-index,Desktop,Core GUI APIs...
# 3

A couple of the key words of SSCCE are "Simple and Executable". Yours is neither so I'm not going to spend much time trying to read the code.

The point of creating a SSCCE is to simplify your application. 90% of the time when you do this you will find your mistake.

> I d like to have just the flip repainted. How could i separate it nicely

Well, your Flip should be a component. Then you just add the component to the panel like any other Swing component. Then you it to repaint itself and only it will be repainted.

This posting contains a simple example of a components with custom painting:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=674807

camickra at 2007-7-12 17:40:24 > top of Java-index,Desktop,Core GUI APIs...
# 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

JusteUneQuestiona at 2007-7-12 17:40:24 > top of Java-index,Desktop,Core GUI APIs...