2010-11-01 49 views
2

我用java applet编写了一些代码,并且我想用一些滑动条控件将它制作成一个java swing应用程序的面板。我应该把它放在一个JApplet或JPanel或其他东西?我很困惑。帮助赞赏。 Applet类的如何将这个java applet代码迁移到java swing

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; 
    } 
} 

回答

1

一旦你重新的因素在Applet一个JPanel绘制,由@justkt建议,你可以创建一个混合的小程序/应用程序,如在此example

2

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

这听起来像你的目的,你正在考虑把它放在一个JFrame把它作为一个独立的窗口。在这种情况下,请考虑扩展JPanelJComponent。请注意,并非像您在现有代码中所做的那样覆盖paint,您将需要覆盖paintComponent

一旦您有子女JComponent,您可以将其添加到JFrame并添加滑块或其他组件以创建您的应用程序。