Rabu, 21 Desember 2011

Thread_Kelas ThreadGraphic

package Graphics;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ThreadGraphic extends JPanel implements Runnable{

//    Counter counter ;
    int counter=5;
    Thread thread;
    private boolean start=true;
    private int value;

    public ThreadGraphic() {
//        counter= new Counter();
        setBackground(Color.white);
        thread = new Thread(this);
        thread.start();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (counter % 2 == 0) {
            g.setColor(Color.magenta);           
            g.fillOval(100, 50, 100, 100);
            counter-=1;
        }else{
            g.setColor(Color.black);
            g.fillOval(100, 50, 100, 100);
            counter+=1;
        }
    }

    public void run() {
        while(true){
//            outputLabel.setText("Counter: "+getValue());
            try {
                thread.sleep(200);
                this.repaint();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    private boolean isStart() {
        return start;
    }

    private int getValue() {
        return value;
    }

    private void setValue(int i) {
        this.value=value;
    }

    public static void main(String args[]){
        JFrame frame = new JFrame( "Grafik 2 Dimensi" );
        frame.addWindowListener( new WindowAdapter(){
            public void windowClosing( WindowEvent e ){
                System.exit( 0 );
            }
        });

        frame.setLayout(new BorderLayout());
        ThreadGraphic graph = new ThreadGraphic();
        frame.add(graph,BorderLayout.CENTER);

        frame.setSize( 450, 250 );
        frame.setVisible(true);

        AnotherCounter counter = new AnotherCounter();
       
    }
}

1 komentar:

  1. Hyundai Sonata AC Compressor
    Excellent blog& presentation. Keep writing more and more. I voted for your blog and suggested your blog to my friends also.

    BalasHapus