• Lunes 29 de Abril de 2024, 07:22

Autor Tema:  Convertir 1 Applet En Jpanel  (Leído 1246 veces)

agsim

  • Nuevo Miembro
  • *
  • Mensajes: 4
    • Ver Perfil
Convertir 1 Applet En Jpanel
« en: Jueves 30 de Marzo de 2006, 11:17 »
0
Hola, tengo un pequeño problema, estoy creando un applet, y buscando encontre otro applet que hacia una función que me gusta. Mi problema es que quiero poder convertir el applet  que en una clase que no extienda de Applet para poder instanciarlo en un JPanel desde mi applet.

Espero que me podias ayudar, aqui os dejo el applet que encontre.

Gracias!!


public class GaltonOriginal extends Applet
    implements Runnable
{
 
    Thread runner;
    int xpos;
    int xold;
    int ypos;
    int yold;
    int xtemp;
    int nmax;
    int m;
    int xpospaal;
    int dim;
    int width;
    int height;
    double r;
    boolean threadSuspended;
    Random rd;
    Image offscreenImg;
    Graphics offscreenG;
    int nobs;
    int nbox;
    int boxes[];
 
    public void init()
    {
        setBackground(Color.white);
        offscreenImg = createImage(size().width, size().height);
        offscreenG = offscreenImg.getGraphics();
        dim = size().width / 2;
        ypos = 4;
        xpos = dim - 2;
        xtemp = 0;
        nmax = 100;
        m = 0;
    }
 
    public void start()
    {
        runner = new Thread(this);
        runner.start();
    }
 
    public void stop()
    {
       
     if(runner != null)
        {
            runner.stop();
            runner = null;
       
        }
    }
 
    public void clear()
    {
        for(int u = 0; u < nbox; u++)
            boxes = 0;
 
    }
 
    public void run()
    {
        while(m < nmax)
        {
            m = m + 1;
            for(ypos = 18; ypos < 16 * nobs; ypos += 15)
            {
                r = rd.nextDouble();
                if(r < 0.5D)
                {
                    xpos = xpos + 10;
                    xold = xpos - 6;
                    xtemp = xtemp + 1;
                } else
                {
                    xpos = xpos - 10;
                    xold = xpos - 10;
                }
                yold = ypos - 5;
                repaint();
                try
                {
                    Thread.sleep(90L);
                }
                catch(InterruptedException _ex) { }
            }
 
            boxes[xtemp] = boxes[xtemp] + 1;
            xold = dim - 10;
            yold = 0;
            xpos = dim - 2;
            ypos = 3;
            xtemp = 0;
            repaint();
        }
    }
 
    public void update(Graphics g)
    {
        paint(g);
    }
 
    //si se clikea con el raton
   
    public boolean mouseDown(Event evt, int x, int y)
    {
        if(x > dim - 30 && x < dim + 30 && y > 220 && y < 235)
        {
            stop();
            clear();
            //play(getCodeBase(), "audio/beep.au");
            start();
            m = 0;
            xtemp = 0;
        } else
        if(threadSuspended)
            runner.resume();
        else
            runner.suspend();
        threadSuspended = !threadSuspended;
        return true;
    }
 
    public void paint(Graphics g)
    {
        Font f = new Font("TimesRoman", 0, 12);
        offscreenG.setColor(Color.white);
        offscreenG.fillRect(0, 0, size().width, size().height);
        offscreenG.setColor(Color.black);
        for(int i = 0; i < nobs; i++)
        {
            for(int j = 0; j <= i; j++)
            {
                //puntos negros de la piramide
             xpospaal = dim - i * 10;
                offscreenG.fillRect(xpospaal + j * 20, 15 + i * 15, 4, 4);
            }
 
        }
 
        offscreenG.setColor(Color.blue);
        for(int x = 0; x < nobs + 1; x++)
        {
            //barra de estado azul al apilar las cosas
         offscreenG.fillRect(x * 20, 199 - boxes
  • , 20, boxes
  • );

            //barra verticales
            offscreenG.drawLine(x * 20, 180, x * 20, 199);
            //barra de suelo
            offscreenG.drawLine(0, 199, size().width, 199);
 
        }
 
        offscreenG.fillRect(dim - 30, 220, 60, 15);
        offscreenG.setColor(Color.white);
        offscreenG.setFont(f);
        offscreenG.drawString("restart", dim - 22, 230);
        offscreenG.setColor(Color.red);
        //dimensiones de la bolita
        offscreenG.fillOval(xpos, ypos, 8, 8);
        g.drawImage(offscreenImg, 0, 0, this);
    }
 
    public GaltonOriginal()
    {
        threadSuspended = false;
        rd = new Random();
        nobs = 10;
        nbox = 11;
        boxes = new int[nbox];
    }
}