smiley face

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cazconv2007
    New Member
    • Feb 2007
    • 30

    smiley face

    Hi i have a homework on drawing a smiley face then pressing a button to draw a sad face i have some code but nothing works i keep getting an error on my action performed method. can you please look and give me some tips thanx
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.*;
    
    
    
    public class Face extends JFrame implements ActionListener 
    {
      
      FacePanel mmp = new FacePanel();
      private JButton smile = new JButton ("Smile");
      private JButton sad = new JButton ("Sad");
     
      
      public Face() 
      {
      	JPanel p1 = new JPanel();
      	 p1.setLayout(new GridLayout(4,2));
        add(mmp);
        JButton smile = new JButton("Smile");	
    		add(smile);						    
    		smile.addActionListener(this);		
    		
    		// The applet has a "Sad" button
    		JButton sad = new JButton("Sad");		
    		add(sad);
    		sad.addActionListener(this);
        
        setTitle("Smiley Face");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 400);
        setVisible(true);
      }
      
      public void actionPerformed(ActionEvent e);
      {
      	String cmd = e.getActionCommand();
    		
    		if (cmd.equals("Smile")) {			// "Smile" was pressed
    			SMILE = true;
    			setBackground(Color.yellow);
    			repaint();
    		}
    		else if (cmd.equals("Sad")) {		// "Sad" 
    				SMILE = false;
    				setBackground(Color.lightGray);
    				repaint();
    		}
    		
      if (smile) {
    			g.drawArc(70, 95, 60, 60, 225, 90);
    		} 
    		else {
    			g.drawArc(70, 145, 60, 60, 45, 90);
    		}
    		
    
    
      	
      }
      
       
     
     
    }
    
    
    public class FaceDriver
    {
      public static void main(String args[])
      {
        Face smiley = new Face();
        
      }
    }
    
    
    import java.awt.* ;
    import javax.swing.* ;
    
    class FacePanel extends JPanel
    {
      public void paintComponent(Graphics g)
      {
      	    //smiley face
            final int MID=150;
            final int TOP=50;
    
      	    g.setColor (Color.red);
            g.drawOval(100,25,175,175);
            g.setColor(Color.black);
            g.drawOval(135,75,25,25);
            g.setColor(Color.black);
            g.drawOval(200,75,25,25);
            g.drawArc(MID+10,TOP+100,60,20,190,175);
    
        
            
      }
    }
    Last edited by horace1; Mar 25 '07, 03:33 PM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you have a ; on the of the line
    Code:
      public void actionPerformed(ActionEvent e);
    which makes the compiler think there is no code for the method

    Comment

    • cazconv2007
      New Member
      • Feb 2007
      • 30

      #3
      i've changed that but still have an error it says that my face class is not abstract

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by cazconv2007
        i've changed that but still have an error it says that my face class is not abstract
        if I remove the ; and recompile the message about the abstract class goes away and I get a few more messages starting with
        Code:
        Face.java:42: cannot find symbol
        symbol  : variable SMILE
        location: class Face
                                SMILE = true;
                                ^

        Comment

        • mdsalmanhussain
          New Member
          • Apr 2013
          • 1

          #5
          the corrected one...

          Code:
          import javax.swing.*;
          import java.awt.*;
          import java.awt.event.*;
          import java.awt.event.*;
           
           
           
          class Face extends JFrame implements ActionListener 
          {
           int a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6;
            FacePanel mmp = new FacePanel();
            private JButton smile = new JButton ("Smile");
            private JButton sad = new JButton ("Sad");
           boolean smilee;
           
            public Face() 
            {
                JPanel p1 = new JPanel();
                 p1.setLayout(new GridLayout(4,2));
              add(mmp);
             // JButton smile = new JButton("Smile");    
                  add(smile);                            
                  smile.addActionListener(this);        
          		
                  // The applet has a "Sad" button
               //   JButton sad = new JButton("Sad");        
                  add(sad);
                  sad.addActionListener(this);
           
              setTitle("Smiley Face");
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setSize(600, 400);
              setVisible(true);
            }
           
            public void actionPerformed(ActionEvent e)
            {
                String cmd = e.getActionCommand();
           
                  if (cmd.equals("Smile")) {            // "Smile" was pressed
                      smilee = true;
                      setBackground(Color.yellow);
                      repaint();
                  }
                  else if (cmd.equals("Sad")) {        // "Sad" 
                        smilee = false;
                          setBackground(Color.lightGray);
                          repaint();
                  }
           
            if (smilee) {
                   //   g.drawArc(70, 95, 60, 60, 225, 90);
          			a1=70;a2=95;a3=a4=60;a5=225;a6=90;
          			
                  } 
                  else {
                      //g.drawArc(70, 145, 60, 60, 45, 90);
                      b1=70;b2=145;b3=b4=60;b5=45;b6=90;
                  }
          	}
           public void paint(Graphics g){
          		g.drawArc(a1,a2,a3,a4,a5,a6);
          		g.drawArc(b1,b2,b3,b4,b5,b6);
          	}
           
           
           
            }
           
           
           
           
          
           
           
          public class examplesmiley
          {
            public static void main(String args[])
            {
              Face smiley = new Face();
           
            }
          }
           
           
          
           
          class FacePanel extends JPanel
          {
            public void paintComponent(Graphics g)
            {
                    //smiley face
                  final int MID=150;
                  final int TOP=50;
           
                    g.setColor (Color.red);
                  g.drawOval(100,25,175,175);
                  g.setColor(Color.black);
                  g.drawOval(135,75,25,25);
                  g.setColor(Color.black);
                  g.drawOval(200,75,25,25);
                  g.drawArc(MID+10,TOP+100,60,20,190,175);
           
           
           
            }
          }
          Last edited by Rabbit; Apr 16 '13, 08:17 PM. Reason: Please use code tags when posting code.

          Comment

          Working...