Immage needs help

S

shoa

Hello all

In the panel I want to display an image on it in one state and no image in
another state:

class MyPanel extends JPanel {
Image logo;
public MyPanel (){

logo = Toolkit.getDefaultToolkit().getImage("logo.gif");
}

public void paint(Graphics g){
if (getCurrentState() == -1) //state one , display image
g.drawImage(logo,0,0,this.getWidth(), this.getHeight(),
this);
else if (getCurrentState()==0) //state two, display this
panel with cyan color
{g.setColor(Color.cyan);
g.drawRect(0,0, this.getWidth(), this.getHeight());
}

}
}

Then I inset this panel to a frame and have a button. From this button, I
set the currentState to -1 and 0 and call the function myPanel.repaint();

However, myPanel does not change when I press the button. The myPanel only
changes the Image when I hide and show again the frame (by clicking minimum
and maximum button).
Could you please help
thank you
S.Hoa
 
I

Ike

you need to use:

public void paintComponent(Graphics g){
super.paintComponent(g);
.. . .//your code from paint() here
} //-Ike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top