2010-11-01 74 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並添加滑塊或其他組件以創建您的應用程序。