bola counter

Wednesday, December 21, 2011

Bola Panel:


package Thread_BallCounter;
import java.awt.*;
import javax.swing.*;
public class ballPanel extends JPanel{
    private int number;
    public ballPanel(){}
    public void setNumber(int number){this.number=number;}
    public int getNumber(){return number;}
    @Override
    public void paintComponent(Graphics graph){
        super.paintComponent(graph);
        if(number%2==0)
            graph.setColor(Color.green);
        else
            graph.setColor(Color.white);
        graph.fillOval(20,20,100,100);
    }
}




Bola Counter:


package Thread_BallCounter;
import javax.swing.*;
public class ballCounter extends Thread{
    JTextField tf;
    JButton startStop;
    Thread thread;
    boolean counter=false;
    int number=0;
    ballPanel ballPanel;
    public ballCounter(){}
    public ballCounter(JTextField tf, JButton startStop, ballPanel ballPanel) {
        this.tf = tf;
        this.startStop = startStop;
        this.ballPanel = ballPanel;
    }
    public void startBallCounter(){
        thread = new Thread(this);
        if(counter==false){
            thread.start();
            counter=true;
            startStop.setText("STOP");
        }else{
            counter=false;
            startStop.setText("START");}
    }
    @Override
    public void run(){
        while(counter){
            number=number+1;
            tf.setText(String.valueOf(number));
            try{
                thread.sleep(1000);
                ballPanel.setNumber(number);
                ballPanel.repaint(); }
            catch(InterruptedException ex) {
                ex.printStackTrace();}
        }
    }
}


GUI:


package Thread_BallCounter;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ballCounterGUI extends JFrame {
    JPanel panel;
    JTextField tf;
    JButton startStopBTN;
    ballPanel ballPanel;
    boolean condition=true;
    ballCounter ballCounter;
    public ballCounterGUI(){
        this.setLocation(100,100);
        this.setSize(400,200);
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Ball Counter Application");
        Container container=this.getContentPane();
        container.setLayout(new GridLayout(0,2));
        panel=new JPanel();
        panel.setLayout(new FlowLayout());
        startStopBTN=new JButton("Start");
        tf=new JTextField(10);
        tf.setEditable(false);
        panel.add(startStopBTN);
        panel.add(tf);
        container.add(panel);
        ballPanel=new ballPanel();
        container.add(ballPanel);
        startStopBTN.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                ballCounterAction();}
        });
    }
    public void ballCounterAction() {
        if(condition){
            ballCounter=new ballCounter(tf, startStopBTN, ballPanel);
            ballCounter.startBallCounter();
            condition=false;}
        else{
            ballCounter.startBallCounter();
            condition=true; }
    }
    public static void main(String[] args) {
        ballCounterGUI test = new ballCounterGUI();
        test.setVisible(true);
    }
}



Get This Comment Form

0 comment(s):

 
Copyright© 2010 | http://felisitasisme.com