الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

timer

بدأه ريحانة الاقصى في 31 أغسطس 2011 · 11 رد · 655 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته ....

كل عام وانتم بخير

لو سمحتو اريد استخدام ال 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();
}
}
#2

وعليكم السلام ورحمة الله وبركاته

لكي تتحكم بال timer يجب أن تستخدم الدالتين start و stop

if(e.getSource()==Button1){
        timer1.stop();
	}
       if(e.getSource()==Button2){
        timer1.start(); 
			}

للصداقة البرمجية

pro-vb6@hotmail.com

#3
أبو فهيد كتب:

وعليكم السلام ورحمة الله وبركاته

لكي تتحكم بال timer يجب أن تستخدم الدالتين start و stop

if(e.getSource()==Button1){
        timer1.stop();
	}
       if(e.getSource()==Button2){
        timer1.start(); 
			}

جربت استخدمهم ما مشي الحال وما اتغير شي

#5
//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;
 Timer timer1 = new Timer(250, this);
int w,x,y,z = 1;
JButton Button1=new JButton("Stop");
JButton Button2=new JButton("Start");

public TimerExample() {
//set initial delay to 1000 milliseconds
setLayout(new BorderLayout());


//Timer t=new Timer(1000, this);
//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(e.getSource()==Button1){
        timer1.stop();
        }
       if(e.getSource()==Button2){
        timer1.start(); 
                        }

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();
}
}

الكود ده بيتحكم ف الرسم وفيه زرارين stop و start لبدء الرسم او ايقافه

تم تعديل هذه المشاركة بواسطة engaway في 31 أغسطس 2011 في 18:27

1
send01.jpg

Be optimistic

#6
engaway كتب:

//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;
 Timer timer1 = new Timer(250, this);
int w,x,y,z = 1;
JButton Button1=new JButton("Stop");
JButton Button2=new JButton("Start");

public TimerExample() {
//set initial delay to 1000 milliseconds
setLayout(new BorderLayout());


//Timer t=new Timer(1000, this);
//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(e.getSource()==Button1){
        timer1.stop();
        }
       if(e.getSource()==Button2){
        timer1.start(); 
                        }

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();
}
}

الكود ده بيتحكم ف الرسم وفيه زرارين stop و start لبدء الرسم او ايقافه

شكرا جزيلا

ولكن هل لي بسوال اخر اذا امكن

كيف استطيع جعله في البداية يقوم بعمل ويعد الفترة المحددة يقوم بعمل اخرلى سبيل المثال

في البداية يرسم مربع وبعد 2 تانية يرسم مستطيل وبعد نفس المدة يرسم دائرة وهكذا

#7
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;
 Timer timer1 = new Timer(250, this);
int w,x,y,z = 1;
JButton Button1=new JButton("Stop");
JButton Button2=new JButton("Start");

public TimerExample() {
//set initial delay to 1000 milliseconds
setLayout(new BorderLayout());


//Timer t=new Timer(1000, this);
//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);


}
/**
* when timer begins this method draws ovals that increase in size
* and descend vertically down the window
*/
    @Override
public void actionPerformed(ActionEvent e) {



if(e.getSource()==Button1){
        timer1.stop();
        }
       if(e.getSource()==Button2){
        timer1.start();  
       }

 if(z<50) //stop timer (and drawing) when z coordinate is greater than 99
{
    Graphics g = container.getGraphics();

    g.drawRect(w,x,y,z);

    w = w+2;
    x = x+2;
    y = y+2;
    z = z+2;

} 
 else
 { 
     Graphics g = container.getGraphics();

    g.drawArc(w, x, y, z, 360, 360);
   // super.paintComponents(g);
    w = w+2;
    x = x+2;
    y = y+2;
    z = z+2;
}

    }
}

public class TimerMain {
//main function instantiates TimerExample object
public static void main(String[] args){
TimerExample display = new TimerExample();
}
}

هو ده قصدك ؟؟؟؟؟؟؟؟؟؟؟

1
send01.jpg

Be optimistic

#8
engaway كتب:

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;
 Timer timer1 = new Timer(250, this);
int w,x,y,z = 1;
JButton Button1=new JButton("Stop");
JButton Button2=new JButton("Start");

public TimerExample() {
//set initial delay to 1000 milliseconds
setLayout(new BorderLayout());


//Timer t=new Timer(1000, this);
//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);


}
/**
* when timer begins this method draws ovals that increase in size
* and descend vertically down the window
*/
    @Override
public void actionPerformed(ActionEvent e) {



if(e.getSource()==Button1){
        timer1.stop();
        }
       if(e.getSource()==Button2){
        timer1.start();  
       }

 if(z<50) //stop timer (and drawing) when z coordinate is greater than 99
{
    Graphics g = container.getGraphics();

    g.drawRect(w,x,y,z);

    w = w+2;
    x = x+2;
    y = y+2;
    z = z+2;

} 
 else
 { 
     Graphics g = container.getGraphics();

    g.drawArc(w, x, y, z, 360, 360);
   // super.paintComponents(g);
    w = w+2;
    x = x+2;
    y = y+2;
    z = z+2;
}

    }
}

public class TimerMain {
//main function instantiates TimerExample object
public static void main(String[] args){
TimerExample display = new TimerExample();
}
}

هو ده قصدك ؟؟؟؟؟؟؟؟؟؟؟

شكرا جزيلا

#9

لا العفو ع ايه المهم انه هو ده قصدك

وممكن تتعمل بطريقة تانية ان احنا نعمل array of shapes وبالتايمر كل شوية شكل جديد يترسم

send01.jpg

Be optimistic

#10
engaway كتب:

لا العفو ع ايه المهم انه هو ده قصدك

وممكن تتعمل بطريقة تانية ان احنا نعمل array of shapes وبالتايمر كل شوية شكل جديد يترسم

كيف يعني array of shape

ممكن توضح قصدك بمثال اذا ما فيها غلبة

#11

مثــــــــال

الرابط ده ف مثال ع الطريقة

تم تعديل هذه المشاركة بواسطة engaway في 2 سبتمبر 2011 في 00:31

1
send01.jpg

Be optimistic

#12
engaway كتب:

مثــــــــال

الرابط ده ف مثال ع الطريقة

شكرا جزيلا

طبقت ما اريده وفقا للرابط

مواضيع مشابهة