ICS 201 مساعده في الـ actionPerformed & Graphics !!

الموضوع في 'علوم الحاسب الآلي' بواسطة Y-O-U, بتاريخ ‏13 ابريل 2012.

  1. Y-O-U

    Y-O-U عضو

    انضم:
    ‏10 أغسطس 2009
    المشاركات:
    312
    التقييمات:
    +0 / 0 / -0
    السلام عليكم

    هذا الكود فيه مشكله لما اضغط Smile ما يبتسم
    بس لما اشيل ال if statment في actionPerformed يبتسم !! كيف اقدر احل المشكله ؟؟
    لأني برضوا لازم اضيف اكشن ثانيه للنظارات :hahaha:

    كود:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import javax.imageio.*;
    import java.io.*;
    
    public class lab8_ex2 extends JFrame {
        public static final int frame_w = 400;
        public static final int frame_h = 400;
    
        public static final int face_diam = 200;
        public static final int face_x = 100;
        public static final int face_y = 100;
    
        public static final int eye_w = 20;
        public static final int eye_h = 10;
        public static final int reye_x = face_x + 55;
        public static final int reye_y = face_y + 60;
        public static final int leye_x = face_x + 130;
        public static final int leye_y = face_y + 60;
    
        public static final int mouth_w = 100;
        public static final int mouth_h = 25;
        public static final int mouth_x = face_x + 50;
        public static final int mouth_y = face_y + 100;
        public static final int mouth_start = 0;
        public static final int mouth_sweep = 180;
        
        boolean smile = false;
        boolean grimace = false;
        public lab8_ex2()
        {
            super("Face");
            setSize(frame_w,frame_h);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new BorderLayout());
            
            JPanel panel = new JPanel();
            panel.setLayout(new FlowLayout());
            add(panel,BorderLayout.SOUTH);
            
            JButton smile = new JButton("Smile");
            smile.addActionListener(new changerFace());
            panel.add(smile);
            
            JButton glass = new JButton("Glass on/off");
            panel.add(glass);
    		setVisible(true); 
        }
        public void paint(Graphics g){
    		super.paint(g);
    		g.drawOval(face_x, face_y, face_diam, face_diam);
    		g.fillOval(reye_x, reye_y, eye_w, eye_h);
    		g.fillOval(leye_x, leye_y, eye_w, eye_h);
    		if(smile)
    			g.drawArc(mouth_x, mouth_y, mouth_w, mouth_h, mouth_start, mouth_sweep*-1);
    		else	
    			g.drawArc(mouth_x, mouth_y, mouth_w, mouth_h, mouth_start, mouth_sweep);
        }
        
    	class changerFace implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			[COLOR="Red"]if(event.equals("Smile")){[/COLOR]
    				smile = true;
    				repaint();
    			[COLOR="red"]}[/COLOR]
    		}
    	}
        
        public static void main(String[] args) {
            new lab8_ex2();
        }   
    	
    }
    
    الكود الثاني في الجرافيكس
    لما اضغط Progress تطلع دائره لما اضغطه مره ثانيه تنمسح الاولى و تطلع الثانيه
    بس انا ابيه تطلع كلهم مع بعض و ما تنمسح الا اذا طلعت اربع دوائر .. بعدين تبدأ من جديد

    كود:
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import javax.imageio.*;
    import java.awt.event.*;
    
    public class lab8_ex3 extends JFrame{
    	
    	public static final int width = 400;
    	public static final int high = 300;
    	
    	public static final int radius = 50;
    	
    	// to check if the button is hitted
    	boolean hit = false;
    	// to know how many circle we draw
    	int number = 0;
    
    	public lab8_ex3(){
    		// set the title
    		super("Circles");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		// set the size of the window
    		setSize(width,high);
    		// creating a button
    		JButton progress = new JButton("Progress");
    		// add action to the button
    		progress.addActionListener(new Changer());
    		// set the button in the south of the window
    		setLayout(new BorderLayout());
    		// creating panel
    		JPanel panel = new JPanel();
    		panel.setLayout(new FlowLayout());
    		panel.add(progress);
    		add(panel,BorderLayout.SOUTH);
    		
    		
    		setVisible(true);
    	}
    	public void paint(Graphics g){
    		super.paint(g);
    		g.fillOval(radius,radius,radius,radius);
    		if(hit){
    			// loop for general application
    				// the condition we want is to check the even numbers
    				if(!(number%2==0))
    					g.drawOval(radius+number*75,radius,radius+number*20,radius+number*20);
    				else
    					g.fillOval(radius+number*75,radius,radius+number*20,radius+number*20);
    		}
    		
    			
    	}
    	
    	class Changer implements ActionListener{
    		public void actionPerformed(ActionEvent e){
    			if(e.equals("Progress")){
    				// incrementing numbers of circles
    				number++;
    				// to return the number to zero when numbers of circles get to maximum
    				number = number%4;
    				hit = true;
    				repaint();
    			}
    		}
    	}
    	
    	
    	public static void main(String[]args){
    		new lab8_ex3();
    	}
    	
    }
     
  2. mho000

    mho000 عضو

    انضم:
    ‏3 يونيو 2010
    المشاركات:
    198
    التقييمات:
    +0 / 0 / -0
    وعليكم السلام ورحمة الله وبركاته

    في الكود الأول أنت بتقارن ActionEvent مع String النتيجة أنه أبدًا ما يتساوى الأثنين

    التصحيح:
    كود:
    if(event.getActionCommand().equals("Smile"))
    getActionCommand() ترجع String المطبوع على الـ JButton

    في الثاني repaint بتمسح المرسوم بعدين تسوي paint
    استعمل for loop عشان تعيد رسم الدوائر اللي تنمسح

    كود:
    for(int i = 0; i <= number; i++)
                {
                    if(!(i%2==0))
                        g.drawOval(radius+i*75,radius,radius+i*20,radius+i*20);
                    else
                        g.fillOval(radius+i*75,radius,radius+i*20,radius+i*20);
                }
    وزي الأول:

    كود:
    if(e.getActionCommand().equals("Progress"))
     
جاري تحميل الصفحة...
مواضيع شبيهة - ICS 201 مساعده في الـ actionPerformed Graphics
  1. izelude
    الردود:
    6
    المشاهدات:
    1,179
  2. izelude
    الردود:
    0
    المشاهدات:
    623
  3. BRAV3
    الردود:
    3
    المشاهدات:
    18,917
  4. تقييم
    الردود:
    0
    المشاهدات:
    1,408
  5. بوشخاط
    الردود:
    1
    المشاهدات:
    971
  6. Nevermind7
    الردود:
    0
    المشاهدات:
    282
  7. sa2904
    الردود:
    5
    المشاهدات:
    1,532
  8. A_M_J
    الردود:
    0
    المشاهدات:
    1,371
  9. Abd ElRazzaq
    الردود:
    0
    المشاهدات:
    828
  10. أحمد شواهنه
    الردود:
    2
    المشاهدات:
    1,838

مشاركة هذه الصفحة