Animation in Java on top of JPanel

68 views Asked by At

I have a JPanel where I have array of buttons. It's a kind of memory game, and I wont to show some few frames of animation, for example Double Point.

Here is a part of code: This animation is that some text is slowly showing and disappearing (I used also Alpha channel in those images) , but I don't know why when id should slowly disapearing it didn't like if of this image stay there.

public void paintComponent(Graphics g)
{

    //Image img = new ImageIcon("res\\double.png").getImage();

    //g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
    t.start();
    Image img2 = new ImageIcon("res\\double\\double_0000"+i+".png").getImage();

    g.drawImage(img2, 0, 0, this.getWidth(), this.getHeight(), null);
}


@Override
public void actionPerformed(ActionEvent e) {

    i++;
    if(i>30)
    t.restart();

    repaint();
}

}

1

There are 1 answers

0
stanisz93 On
public class ScoreWindow extends JPanel implements ActionListener{


private static final long serialVersionUID = 1L;
final public JButton btnScoreReturnButton;
final public JLabel ScoreLabel;
//final public JScrollBar scrollScoreBar;
private String[] data;
Timer t;
String sth="";
int i=0;
int datapower=0;
public ScoreWindow() {
    btnScoreReturnButton = new JButton("Powrót");
    SpringLayout sl_ScoreWindow = new SpringLayout();
    sl_ScoreWindow.putConstraint(SpringLayout.NORTH, btnScoreReturnButton, 27, SpringLayout.NORTH, this);
    sl_ScoreWindow.putConstraint(SpringLayout.WEST, btnScoreReturnButton, 11, SpringLayout.WEST, this);
    sl_ScoreWindow.putConstraint(SpringLayout.EAST, btnScoreReturnButton, 100, SpringLayout.WEST, this);
    this.setLayout(sl_ScoreWindow);
    this.add(btnScoreReturnButton);

    ScoreLabel = new JLabel();
    sl_ScoreWindow.putConstraint(SpringLayout.NORTH, ScoreLabel, 64, SpringLayout.NORTH, this);
    sl_ScoreWindow.putConstraint(SpringLayout.WEST, ScoreLabel, 111, SpringLayout.WEST, this);
    sl_ScoreWindow.putConstraint(SpringLayout.SOUTH, ScoreLabel, -15, SpringLayout.SOUTH, this);
    sl_ScoreWindow.putConstraint(SpringLayout.EAST, ScoreLabel, -76, SpringLayout.EAST, this);

    this.add(ScoreLabel);

    JLabel SubtitleLabel = new JLabel();
    sl_ScoreWindow.putConstraint(SpringLayout.NORTH, SubtitleLabel, 20, SpringLayout.NORTH, this);
    sl_ScoreWindow.putConstraint(SpringLayout.WEST, SubtitleLabel, 80, SpringLayout.WEST,ScoreLabel);
    sl_ScoreWindow.putConstraint(SpringLayout.SOUTH, SubtitleLabel, 60, SpringLayout.NORTH, this);
    sl_ScoreWindow.putConstraint(SpringLayout.EAST, SubtitleLabel, -150, SpringLayout.EAST, this);
    SubtitleLabel.setForeground(Color.BLACK);
    SubtitleLabel.setHorizontalAlignment(JLabel.CENTER);
    SubtitleLabel.setVerticalAlignment(JLabel.CENTER);
    SubtitleLabel.setFont(ScoreLabel.getFont().deriveFont(32.0f));
     SubtitleLabel.setText("Najlepsze Wyniki");
    this.add(SubtitleLabel);
    t=new Timer(100,this);



}



public  void ReadData()

{

            data = new String[20];
            FileReader readFile=null;
            BufferedReader reader =null;
            String temp;

            try
            {

                readFile=new FileReader("highscore.dat");

                reader=new BufferedReader(readFile);
                int i=0;

                while((temp=reader.readLine())!=null)
                {

                    data[i]=temp;
                    i++;
                }

            }
            catch(Exception e)
            {
                data[0] ="Nobody:0";

            }
            finally 
            {
                try {
                    if(reader!=null)
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
}
public void LoadData() {

    String stream="<html>";
    //System.out.print(data[0]+"\n");
    for(int i=0;i<data.length;i++)
    { 
        if(data[i]!=null)
        {
            if(i==0)
                stream=stream+data[i];

        //tream=stream+data[i];
            else if(i>0)
            stream=stream+"<br>"+data[i];



        }

    }

    stream+="</html>";
    System.out.print(stream+"\n");
    ScoreLabel.setHorizontalAlignment(JLabel.CENTER);
    ScoreLabel.setVerticalAlignment(JLabel.CENTER);
    ScoreLabel.setForeground(Color.WHITE);
     ScoreLabel.setFont(ScoreLabel.getFont().deriveFont(16.0f));

    ScoreLabel.setText(stream);
    ScoreLabel.setOpaque(false);
}

public int AmoutofData()
{
    String amount[] = ScoreLabel.getText().split("<br>");

return amount.length;
}


public void paintComponent(Graphics g)
{


    t.start();
if(i<10)
sth="0"+i;
else
sth=i+"";
        Image img2 = new ImageIcon("res\\double\\double_000"+sth+".png").getImage();

        g.drawImage(img2, i, i, 100, 100, null);




}



@Override
public void actionPerformed(ActionEvent e) {

    i++;
    if(i>30)
    i=0;

    repaint();


}

}