circle
public class CirclePanel extends JPanel
{
//* Deklarasi atribut
private int number;
//* Constructor
public CirclePanel()
{
}
//* Method accessor dan mutator untuk mengubah nilai atribut
public void setNumber(int number)
{
this.number=number;
}
public int getNumber()
{
return number;
}
//* Method untuk membuat lingkaran, dengan instruksi bersyarat
@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,10,200,200);
}
}
public class CircleCounter extends Thread {
//* Deklarasi atribut
private JTextField textField;
private JButton starStop;
Thread thread;
private boolean counter = false;
private int number = 0;
CirclePanel circlePanel;
//* Constructor
public CircleCounter() {
}
public CircleCounter(JTextField textField, JButton starStop, CirclePanel circlePanel) {
this.textField = textField;
this.starStop = starStop;
this.circlePanel = circlePanel;
}
//* Method menjalankan Thread
public void startCircleCounter() {
thread = new Thread(this);
if (counter == false) {
thread.start();
counter = true;
starStop.setText("Stop");
} else {
counter = false;
starStop.setText("Start");
}
}
//* Eksekusi ketika thread dijalankan
@Override
public void run() {
while (counter) {
number = number + 1;
textField.setText(String.valueOf(number));
try {
thread.sleep(500);
circlePanel.setNumber(number);
circlePanel.repaint();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CircleCounterGUI extends JFrame
{
//* Deklarasi atribut
private JPanel mainPanel;
private JTextField textField;
private JButton startStopButton;
private CirclePanel circlePanel;
private boolean condition=true;
CircleCounter circleCounter;
public CircleCounterGUI()
{
//* mengatur frame
this.setLocation(100,100);
this.setSize(600,300);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Circle Green & White");
//* membuat container beserta komponennya, kemudian memasukkannya pada container
Container container=this.getContentPane();
container.setLayout(new GridLayout(0,2));
mainPanel=new JPanel();
mainPanel.setLayout(new FlowLayout());
startStopButton=new JButton("Start");
textField=new JTextField(10);
textField.setEditable(false);
mainPanel.add(startStopButton);
mainPanel.add(textField);
container.add(mainPanel);
circlePanel=new CirclePanel();
container.add(circlePanel);
//* penanganan pada tombol startStopButton
startStopButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
randomCircleAction();
}
});
}
//* method untuk memulai counter/menghitung dan menampilkan lingkaran berwarna
public void randomCircleAction()
{
if(condition)
{
circleCounter=new CircleCounter(textField, startStopButton, circlePanel);
circleCounter.startCircleCounter();
condition=false;
}
else
{
circleCounter.startCircleCounter();
condition=true;
}
}
}
package pertemuan13_pbo;
public class CircleCounterGUI_Test
{
public static void main(String [] args)
{
//* Menjalankan fungsi thread dari kelas CircleCounterGUI dan menampilkannya
CircleCounterGUI test=new CircleCounterGUI();
test.setVisible(true);
}
}
{
//* Deklarasi atribut
private int number;
//* Constructor
public CirclePanel()
{
}
//* Method accessor dan mutator untuk mengubah nilai atribut
public void setNumber(int number)
{
this.number=number;
}
public int getNumber()
{
return number;
}
//* Method untuk membuat lingkaran, dengan instruksi bersyarat
@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,10,200,200);
}
}
public class CircleCounter extends Thread {
//* Deklarasi atribut
private JTextField textField;
private JButton starStop;
Thread thread;
private boolean counter = false;
private int number = 0;
CirclePanel circlePanel;
//* Constructor
public CircleCounter() {
}
public CircleCounter(JTextField textField, JButton starStop, CirclePanel circlePanel) {
this.textField = textField;
this.starStop = starStop;
this.circlePanel = circlePanel;
}
//* Method menjalankan Thread
public void startCircleCounter() {
thread = new Thread(this);
if (counter == false) {
thread.start();
counter = true;
starStop.setText("Stop");
} else {
counter = false;
starStop.setText("Start");
}
}
//* Eksekusi ketika thread dijalankan
@Override
public void run() {
while (counter) {
number = number + 1;
textField.setText(String.valueOf(number));
try {
thread.sleep(500);
circlePanel.setNumber(number);
circlePanel.repaint();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CircleCounterGUI extends JFrame
{
//* Deklarasi atribut
private JPanel mainPanel;
private JTextField textField;
private JButton startStopButton;
private CirclePanel circlePanel;
private boolean condition=true;
CircleCounter circleCounter;
public CircleCounterGUI()
{
//* mengatur frame
this.setLocation(100,100);
this.setSize(600,300);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Circle Green & White");
//* membuat container beserta komponennya, kemudian memasukkannya pada container
Container container=this.getContentPane();
container.setLayout(new GridLayout(0,2));
mainPanel=new JPanel();
mainPanel.setLayout(new FlowLayout());
startStopButton=new JButton("Start");
textField=new JTextField(10);
textField.setEditable(false);
mainPanel.add(startStopButton);
mainPanel.add(textField);
container.add(mainPanel);
circlePanel=new CirclePanel();
container.add(circlePanel);
//* penanganan pada tombol startStopButton
startStopButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
randomCircleAction();
}
});
}
//* method untuk memulai counter/menghitung dan menampilkan lingkaran berwarna
public void randomCircleAction()
{
if(condition)
{
circleCounter=new CircleCounter(textField, startStopButton, circlePanel);
circleCounter.startCircleCounter();
condition=false;
}
else
{
circleCounter.startCircleCounter();
condition=true;
}
}
}
package pertemuan13_pbo;
public class CircleCounterGUI_Test
{
public static void main(String [] args)
{
//* Menjalankan fungsi thread dari kelas CircleCounterGUI dan menampilkannya
CircleCounterGUI test=new CircleCounterGUI();
test.setVisible(true);
}
}
Komentar