Results 1 to 4 of 4
  1. #1
    lauren is offline Member
    Join Date
    Dec 2015
    Posts
    5
    Rep Power
    0

    Default Turning an applet into a Jframe

    We have to create a program where the cursor follow a pair of eyes when in the window, when the cursor is outside the window the eyes look straight ahead. In the book it asks us to make it an applet, which I did and my code is below. However my professor wants us to make it a JFrame instead, which is where I am lost. Thanks guys! I'm new to the website and new to Java just need it for my major.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class WatchMe extends JApplet
    {
       private final int EYES_AHEAD = 0;
       private final int EYES_UP = 1;
       private final int EYES_DOWN = 2;
       private final int EYES_LEFT = 3;
       private final int EYES_RIGHT = 4;
       
       private final int LEFT_EYE_X = 50, LEFT_EYE_Y = 75;
       private final int RIGHT_EYE_X = 100, RIGHT_EYE_Y = 75;
       private final int EYE_WIDTH = 50, EYE_HEIGHT = 50;
       
       private final int LEFT_PUPIL_AHEAD_X =65, LEFT_PUPIL_AHEAD_Y = 90;
       private final int RIGHT_PUPIL_AHEAD_X = 115, RIGHT_PUPIL_AHEAD_Y = 90;
       private final int LEFT_PUPIL_DOWN_X = 65, LEFT_PUPIL_DOWN_Y = 100;
       private final int RIGHT_PUPIL_DOWN_X = 115, RIGHT_PUPIL_DOWN_Y = 100;
       private final int LEFT_PUPIL_UP_X = 65, LEFT_PUPIL_UP_Y = 80;
       private final int RIGHT_PUPIL_UP_X = 115, RIGHT_PUPIL_UP_Y = 80;
       private final int LEFT_PUPIL_RIGHT_X = 75, LEFT_PUPIL_RIGHT_Y = 90;
       private final int RIGHT_PUPIL_RIGHT_X = 125, RIGHT_PUPIL_RIGHT_Y = 90;
       private final int LEFT_PUPIL_LEFT_X = 55, LEFT_PUPIL_LEFT_Y = 90;
       private final int RIGHT_PUPIL_LEFT_X = 105, RIGHT_PUPIL_LEFT_Y = 90;
       
       private final int PUPIL_WIDTH = 20, PUPIL_HEIGHT = 20;
       private int eyeState = EYES_AHEAD;
       
       public void init()
       {
          getContentPane().setBackground(Color.white);
          addMouseMotionListener(new MyMouseMotionListener());
       }
       
       public void paint(Graphics g)
       {
          super.paint(g);
          drawEyes(g);
          
          if (eyeState == EYES_AHEAD)
             drawPupilsAhead(g);
          else if (eyeState == EYES_UP)
             drawPupilsUp(g);
          else if (eyeState == EYES_DOWN)
             drawPupilsDown(g);
          else if (eyeState == EYES_LEFT)
             drawPupilsLeft(g);
          else if (eyeState == EYES_RIGHT)
             drawPupilsRight(g);
          else
             drawPupilsAhead(g);
       }
       
       public void drawEyes(Graphics g)
       {
          g.drawOval(LEFT_EYE_X, LEFT_EYE_Y, EYE_WIDTH, EYE_HEIGHT);
          g.drawOval(RIGHT_EYE_X, RIGHT_EYE_Y, EYE_WIDTH, EYE_HEIGHT);
       }
       
       public void drawPupilsAhead(Graphics g)
       {
          g.fillOval(LEFT_PUPIL_AHEAD_X, LEFT_PUPIL_AHEAD_Y, PUPIL_WIDTH, PUPIL_HEIGHT);
          g.fillOval(RIGHT_PUPIL_AHEAD_X, RIGHT_PUPIL_AHEAD_Y, PUPIL_WIDTH, PUPIL_HEIGHT);
       }
       
       public void drawPupilsDown(Graphics g)
       {
          g.fillOval(LEFT_PUPIL_DOWN_X, LEFT_PUPIL_DOWN_Y, PUPIL_WIDTH, PUPIL_HEIGHT);
          g.fillOval(RIGHT_PUPIL_DOWN_X, RIGHT_PUPIL_DOWN_Y, PUPIL_WIDTH, PUPIL_HEIGHT);
       }
       
       public void drawPupilsUp(Graphics g)
       {
          g.fillOval(LEFT_PUPIL_UP_X, LEFT_PUPIL_UP_Y, PUPIL_WIDTH, PUPIL_HEIGHT);
          g.fillOval(RIGHT_PUPIL_UP_X, RIGHT_PUPIL_UP_Y, PUPIL_WIDTH, PUPIL_HEIGHT);
       }
       
       public void drawPupilsRight(Graphics g)
       {
          g.fillOval(LEFT_PUPIL_RIGHT_X, LEFT_PUPIL_RIGHT_Y, PUPIL_WIDTH, PUPIL_HEIGHT);
          g.fillOval(RIGHT_PUPIL_RIGHT_X, RIGHT_PUPIL_RIGHT_Y, PUPIL_WIDTH, PUPIL_HEIGHT);
       }
       
       public void drawPupilsLeft(Graphics g)
       {
          g.fillOval(LEFT_PUPIL_LEFT_X, LEFT_PUPIL_LEFT_Y, PUPIL_WIDTH, PUPIL_HEIGHT);
          g.fillOval(RIGHT_PUPIL_LEFT_X, RIGHT_PUPIL_LEFT_Y, PUPIL_WIDTH, PUPIL_HEIGHT);
       }
       
       private class MyMouseMotionListener extends MouseMotionAdapter
       {
          public void mouseMoved(MouseEvent e)
          {
             int x = e.getX(), y = e.getY();
             
             if (y < RIGHT_EYE_Y)
                eyeState = EYES_UP;
             else if (y > (RIGHT_EYE_Y + EYE_HEIGHT))
                eyeState = EYES_DOWN;
             else if (x < LEFT_EYE_X)
                eyeState = EYES_LEFT;
             else if (x> (RIGHT_EYE_X + EYE_WIDTH))
                eyeState = EYES_RIGHT;
             
             repaint();
          }
       }
    }
    Last edited by lauren; 12-14-2015 at 09:00 PM.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: Turning an applet into a Jframe

    1.instead of extending JApplet use JFrame

    2.introduce main method like this abelow and create default constructor

    public static void main(String[] args) {
    new WatchMe();
    }
    3.change your init method to default constructor and do like this below

    public WatchMe()
    {

    setVisible(true);
    //setSize(800, 600);
    getContentPane().setBackground(Color.white);
    repaint();
    addMouseMotionListener(new MyMouseMotionListener());
    }
    Ramya:cool:

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    32

    Default Re: Turning an applet into a Jframe

    Please edit your post and wrap your code with code tags:
    [code]
    **YOUR CODE GOES HERE**
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    lauren is offline Member
    Join Date
    Dec 2015
    Posts
    5
    Rep Power
    0

    Default Re: Turning an applet into a Jframe

    I edited it, did I do what you meant?

Similar Threads

  1. Run JFrame programm as applet
    By thomason93 in forum Java Applets
    Replies: 0
    Last Post: 05-29-2014, 03:26 PM
  2. How would I add an applet into a jframe
    By Wesley.laferriere in forum New To Java
    Replies: 4
    Last Post: 12-11-2012, 09:35 PM
  3. JFrame to Applet
    By utsav1995 in forum New To Java
    Replies: 2
    Last Post: 05-31-2012, 04:41 PM
  4. JFrame to Applet?
    By Alerhau in forum New To Java
    Replies: 27
    Last Post: 08-14-2011, 02:38 AM
  5. JFrame vs Applet
    By baltimore in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 03:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •