السلام عليكم ورحمة الله وبركاته ....
كل عام وانتم بخير
لو سمحتو اريد استخدام ال timer لرسم drawOval حسب الكود التالي
//imports for TimerExample class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
class TimerExample extends JFrame implements ActionListener {
private JPanel container;
JLabel labelCounter;
private Timer timer1 = new Timer(250, this);
int w,x,y,z = 1;
JButton Button1=new JButton("button 1");
JButton Button2=new JButton("button 2");
public TimerExample() {
//set initial delay to 1000 milliseconds
setLayout(new BorderLayout());
timer1.setInitialDelay(1150);
//initialize window
container = new JPanel();
this.add(container);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,400);
this.setVisible(true);
//start timer
Button1.addActionListener(this);
Button2.addActionListener(this);
add(Button1,BorderLayout.SOUTH);
add(Button2,BorderLayout.EAST);
timer1.start();
}
/**
* when timer begins this method draws ovals that increase in size
* and descend vertically down the window
*/
public void actionPerformed(ActionEvent e) {
if (z < 100){
Graphics g = container.getGraphics();
g.drawOval(w,x,y,z);
w = w+2;
x = x+2;
y = y+2;
z = z+2;
}
else //stop timer (and drawing) when z coordinate is greater than 99
timer1.stop();
}
}
public class TimerMain {
//main function instantiates TimerExample object
public static void main(String[] args){
TimerExample display = new TimerExample();
}
}وهذا شغال وتمام ولكن عندما اعدل على الactionPerformed بحيث يكون عندي زر يتحكم بعمل ال timer لا يعمل
لماذا ؟؟؟
كيف اجعله خاص ب event معين
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Button1){
JOptionPane.showMessageDialog(null,"button 1 is press");
}
if(e.getSource()==Button2){
if (z < 100){
Graphics g = container.getGraphics();
g.drawOval(w,x,y,z);
w = w+2;
x = x+2;
y = y+2;
z = z+2;
}
else //stop timer (and drawing) when z coordinate is greater than 99
timer1.stop();
}
}