JPanel shrinking-expanding problem
BACKGROUND
OK, in this program i made a window. In that window is a panel and the panel has a label in it. The panel is supposed to be 250 long by 250 wide in the window which is 500 long and 500 wide.
I have given the panel two animations:
1. shrinkToSide = the panel shrinks to a width of one.
2. expandToCorner = panel expands to its original width/
A button on the window toggles between shrinking and expanding.
PROBLEMS
1. I only see that part of the panel which holds the label - not the rest
2. The panel is supposed to be at x=0 and y =0 but it's at the center of the window.
3. Clicking on the button shrinks the panel (i.e. the whole panel suddenly appears, shrinks, then disappears) but clicking the button again does not expand it. I don't understand why!
QUESTION
After fixing the above problems in the program, what i also want to do is to make the background of the panel fade from white to black. I can do the fade but I don't know what starting colors and ending colors i should use.
CODE
FlashCard.java
Code:
[B]public class FlashCard extends JPanel implements ActionListene[/B]r {
private Color backgroundColor = Color.WHITE;
private final int ANIMATION_TICKER = 15;
private final int WIDTH;
private int width;
private int height;
private int operationType;
public JLabel flashCardLabel;
Timer timer = new Timer(ANIMATION_TICKER,this);
[B]public void shrinkToSide()[/B]
{
this.operationType=1;
timer.start();
}
[B]public void expandToCorner()[/B]
{
this.operationType = 2;
timer.start();
}
[B]public void setCardText(String text)[/B]
{
flashCardLabel.setText(text);
}
[B]public FlashCard(int x,int y,int width,int height)[/B]
{
this.width = width;
this.WIDTH = width;
this.height = height;
setBounds(x, y, this.width, this.height);
setBackground(this.backgroundColor);
flashCardLabel = new JLabel();
add(flashCardLabel);
}
[B]public void actionPerformed(ActionEvent e)[/B]
{
switch(this.operationType)
{
case 1:
{
if(this.width>1)
{
this.width -= 5;
setSize(this.width, this.height);
}else{
timer.stop();
}
break;
}
case 2:
{
if(this.width!=this.WIDTH)
{
this.width+=5;
setSize(this.width,this.height);
}else{
timer.stop();
}
break;
}
default:
{
JOptionPane.showMessageDialog(null, "Animaiton Failed.");
}
}
}
}
FlashCard_Frame.java
Code:
[B]public class FlashCard_Frame extends JFrame[/B] {
private JLabel label;
private FlashCard flashcard;
private JButton button;
private int actionType=1;
[B]public FlashCard_Frame[/B]()
{
super("FlashCard Application");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
label = new JLabel("Hi!");
add(label);
flashcard = new FlashCard(0,0,250,250);
flashcard.setCardText("Howdy");
add(flashcard);
button = new JButton ("Hide Panel");
button.addActionListener(
new ActionListener(){
[B]public void actionPerformed(ActionEvent arg0)[/B] {
if(actionType%2 != 0)flashcard.shrinkToSide();
else flashcard.expandToCorner();
}
}
);
add(button);
}
[B][COLOR="Red"]public static void main(String args[])[/COLOR][/B]
{
FlashCard_Frame frame = new FlashCard_Frame();
//
frame.setVisible(true);
}
}