0

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!

3gg5y
  • 13
  • 2
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Feb 02 '19 at 18:16
  • Maybe you can use the [Animated Icon](https://tips4java.wordpress.com/2009/06/21/animated-icon/). It allows you to load multiple images in a single Icon and will then provide the animation. You can do continuous animation or control the number of animation cycles. – camickr Feb 02 '19 at 18:26

0 Answers0