Java让我选择使用哪个类而不是同时运行它们

时间:2016-02-10 18:44:18

标签: java animation

正如标题所说,程序让我选择我想要运行哪个类,何时需要同时运行

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Animation extends JPanel {
    int x = 250;
    int y = 600;
    protected void moveBall() {
        x = x + 0;
        y = y - 1;
    }
    @Override
    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillOval(x, y, 25, 25);
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame("Animation");
        Animation game1 = new Animation();
        frame.add(game1);
        frame.setSize(500, 600);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (true) {
            game1.moveBall();
            game1.repaint();
            Thread.sleep(10);    
        }
    }
}


@SuppressWarnings("serial")
class Animation1 extends JPanel {
    int x1 = 0;
    int y1 = 300;
    protected void moveBall() {
        x1 = x1 + 1;
        y1 = y1 + 0;
    }

    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillOval(x1, y1, 25, 25);
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame ("Animation");
        Animation1 game1 = new Animation1();
        frame.add(game1);
        frame.setSize(500, 600);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (true) {
            game1.moveBall();
            game1.repaint();
            Thread.sleep(10);    
        }
    }
}

我需要两条线从左到右交叉一条从下往上交叉,所以我需要它们同时运行,我不知道如果我这样做的方式是完全正确的或者如果有的话一个更好的方法,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:-1)

首先:您目前有两个入口点,这不是您想要的。

我会重构它以使用单个main(String [] args)方法。