2010-11-01 5 views
2

J'ai écrit du code en utilisant une applet java, et je veux en faire un panneau dans une application java swing avec quelques commandes de barre de curseur. Devrais-je le mettre dans un JApplet ou JPanel ou autre chose? Je suis confus. Aide appréciée.Comment migrer ce code de l'applet java vers 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; 
    } 
} 

Répondre

1

Une fois que vous avez re-factorisé votre Applet pour dessiner dans un JPanel, comme le suggère @justkt, vous pouvez créer une applet hybride/application, comme le montre cette example.

2

Extensions de classe Applet sont des composants graphiques lourds, tandis que les composants Swing tels que JApplet et JPanel sont des composants légers. Cela dit, applet JApplet extends, donc si vous le souhaitez, vous pouvez convertir votre code existant pour étendre JApplet.

Il semble que pour vos besoins vous envisagez de mettre cela dans un JFrame pour l'avoir comme une fenêtre autonome. Dans ce cas, regardez dans l'extension JPanel ou JComponent. Notez que plutôt que de surcharger paint comme vous l'avez fait dans votre code existant, vous devrez remplacer paintComponent.

Une fois que vous avez un enfant de JComponent, vous pouvez l'ajouter à un JFrame et ajouter vos curseurs ou autres composants pour créer votre application.