Applet onde e como chamo p começar a thread

1 resposta
T

Gostaria de quando clicasse no button iniciar a animação começasse, porém achei q só colocando t.start já daria certo =/, o que mais tenho q fazer?

import java.awt.*;

import javax.swing.JApplet;
import javax.swing.JFrame;


public class applet extends JApplet{
    DrawPanel panel;
    DrawControls controls;


    public void init() {
	setLayout(new BorderLayout());
	panel = new DrawPanel();
        controls = new DrawControls(panel);
	add("Center", panel);
	add("South",controls);
    }

    public void destroy() {
        remove(panel);
        remove(controls);
    }

    public static void main(String args[]) {

	JFrame f = new JFrame("Pêndulo tipo mola");
	 applet drawTest = new  applet();
	drawTest.init();
	drawTest.start();

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	f.add("Center", drawTest);
	f.setSize(500, 500);
	f.setVisible(true);
    }


}



import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.util.Random;

class DrawPanel extends Panel  implements Runnable{

   

int w =350;
    int h = 250;
    public DrawPanel() {
	setBackground(Color.white);
        
           }


    public void run ()
	{       int x = 10;
		int y = 10;
		int xx = 1;
		int yy = 2;
		
		
		while (true)
		{
			Random r = new Random();
			int p = r.nextInt()+1;
			p = p%4;
			if(p == 0 || p < 0)p = 1;

			if(x > this.getSize().width - 60)
			{
				xx = -p;
			}else
				if(y > this.getSize().height - 60)
				{
					yy = -p;
				}
				else if(x < 0)
				{
					xx = p;

				}
				else if(y < 0)
				{
					yy = p;
				}
			x += xx;
			y += yy;
			
			repaint();

			try
			{
				
				Thread.sleep (20);
			}
			catch (InterruptedException ex)
			{
				// do nothing
			}

			
		}

	}




    public void paint(Graphics g) {
         super.paint(g);
        Graphics2D g2d = (Graphics2D) g.create();
	g2d.setColor(Color.BLACK);
         g2d.drawLine(125, 0, 225,0);//apoio do pendulo
         g2d.drawLine(w/2, 0, w/2,10);//inicio do pendulo
          g2d.drawLine((w/2), (h/2)-30, w/2,(h/2)+10);// fim do pendulo
         for(int i = 0; i< 80; i+=5) //mola
          g2d.drawOval((w/2)-12, 10+i, (w/2)-150 , 10);

         g2d.setColor(Color.RED);
         g2d.fillOval((w/2)-17 , (h/2) -20,30, 30); // bolinha

        g2d.dispose();

    }
   }

   

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.Event.*;
import java.awt.event.ActionListener;



class DrawControls extends JPanel implements ActionListener {
    DrawPanel target;

    private JButton iniciar = new JButton("Iniciar");
   private JButton pausar = new JButton("Pausar");
   private JButton continuar = new JButton("Continuar");

   private JLabel massa = new JLabel("Massa:"); private JLabel kg = new JLabel("kg");
   private JLabel k = new JLabel("Constante da mola (k):");private JLabel nm = new JLabel("N/m");
   private JLabel a = new JLabel("Amplitude:");private JLabel m = new JLabel("m");
   private JLabel osc = new JLabel("Período de Oscilação:");private JTextField oscresp = new JTextField(3);
   private JLabel s = new JLabel("s");

   private JRadioButton e = new JRadioButton("Elongação");
   private JRadioButton et = new JRadioButton("Energia Total");

   private JTextField txtmassa = new JTextField(3);
    private JTextField txtk = new JTextField(3);
     private JTextField txta = new JTextField(3);
   






    public DrawControls(DrawPanel target) {
	 GridBagConstraints cons = new GridBagConstraints();
        GridBagLayout layout = new GridBagLayout();

        JPanel panel = new JPanel(layout);

        cons.fill = GridBagConstraints.BOTH;
        cons.gridy = 0;        cons.gridx = 0;

	   panel.setPreferredSize(new Dimension(450,125));
	setBackground(Color.lightGray);

          panel.add(massa, cons);
          cons.gridy = 0;        cons.gridx = 1;
          panel.add(txtmassa, cons);
          cons.gridy = 0;        cons.gridx = 2;
          panel.add(kg, cons);

          cons.gridx = 0; cons.gridy = 1;
       panel.add(k, cons);
       cons.gridy = 1;        cons.gridx = 1;
       panel.add(txtk, cons);
       cons.gridy = 1;        cons.gridx = 2;
       panel.add(nm, cons);

        cons.gridx = 0; cons.gridy = 2;
       panel.add(a, cons);
       cons.gridy = 2;        cons.gridx = 1;
       panel.add(txta, cons);
       cons.gridy = 2;        cons.gridx = 2;
       panel.add(m, cons);

        cons.gridx = 0; cons.gridy = 3;
      panel.add(osc, cons);
      cons.gridx = 1; cons.gridy = 3;
      panel.add(oscresp, cons);
      cons.gridx = 2; cons.gridy = 3;
      panel.add(s, cons);
					//botões
       cons.gridx = 0; cons.gridy = 4;
	iniciar.addActionListener(this);
      panel.add(iniciar, cons);

      cons.gridx = 1; cons.gridy = 4;
	pausar.addActionListener(this);
	panel.add(pausar, cons);


      cons.gridx = 2; cons.gridy = 4;
	continuar.addActionListener(this);
      panel.add(continuar, cons);

      ButtonGroup group = new ButtonGroup();

      cons.gridx = 3; cons.gridy = 0;
    JRadioButton RadioButton = new JRadioButton("Velocidade", true);
    RadioButton.setBackground(Color.lightGray);
    panel.add(RadioButton, cons);
    group.add(RadioButton);
    cons.gridx = 3; cons.gridy = 1;
    RadioButton = new JRadioButton("Elongação");
    RadioButton.setBackground(Color.lightGray);
    panel.add(RadioButton, cons);
    group.add(RadioButton);
    cons.gridx = 3; cons.gridy = 2;
    RadioButton = new JRadioButton("Energia total");
    RadioButton.setBackground(Color.lightGray);
    panel.add(RadioButton, cons);
    group.add(RadioButton);



     panel.setBackground(Color.lightGray);
          setLayout(new BorderLayout());
      add("South", panel);




      }

     public void actionPerformed(ActionEvent e) {

        if (e.getSource() == iniciar){
           Thread t = new Thread();
           t.start();
}
     }}

1 Resposta

T

consegui

public void run ()
	{      f= true;
               while(f){

                   String numsei = Integer.toString( j );

			try
			{
				
				Thread.sleep (20);
			}
			catch (InterruptedException ex)
			{
				ex.printStackTrace();
			}
                        
                          System.out.println("Número:"+ j++);
			
		}

	}

=======

DrawPanel tread;
   
    public DrawControls(DrawPanel target) {
        tread = new DrawPanel();

  public void actionPerformed(ActionEvent e) {

        if (e.getSource() == iniciar){
           Thread t = new Thread(tread);
           t.start();
}
     }}
Criado 10 de julho de 2011
Ultima resposta 10 de jul. de 2011
Respostas 1
Participantes 1