如何将此Java applet代码迁移到java swing

时间:2010-11-01 18:48:09

标签: java swing

我使用java applet编写了一些代码,我想把它变成一个带有一些滑块控件的java swing应用程序中的面板。我应该把它放在JApplet或JPanel或其他东西?我糊涂了。帮助赞赏。

import java.applet.*;
import java.util.ArrayList;
import java.awt.*;
import java.util.Random;

public class BallParticle extends Applet implements Runnable {

    private ArrayList<Ball> ballArr = new ArrayList<Ball>();

    private static int refreshBackground = 1,//1 = refresh, 0 = don't refresh
                       runSpeed = 40;        // ~ 25 Hz
    private static String state="p";         //"s"=spiral, "p"=particle


    public void init() {
        setSize(600, 500);
    }

    public void start() {
        Thread th = new Thread(this);
        th.start();
    }

    public void run() {
        while (true) {
            for (Ball ball: ballArr)
                ball.move();
            repaint();

            try {
                Thread.sleep(runSpeed);
            } catch (InterruptedException e) {
            }
        }
        }

    @Override
    public void update(Graphics g) {
         //clear screen in background
        if (refreshBackground == 1) {
            Image img1 = createImage(this.getSize().width, this.getSize().height);
            Graphics g1 = img1.getGraphics();
            g1.setColor(getBackground());
            paint(g1);
            g.drawImage(img1, 0, 0, this);
        }
        //draw image on the screen
        paint(g);
    }
    public void paint(Graphics g) {
        for (Ball b: ballArr){
            g.setColor(b.getColor());
            g.fillOval(b.getXCoor(),b.getYCoor(),b.getSize(),b.getSize());
        }
    }

    int i = 0;
    public boolean mouseDown(Event e, int centerX, int centerY) {
        ballArr.add(new Ball(centerX, centerY, "p"));
        return true;
    }

}
//////////////////////////////////////////////////////////////////////////////
class Ball{
    //////////////////////////////////////////////////////////////////////////
    private static int instanceCount; {{instanceCount++;}}
    private int z = 11, t=1, u=1;
    private int[] RGB = new int[3];
    private int[] randomizeColor = new int[3];
    private double radius, theta;
    private int x, y, centerX, centerY, size, spiralDirection=1,
                ballSizeLowerBound, ballSizeUpperBound,
                radiusLowerBound, radiusUpperBound,
                mouseInputX, mouseInputY,
                radiusXMultiplier, radiusYMultiplier;
    private Color color;
    private String state;
    private Random random = new Random();
    ///////////////////////////////////////////////////////////////////////////
    public Ball(int x, int y, int centerX, int centerY, int radius,
                int theta, int size, Color color){
        this.x=x;this.y=y;this.centerX=centerX;this.centerY=centerY;
        this.radius=radius;this.theta=theta;this.size=size;this.color=color;
    }

    public Ball(int mouseInputX, int mouseInputY, String state){
        this.mouseInputX=mouseInputX;
        this.mouseInputY=mouseInputY;
        this.state=state;
       //randomize color
        RGB[0] = random.nextInt(255);
        RGB[1] = random.nextInt(255);
        RGB[2] = random.nextInt(255);
        randomizeColor[0] = 1+random.nextInt(3);
        randomizeColor[0] = 1+random.nextInt(3);
        randomizeColor[0] = 1+random.nextInt(3);
        centerX=mouseInputX;
        centerY=mouseInputY;
        if (state.equals("s")){ //setup spiral state

            ballSizeLowerBound=5;
            ballSizeUpperBound=18;
            radiusLowerBound=0;
            radiusUpperBound=50;
            radiusXMultiplier=1;
            radiusYMultiplier=1;
        }
        if (state.equals("p")){ //setup particle state
            ballSizeLowerBound = 15;
            ballSizeUpperBound =20 + random.nextInt(15);
            radiusLowerBound = 5;
            radiusUpperBound = 15+ random.nextInt(40);
            radiusXMultiplier=1 + random.nextInt(3);
            radiusYMultiplier=1 + random.nextInt(3);
        }

        size = ballSizeUpperBound-1; //ball size
 radius = radiusUpperBound-1;

        if (instanceCount %2 == 0) // alternate spiral direction
            spiralDirection=-spiralDirection;
    }
    ///////////////////////////////////////////////////////////////////////////
    public int getX(){return x;}
    public int getY(){return y;}
    public int getCenterX(){return centerX;}
    public int getCenterY(){return centerY;}
    public int getXCoor(){return centerX+x*spiralDirection;}
    public int getYCoor(){return centerY+y;}
    public double getRadius(){return radius;}
    public int getSize(){return size;}
    public double getTheta(){return theta;}
    public String getState(){return state;}
    public Color getColor(){return color;}

    public void setX(int x){this.x=x;}
    public void setY(int y){this.y=y;}
    public void setCenterX(int centerX){this.centerX=centerX;}
    public void setCenterY(int centerY){this.centerY=centerY;}
    public void setRadii(double radii){this.radius=radii;}
    public void setTheta(double theta){this.theta=theta;}
    public void setSize(int size){this.size=size;}
    public void setState(String state){this.state=state;}
    public void setColor(Color color){this.color=color;}
    //////////////////////////////////////////////////////////////////////////
    void move(){

            //spiral: dr/dt changes at bounds
            if (radius > radiusUpperBound || radius < radiusLowerBound)
                u = -u;

            //spiral shape formula: parametric equation for the
            //polar equation radius = theta
            x = (int) (radius * radiusXMultiplier * Math.cos(theta));
            y = (int) (radius * radiusYMultiplier * Math.sin(theta));

            radius += .3*u;
            theta += .3;

            //ball size formula
            if (size == ballSizeUpperBound || size == ballSizeLowerBound)
                t = -t;
            size += t;

            //ball colors change
            for (int i = 0; i < RGB.length; i++)
                if (RGB[i] >= 250 || RGB[i] <= 3)
                    randomizeColor[i] = -randomizeColor[i];

            RGB[0]+= randomizeColor[0];
            RGB[1]+= randomizeColor[1];
            RGB[2]+= randomizeColor[2];
            color = new Color(RGB[0],RGB[1],RGB[2]);

    }

    @Override
    public String toString(){
        return "r0="+randomizeColor[0] + "r1="+randomizeColor[1] +"r2="+randomizeColor[2]
                +" R="+RGB[0]+" G="+RGB[1]+" B="+RGB[2];
                //"x="+x+" y="+y+" centerX="+centerX+" centerY="+centerY+" radius="
                //+radius+" theta="+theta+" size="+size+" spDirect="+spiralDirection;
    }
}

2 个答案:

答案 0 :(得分:2)

Applet的扩展是重量级图形组件,而JAppletJPanel等Swing组件是轻量级组件。也就是说,JApplet extends applet,所以如果你希望你可以将现有代码转换为JApplet

听起来你的目的就是把它放在JFrame中,让它作为一个独立的窗口。在这种情况下,请考虑扩展JPanelJComponent。请注意,您不必像现有代码那样覆盖paint,而是覆盖paintComponent

如果您有JComponent的孩子,可以将其添加到JFrame并添加滑块或其他组件以创建您的应用。

答案 1 :(得分:1)

根据@justkt的建议,重新考虑AppletJPanel进行绘制后,您可以创建混合小程序/应用程序,如此example所示。