I have a class, where I load the display of weather data and want to display a loading animation when the display is loaded.
Here's the initial code:
DrawFrame(JSONArray weather, String button) {
this.weather = weather;
drawPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
switch (button) {
case MainFrame.hourly1ButtonValue:
hourly1 = (Graphics2D) g;
drawHourly1();
break;
case MainFrame.hourly12ButtonValue:
hourly12 = (Graphics2D) g;
drawHourly12();
break;
case MainFrame.index5ButtonValue:
stargazing = (Graphics2D) g;
drawStargazing();
break;
}
}
};
KeyListener kl=new KeyAdapter()
{
public void keyPressed(KeyEvent evt)
{
//If someone click Esc key, this program will exit
if(evt.getKeyCode()==KeyEvent.VK_ESCAPE)
{
System.exit(0);
}
}
};
JFrame frame = new JFrame("Wetteranzeige");
//frame.setUndecorated(true);
frame.add(drawPanel);
frame.setPreferredSize(new Dimension(dimension.width, dimension.height));
//frame.addKeyListener(kl);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
and this is one of the methods, I'm using for display one weather Display:
private void drawStargazing() {
Integer[][] coordinates = new Integer[2][5];
coordinates = coordinates5DaysIndex();
drawPanel.setBackground(Color.black);
int i = 0;
int j = 0;
int k = 1;
int x;
int y;
while (i <= 4) {
x = coordinates[0][i];
y = coordinates[1][i];
stargazing.drawImage(toolkit.getImage(getClass().getResource("images/stern_t.png/")), x - 20, y - 20, drawPanel);
i++;
Stroke stroke = new BasicStroke(5);
stargazing.setStroke(stroke);
}
while (k >= 1 && j <= 3) {
int xEnde = coordinates[0][k];
int yEnde = coordinates[1][k];
int xStart = coordinates[0][k - 1];
int yStart = coordinates[1][k - 1];
stargazing.setColor(Color.white);
stargazing.drawLine(xStart, yStart, xEnde, yEnde);
j++;
k++;
}
}
it produces something like that: 1
when I add a gif it loops endlessly, not once, so I thought I could manually paint the gif by setting each frame (in my case change the position of an icon) but I can't figure out to correctly repaint with a timer in accordance to my drawing construct here ...
I can't manage to make it work like in this example: https://docs.oracle.com/javase/tutorial/uiswing/painting/step3.html
So does anyone have a solution for me? Your help would be greatly appreciated!