简体   繁体   中英

Why isn't my java animation working?

Everything is still when I want the raindrops to move down the screen until they hit the bottom and then do it again. I used implements runnable and have a run method that repaints it. Anyone know what I am missing?

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Screensaver extends JPanel implements Runnable{
    private final static int FRAME_HEIGHT = 600;
    private final static int FRAME_WIDTH = 600;
    int rainY = 100;
    int rainGo = 1;
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
        frame.add(new Screensaver());
        frame.setVisible(true);

    }
    public Screensaver(){
        Color background;
        background = new Color(212,202,115);
        setBackground(background);
    }
     protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Color tree;
            Color leaves;
            Color cloud;
            Color rain;
            rain = new Color(0,128,255);
            cloud = new Color(160,160,160);
            leaves = new Color(0,204,0);
            tree = new Color(102,0,0);

            g.setColor(tree);
            //g.drawLine(400, 375, 360, 340);
            g.fillRect(400, 250,80, 320 );
            g.setColor(leaves);
            g.fillOval(340,150 , 200, 160);
            g.setColor(cloud);
            g.fillOval(10,5,550,100);

            Random random = new Random();
            int rainX1 = random.nextInt(500) + 40;
            int rainW1 = random.nextInt(30) + 10;
            int rainX2 = random.nextInt(500) + 40;
            int rainW2 = random.nextInt(30) + 10;
            int rainX3 = random.nextInt(500) + 40;
            int rainW3 = random.nextInt(30) + 10;
            int rainX4 = random.nextInt(500) + 40;
            int rainW4 = random.nextInt(30) + 10;

            g.setColor(rain);
            g.fillOval(rainX1, rainY + rainGo, rainW1, rainW1);
            g.fillOval(rainX2, rainY+ rainGo, rainW2, rainW2);
            g.fillOval(rainX3, rainY+ rainGo, rainW3, rainW3);
            g.fillOval(rainX4, rainY+ rainGo, rainW4, rainW4);


        }
     public void run(){
         while(true){
             changeRain();
             repaint();
         }
     }
     public void changeRain(){
         if(rainY+rainGo< FRAME_HEIGHT){
             rainGo++;
         }
         else{
             rainGo = 1;
         }
     }
}

Short answer: You didn't start a Thread to start your animation.

Full answer: Use one of Swings concurrency mechanisms such as a Swing Timer for animation. Swing Timers are designed to interact correctly with Swing components.

I don't see anywhere in your code where you create a background Thread and start it. Threads don't simply start themselves. If you do go this route, consider putting a Thread.sleep(...) in your run method to give the while loop a slight pause. Better though to use a Swing Timer for your animation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM