1 2 Previous Next 20 Replies Latest reply: Aug 23, 2008 1:59 PM by 800636 Go to original post RSS
      • 15. Re: making moving eyes in java
        mlk
        mintoo2cool wrote:
        sorry to waste ur time and energy
        If you take my comment, learn from it and go away and give each variable a meaningful name, format your code nicely and generally just make sure you code is more readable then you have not wasted my time. I frequent these forums to help people learn Java, and learning to keep code readable is an important lesson.

        If however you don't go away with that message I would not worry too much, I've not spent all that long on your post. :)
        • 16. Re: making moving eyes in java
          800636
          mlk wrote:
          mintoo2cool wrote:
          sorry to waste ur time and energy
          If you take my comment, learn from it and go away and give each variable a meaningful name, format your code nicely and generally just make sure you code is more readable then you have not wasted my time. I frequent these forums to help people learn Java, and learning to keep code readable is an important lesson.

          If however you don't go away with that message I would not worry too much, I've not spent all that long on your post. :)
          no no...give me 2-3days and i will be back with a better code...i m learning more than just "moving the eyeballs" from here...

          @yawmark ROFL! i was asking for it!! :D

          EDIT:i verified the second code that i posted yesterday, just now, at a friends house...

          (i posted the new version of the code, without any verification,as i dont have java right now, because i dont have a harddisk right now (thats another story) and i m coming to the forum on an ubuntu live cd...)

          and it has some variable declaration errors, after resolving the errors i executed the code and its "almost" working, the eyeball is staying within the eye and along with the mouse...but only in the 2nd and 3rd quardrant...

          in the other 2 quadrants, its still within the eye but if the mouse pointer is on the top of the page then the eyeball is seeing the bottom...i think i know what the problem is...

          i wish i could post the screenshots....

          but nonetheless i will change the code appropriately in accordance with the java coding conventions,as its really important...and hopefully you guys would help me create an easier version of the code...

          Edited by: mintoo2cool on May 29, 2008 2:23 AM
          • 17. Re: making moving eyes in java
            mlk
            i wish i could post the screenshots....
            If you really feel screen shots will help, use something like [imageshackhttp://imageshack.us/] and post a link to the image.

            Good luck.
            • 18. Re: making moving eyes in java
              800636
              Oi!
              I had forgotten about this thread...

              Well I successfully created the program, long ago...here is the code...
              /* Re-implementation of eyeball*/
              
              import java.awt.*;
              import java.awt.event.*;
              import java.applet.*;
              /*This class calculates the co-ordinates of Eyeball*/
              class Eyeball 
              {
                   double xm,ym,x1,x2,y1,y2,xc,yc,radius;
                   int x_coord,y_coord;     
                   /*Get the co-ordinates of the eye,use this to generate the eye-ball*/     
                   public void geteyes(int x,int y,int rad)
                   {
                        xc=x;
                        yc=y;
                        radius=rad;
                   }
                   /*Get the co-ordinates of mouse,use this to generate the eye-ball*/     
                   public void getmouse(int x,int y)
                   {
                        xm=x;
                        ym=y;
                   }
                   /*calculate the slope of the line joining the centre of the eye with the mouse co-ordinates*/
                   double slope(double xm,double ym,double xc,double yc)
                   {
                        double sl=(xm-xc)/(ym-yc);
                        return sl;
                   }
                   /*calculate the value of constant c in the quadratic equation ay2+by+c=0*/
                   double cvalue(double yc,double radius,double slope)
                   {
                        double exp1=radius*radius;
                        double exp2=slope*slope;
                        double exp3=yc*yc;
                        double cvalue=exp3-((exp1*exp2)/(exp2+1));
                        return      cvalue;
                   }
                   /*calculate the value of b in the quadratic equation ay2+by+c=0*/
                   double bvalue(double yc)
                   {
                        double bvalue=-1*2*yc;
                        return bvalue;
                   }
                   /*calculate the discriminant value of the quadratic equation ay2+by+c=0*/
                   double desc(double b,double c)
                   {
                        double exp1=b*b;
                        double exp2=4*c;
                        double desc=Math.sqrt(exp1-exp2);
                        return desc;
                   }
                   /*this is one of the 2 values of y when the quadratic equation is solved*/
                   double y1(double b,double desc)
                   {
                        double y=(b + desc)/2;
                        return y;
                   }
                   /*this is the second value of y when the quadratic equation is solved*/
                   double y2(double b,double desc)
                   {
                        double y=(b - desc)/2;
                        return y;
                   }
                   /*this gets the value of x corresponding to the value of y*/
                   double x(double y1,double sl,double yc,double xc)
                   {
                        double exp1=y1-yc;
                        double exp2=exp1/sl;
                        double x=exp2+xc;
                        return x;
                   }
                   /*use the methods and calculate the x and y values*/     
                   void calculations()
                   {
                        double m=slope(xm,ym,xc,yc);
                        double c=cvalue(yc,radius,m);
                        double b=bvalue(yc);
                        double desc=desc(b,c);
                        y1=y1(-b,desc);
                        y2=y2(-b,desc);
                        x1=x(y1,m,yc,xc);
                        x2=x(y2,m,yc,xc);
                   }
                   /*decide the co-ordinates form the 2 possible values of x and y*/     
                   void co_ordinates()
                   {
                        calculations();          
                        if(xm>xc)
                        {
                             x_coord=(int) x1;
                             y_coord=(int) y1;
                        }
                        else
                        {
                             x_coord=(int) x2;
                             y_coord=(int) y2;
                        }
                   }
                   /*pass the value of the x-coordinate*/     
                   public int give_eyeballx()
                   {
                        co_ordinates();          
                        return x_coord;
                   }
                   /*pass the value of the y-coordinate*/
                   public int give_eyebally()
                   {
                        co_ordinates();
                        return y_coord;
                   }
              }
              
              /*Applet Class*/
              public class Eyes extends Applet implements MouseMotionListener
              {
                   /*Objects*/     
                   Eyeball e1=new Eyeball(); 
                   Eyeball e2=new Eyeball();
                   /*Init Applet*/
                   public void init()
                   {
                        addMouseMotionListener(this);
                   }
                   /*MouseMoved*/     
                   public void mouseMoved(MouseEvent e)
                   {
                        int xm=e.getY();
                        int ym=e.getX();
                        /*Pass-values to Eyeball Objects*/          
                        e1.getmouse(xm,ym);
                        e2.getmouse(xm,ym);          
                        repaint();
                   }
                   public void mouseDragged(MouseEvent e){}
                   public void paint(Graphics g)
                   {
                        /*pass co-ordinates of centre of eyes and radius to eyeball objects*/
                        e1.geteyes(330,330,30);
                        e2.geteyes(260,330,30);     
                        /*draw the eyes*/          
                        g.drawOval(300,300,75,80);
                        g.drawOval(230,300,75,80);
                        /*draw the eyeballs*/          
                        g.fillOval(e1.give_eyeballx(),e1.give_eyebally(),20,20);
                        g.fillOval(e2.give_eyeballx(),e2.give_eyebally(),20,20);
                   }
              }
              
              /*<applet code="Eyes" height=800 width=600></applet>*/
              Can you people understand it yet? Or is it still un-readable?

              Here is the screenshot
              [http://img141.imageshack.us/img141/8153/screenshotzj8.png]
              This is what it was like at the start of this thread
              [http://img360.imageshack.us/img360/6429/screenshotcv8.png]

              Edited by: mintoo2cool on Aug 23, 2008 12:10 AM
              • 19. Re: making moving eyes in java
                Darryl Burke
                Pretty good :-) I would avoid using underscores and use camelCase instead. And method names like getY2, getX etc.

                In future, though, if you post code here, it's better to indent with 2 to 4 spaces, not tabs, as the forum software doesn't handle tabs very well.

                Your posted problem helped me while away boredom for some time, and this was the result. Enjoy!
                // FacePanel.java
                
                import java.awt.BasicStroke;
                import java.awt.Color;
                import java.awt.Graphics;
                import java.awt.Graphics2D;
                import java.awt.MultipleGradientPaint.CycleMethod;
                import java.awt.Point;
                import java.awt.RadialGradientPaint;
                import java.awt.RenderingHints;
                import java.awt.Shape;
                import java.awt.Stroke;
                import java.awt.event.InputEvent;
                import java.awt.event.MouseAdapter;
                import java.awt.event.MouseEvent;
                import java.awt.event.MouseWheelEvent;
                import java.awt.geom.Arc2D;
                import java.awt.geom.Ellipse2D;
                import java.awt.geom.Path2D;
                import java.awt.geom.Point2D;
                import java.awt.geom.Rectangle2D;
                import java.util.ArrayList;
                import java.util.List;
                import javax.swing.JFrame;
                import javax.swing.JOptionPane;
                import javax.swing.JPanel;
                import javax.swing.SwingUtilities;
                
                public class FacePanel extends JPanel {
                
                   private Point mouseDown,  mouseHere,  mouseClicked;
                   private Face faceToDrag;
                   private Ellipse2D.Double circle;
                   private Rectangle2D.Double rect;
                   private List<Face> faces = new ArrayList<Face>();
                
                   public FacePanel() {
                      FaceListener listener = new FaceListener();
                      addMouseListener(listener);
                      addMouseMotionListener(listener);
                      addMouseWheelListener(listener);
                
                      setBackground(Color.WHITE);
                      setFocusable(true);
                   }
                
                   @Override
                   public void paintComponent(Graphics g) {
                      super.paintComponent(g);
                      Graphics2D g2 = (Graphics2D) g;
                      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                              RenderingHints.VALUE_ANTIALIAS_ON);
                      for (Face face : faces) {
                         face.paint(g2);
                      }
                      if (rect != null) {
                         if (circle == null) {
                            g2.setColor(Color.RED);
                         } else {
                            g2.draw(circle);
                         }
                         g2.setStroke(new BasicStroke(1.0f,
                                 BasicStroke.CAP_BUTT,
                                 BasicStroke.JOIN_BEVEL,
                                 1.0f,
                                 new float[] {0.8f, 6.0f},
                                 0.0f));
                         g2.draw(rect);
                      }
                   }
                
                   public void makeUI() {
                      JFrame frame = new JFrame("Faces");
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      frame.setSize(600, 400);
                      frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                      frame.add(this);
                      frame.setVisible(true);
                
                      String[] instructions = {"Drag the mouse diagonally to draw a face.",
                      "Move the mouse and the eyes follow.",
                      "Click and the nose points at the mouse.",
                      "Double click to change the mouth.",
                      "Drag the face around with the mouse.",
                      "Use the scroll wheel to change the size.",
                      "Right click to delete a face.",
                      "",
                      "Draw as many faces as you like!"
                      };
                      JOptionPane.showMessageDialog(frame, instructions);
                   }
                
                   public static void main(String[] args) {
                      SwingUtilities.invokeLater(new Runnable() {
                
                         @Override
                         public void run() {
                            new FacePanel().makeUI();
                         }
                      });
                   }
                
                   private class FaceListener extends MouseAdapter {
                
                      @Override
                      public void mousePressed(MouseEvent e) {
                         if (e.getButton() == MouseEvent.BUTTON1) {
                            mouseDown = e.getPoint();
                            faceToDrag = getFaceAt(mouseDown);
                            if (faceToDrag != null) {
                               faceToDrag.setOffset(mouseDown);
                            }
                         }
                      }
                
                      @Override
                      public void mouseDragged(MouseEvent e) {
                         if ((e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) == 0) {
                            return;
                         }
                
                         mouseHere = e.getPoint();
                         if (faceToDrag != null) {
                            faceToDrag.setLocation(mouseHere);
                         } else {
                            double downX = mouseDown.getX();
                            double downY = mouseDown.getY();
                            double hereX = mouseHere.getX();
                            double hereY = mouseHere.getY();
                
                            double l = Math.min(downX, hereX);
                            double t = Math.min(downY, hereY);
                            double w = Math.abs(downX - hereX);
                            double h = Math.abs(downY - hereY);
                            rect = new Rectangle2D.Double(l, t, w, h);
                
                            double dia = Math.min(w, h);
                            if (dia >= Face.MIN_SIDE) {
                               l = downX < hereX ? downX : downX - dia;
                               t = downY < hereY ? downY : downY - dia;
                               circle = new Ellipse2D.Double(l, t, dia, dia);
                            } else {
                               circle = null;
                            }
                         }
                         repaint();
                      }
                
                      @Override
                      public void mouseReleased(MouseEvent e) {
                         faceToDrag = null;
                         if (circle != null) {
                            if (mouseClicked == null) {
                               mouseClicked = new Point((int) circle.getCenterX(),
                                       (int) circle.getCenterY());
                            }
                            faces.add(new Face(circle));
                         }
                         circle = null;
                         rect = null;
                         repaint();
                      }
                
                      @Override
                      public void mouseMoved(MouseEvent e) {
                         mouseHere = e.getPoint();
                         repaint();
                      }
                
                      @Override
                      public void mouseClicked(MouseEvent e) {
                         Point p = e.getPoint();
                         switch (e.getButton()) {
                            case MouseEvent.BUTTON1:
                               switch (e.getClickCount()) {
                                  case 1:
                                     mouseClicked = p;
                                     break;
                                  case 2:
                                     for (int i = faces.size() - 1; i >= 0; i--) {
                                        if (faces.get(i).
                                                contains(p)) {
                                           faces.get(i).
                                                   changeMouth();
                                           break;
                                        }
                                     }
                                     break;
                               }
                               break;
                            case MouseEvent.BUTTON3:
                               Face face = getFaceAt(p);
                               if (face != null) {
                                  faces.remove(face);
                               }
                               break;
                         }
                         repaint();
                      }
                
                      @Override
                      public void mouseWheelMoved(MouseWheelEvent e) {
                         Point p = e.getPoint();
                         Face face = getFaceAt(p);
                         if (face != null) {
                            face.setOffset(p);
                            face.setSize(e.getUnitsToScroll());
                            repaint();
                         }
                      }
                
                      private Face getFaceAt(Point p) {
                         Face faceAtP = null;
                         for (int i = faces.size() - 1; i >= 0; i--) {
                            if (faces.get(i).
                                    contains(p)) {
                               faceAtP = faces.get(i);
                               faces.remove(faceAtP);
                               faces.add(faceAtP);
                               break;
                            }
                         }
                         return faceAtP;
                      }
                   }
                
                   private class Face extends Ellipse2D.Double {
                
                      static final int MIN_SIDE = 30;
                      final static int MOUTH_TYPES = 5;
                      int mouthType;
                      Point offset = new Point();
                      Graphics2D g2;
                
                      public Face(Ellipse2D.Double circle) {
                         super(circle.x, circle.y, circle.width, circle.height);
                      }
                
                      public void setOffset(Point p) {
                         offset.x = p.x - (int) x;
                         offset.y = p.y - (int) y;
                      }
                
                      public void setLocation(Point p) {
                         x = p.x - offset.x;
                         y = p.y - offset.y;
                      }
                
                      public void setSize(int increment) {
                         double ratioX = offset.x / width;
                         double ratioY = offset.y / height;
                
                         width = Math.max(MIN_SIDE, width - increment);
                         height = width;
                
                         x += offset.x - ratioX * width;
                         y += offset.y - ratioY * height;
                      }
                
                      public void changeMouth() {
                         mouthType++;
                         mouthType %= MOUTH_TYPES;
                      }
                
                      public void paint(Graphics2D g) {
                         g2 = g;
                
                         Point2D.Double noseCenter = new Point2D.Double(getCenterX(),
                                 getCenterY());
                         float[] floats = {0.1f, 0.3f, 0.6f};
                         Color[] colors = {Color.WHITE, Color.YELLOW, Color.ORANGE};
                         RadialGradientPaint paint = getPaint(this, noseCenter, floats, colors);
                         g2.setPaint(paint);
                         g2.fill(this);
                         g2.setColor(Color.BLACK);
                         g2.draw(this);
                
                         double dia = width * 0.27;
                         double top = y + getHeight() * 0.16;
                         double left = getCenterX() - dia * 1.3;
                         paintEye(left, top, dia);
                
                         left = getCenterX() + dia * 0.3;
                         paintEye(left, top, dia);
                
                         left = noseCenter.x - dia * 0.5;
                         top = noseCenter.y - dia * 0.5;
                         paintNose(left, top, dia);
                
                         paintMouth();
                      }
                
                      private void paintEye(double left, double top, double dia) {
                         Ellipse2D.Double eye = new Ellipse2D.Double(left, top, dia, dia);
                         Point2D.Double focus = getOffCenter(eye, mouseHere);
                         float[] floats = {0.1f, 0.25f, 1.0f};
                         Color[] colors = {Color.WHITE, Color.LIGHT_GRAY.brighter(),
                            Color.LIGHT_GRAY
                         };
                         RadialGradientPaint paint = getPaint(eye, focus, floats, colors);
                         g2.setPaint(paint);
                         g2.fill(eye);
                         g2.setColor(Color.BLACK);
                         g2.draw(eye);
                
                         Shape clip = g2.getClip();
                         g2.setClip(eye);
                
                         Ellipse2D iris = new Ellipse2D.Double(focus.x - dia * 0.3,
                                 focus.y - dia * 0.3, dia * 0.6, dia * 0.6);
                         g2.fill(iris);
                         iris = new Ellipse2D.Double(focus.x - dia * 0.3 - 2,
                                 focus.y - dia * 0.3 - 2, dia * 0.6 + 4, dia * 0.6 + 4);
                         g2.draw(iris);
                
                         g2.setClip(clip);
                      }
                
                      private void paintNose(double left, double top, double dia) {
                         Ellipse2D.Double nose = new Ellipse2D.Double(left, top, dia, dia);
                         Point2D.Double focus = getOffCenter(nose, mouseClicked);
                         float[] floats = {0.01f, 0.2f, 1.0f};
                         Color[] colors = {Color.WHITE, Color.RED, Color.RED.darker()};
                         RadialGradientPaint paint = getPaint(nose, focus, floats, colors);
                         g2.setPaint(paint);
                         g2.fill(nose);
                         g2.setColor(Color.BLACK);
                         g2.draw(nose);
                      }
                
                      public void paintMouth() {
                         Shape mouth = null;
                         double mouthX;
                         double mouthY;
                         double mouthW;
                         double mouthH;
                         double start;
                         double extent;
                         boolean fill = false;
                
                         switch (mouthType) {
                            case 0: // :)
                
                               mouthX = x + width * 0.2;
                               mouthY = y + height * 0.2;
                               mouthW = width * 0.6;
                               mouthH = height * 0.6;
                               start = 210;
                               extent = 120;
                               mouth = new Arc2D.Double(mouthX, mouthY, mouthW, mouthH,
                                       start, extent, Arc2D.OPEN);
                               break;
                            case 1: // :(
                
                               mouthX = x + width * 0.2;
                               mouthY = y + height * 0.7;
                               mouthW = width * 0.6;
                               mouthH = height * 0.5;
                               start = 30;
                               extent = 120;
                               mouth = new Arc2D.Double(mouthX, mouthY, mouthW, mouthH,
                                       start, extent, Arc2D.OPEN);
                               break;
                            case 2: // :D
                
                               mouthX = x + width * 0.25;
                               mouthY = y + height * 0.35;
                               mouthW = width * 0.5;
                               mouthH = height * 0.5;
                               start = 200;
                               extent = 140;
                               mouth = new Arc2D.Double(mouthX, mouthY, mouthW, mouthH,
                                       start, extent, Arc2D.CHORD);
                               fill = true;
                               break;
                            case 3: // :O
                
                               mouthX = x + width * 0.41;
                               mouthY = y + width * 0.69;
                               mouthW = width * 0.18;
                               mouthH = mouthW;
                               mouth = new Ellipse2D.Double(mouthX, mouthY, mouthW, mouthH);
                               fill = true;
                               break;
                            case 4: // :S
                
                               Path2D.Double path = new Path2D.Double();
                               path.moveTo(x + width * 0.25, y + height * 0.75);
                               path.curveTo(x + width * 0.44, y + height * 0.95,
                                       x + width * 0.56, y + height * 0.55,
                                       x + width * 0.75, y + height * 0.75);
                               mouth = path;
                         }
                
                         if (fill) {
                            g2.setColor(Color.WHITE);
                            g2.fill(mouth);
                         }
                         g2.setColor(Color.BLACK);
                         Stroke stroke = g2.getStroke();
                         g2.setStroke(new BasicStroke((float) height * 0.02f,
                                 BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                         g2.draw(mouth);
                         g2.setStroke(stroke);
                      }
                
                      private RadialGradientPaint getPaint(Ellipse2D.Double circle,
                              Point2D.Double focus, float[] floats, Color[] colors) {
                         Point2D.Double center = new Point2D.Double(circle.getCenterX(),
                                 circle.getCenterY());
                         return new RadialGradientPaint(center, (float) circle.width, focus,
                                 floats, colors, CycleMethod.NO_CYCLE);
                      }
                
                      private Point2D.Double getOffCenter(Ellipse2D.Double circle,
                              Point mousePoint) {
                         double circleX = circle.getCenterX();
                         double circleY = circle.getCenterY();
                         Point2D.Double circleCenter = new Point2D.Double(circleX, circleY);
                         double circleDia = circle.width;
                
                         Point2D.Double intersect = getIntersect(circleX, circleY, mousePoint);
                
                         double mouseX = mousePoint.getX();
                         double mouseY = mousePoint.getY();
                         Point2D.Double refPoint = new Point2D.Double(mouseX, mouseY);
                
                         double ratio = getDistance(circleCenter, refPoint) / getDistance(
                                 circleCenter, intersect);
                         double offCenter = circleDia * ratio * 0.4;
                
                         double distance = getDistance(circleCenter, refPoint);
                         if (distance == 0.0) {
                            return circleCenter;
                         }
                         double offsetX = circleX - offCenter * (circleX - mouseX) / distance;
                         double offsetY = circleY - offCenter * (circleY - mouseY) / distance;
                
                         return new Point2D.Double(offsetX, offsetY);
                      }
                
                      private double getDistance(Point2D.Double one, Point2D.Double two) {
                         return Math.sqrt(squareOf(one.x - two.x) + squareOf(one.y - two.y));
                      }
                
                      private double squareOf(double d) {
                         return d * d;
                      }
                
                      private Point2D.Double getIntersect(double centerX, double centerY,
                              Point mousePoint) {
                         double mouseX = mousePoint.getX();
                         double mouseY = mousePoint.getY();
                
                         if (centerX == mouseX && centerY == mouseY) {
                            return new Point2D.Double(0, 0);
                         }
                         if (centerX == mouseX) {
                            return new Point2D.Double(centerX, 0);
                         }
                         if (centerY == mouseY) {
                            return new Point2D.Double(0, centerY);
                         }
                
                         double w = FacePanel.this.getWidth();
                         double h = FacePanel.this.getHeight();
                         double intersectX,  intersectY;
                
                         if (mouseX < centerX) {
                            intersectY = centerY - (centerY - mouseY) * (centerX / (centerX -
                                    mouseX));
                         } else {
                            intersectY = centerY + (mouseY - centerY) * (w - centerX) /
                                    (mouseX - centerX);
                         }
                         intersectY = Math.min(Math.max(0, intersectY), h);
                
                         if (mouseY < centerY) {
                            intersectX = centerX - (centerX - mouseX) * (centerY / (centerY -
                                    mouseY));
                         } else {
                            intersectX = centerX + (mouseX - centerX) * (h - centerY) /
                                    (mouseY - centerY);
                         }
                         intersectX = Math.min(Math.max(0, intersectX), w);
                
                         return new Point2D.Double(intersectX, intersectY);
                      }
                   }
                }
                // Applet wrapper FaceApplet.java
                
                public class FaceApplet extends javax.swing.JApplet {
                   
                   @Override
                   public void init() {
                      add(new FacePanel());
                   }
                }
                db
                • 20. Re: making moving eyes in java
                  800636
                  Wow!!! I don't know what to say,except that I am humbled...Thats pretty creative and inspirational!! The Duke Points...they are yours!!

                  You should have seen my face when I compiled and ran your program!! LOL!!

                  I am going to try and do something similar too!! but not from your code, it will be different!! I will post it as soon as I am done...but I am kind of busy now and Java is not a part of my curriculum, I have UML and OS and networks lab now, we use a lot of C currently, its as shift back to structured programming, but now I am determined to finish of my course as soon as possible and get back to doing this and learning more java...

                  and thanks for the tips and encouragement !! and thanks for sharing that awesome program !! really appreciate it!!
                  :D
                  1 2 Previous Next