'How can I make graphics moving when I press start button and stop when I press stop button.(Java)
I am working on my first college project. I just start coding. I am making a washing machine for this project.
How can I make my graphics rotate when pressed the start button and stop when I pressed the stop button? Also, I have a countdown timer, once it times out, my graphics will stop rotating as well. (They are in different packages).
Here is my code(Main, first package)
package Main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import Timer.*;
public class WashingMachine extends JPanel implements ActionListener{
JFrame frame = new JFrame("Washing Machine");
JLabel label_numCloth = new JLabel("Number of Clothes");
JTextField num_tf = new JTextField("",10);
JButton com_bt = new JButton("Compute");
JButton start_bt = new JButton("Start");
JButton stop_bt = new JButton("Stop");
JLabel label_cost = new JLabel("Cost: ");
JLabel label_price = new JLabel("1 Kg = 4 Baht");
JPanel panel_num = new JPanel();
JPanel panel_cost = new JPanel();
JPanel panel_r = new JPanel();
JPanel panel_l = new JPanel();
JPanel panel_start_stop = new JPanel();
static double cost;
WashingMachine(){
panel_num.setLayout(new GridLayout(3,1));
panel_num.add(label_numCloth);
panel_num.add(num_tf);
panel_num.add(com_bt);
panel_cost.setLayout(new GridLayout(2,1));
panel_cost.add(label_cost);
panel_cost.add(label_price);
panel_start_stop.setLayout(new GridLayout(1,2));
panel_start_stop.add(start_bt);
panel_start_stop.add(stop_bt);
panel_l.add(panel_start_stop);
panel_r.setLayout(new BorderLayout());
panel_r.add(panel_num,BorderLayout.NORTH);
panel_r.add(panel_cost,BorderLayout.CENTER);
frame.add(this);
setLayout(new BorderLayout());
frame.add(panel_r,BorderLayout.EAST);
frame.add(panel_l,BorderLayout.WEST);
frame.setBackground(Color.BLACK);
frame.setSize(250,250);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label_price.setForeground(Color.red);
com_bt.addActionListener(this);
start_bt.addActionListener(this);
stop_bt.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//Calculate price
double cloth = Double.parseDouble(num_tf.getText());
cost = cloth*4;
label_cost.setText("Cost: " + cost + " " + "Baht");
if(e.getSource() == start_bt) {
repaint();
Simulation sim = new Simulation();
Countdown cout_d = new Countdown();
if(e.getSource() == start_bt) {
repaint();
}
}
}
}
Here is my code (Simulation for washing machine,second package)
package Timer;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Simulation extends JPanel implements ActionListener{
JFrame frame = new JFrame("Simulation and Timer");
Timer timer = new Timer(500, this);
int startAngle = 0;
int shift =5;
public Simulation(){
timer.start();
frame.add(this);
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillOval(87,90,275,275);
g.setColor(new Color(0,255,255));
g.fillOval( 100,100 ,250,250 );
g.setColor(Color.white);
g.fillOval(275,120,20,20);
}
public void actionPerformed(ActionEvent e){
}
}
package Timer;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
import java.awt.*;
public class Countdown {
JFrame frame = new JFrame("Countdown Clock");
JLabel counter_label = new JLabel("");
Timer timer;
private int second = 0;
private int minute = 40;
String ddSecond;
String ddMinute;
DecimalFormat dFormat = new DecimalFormat("00");
Font font = new Font("Arial",Font.BOLD,20);
public static void main(String[] args) {
new Countdown();
}
public Countdown() {
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
counter_label.setBounds(120, 100,50 ,50 );
counter_label.setHorizontalAlignment(JLabel.CENTER);
counter_label.setFont(font);
frame.setBackground(Color.black);
frame.add(counter_label);
frame.setVisible(true);
counter_label.setText("40:00");
countdownTimer();
timer.start();
}
public void countdownTimer() {
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
second--;
ddSecond = dFormat.format(second);
ddMinute = dFormat.format(minute);
counter_label.setText(ddMinute + ":" + ddSecond);
if(second==-1) {
second = 59;
minute--;
ddSecond = dFormat.format(second);
ddMinute = dFormat.format(minute);
counter_label.setText(ddMinute + ":" + ddSecond);
}
if(minute==0 && second==0 ) {
timer.stop();
}
}
});
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
