0

Please help me how to make this eye move or to make it blink using repaint, thread and implements runnable. I don't know where to place the right codes to make it work. Please help me guys! Thank you! Here is the code:

import java.awt.*;
import java.applet.*;

public class Pucca extends Applet {

public Pucca(){
setSize(700, 700); }

//paint method
public void paint(Graphics g){

Color white = new Color(255,255,255);
g.setColor(white);
g.fillOval(600, 100, 125, 125); //left white fill eye

g.setColor(Color.BLA­CK);
g.drawOval(600, 100, 125, 125); // left big black line eye

g.setColor(white);
g.fillOval(700, 100, 125, 125); //right white fill eye

g.setColor(Color.BLA­CK);
g.drawOval(700, 100, 125, 125); //right big black line eye

Color blue = new Color(0, 160, 198);
g.setColor(blue);
g.fillOval(635, 130, 51, 51); // left blue fill eye

g.setColor(Color.BLA­CK);
g.drawOval(635, 130, 50, 50); // left black small line eye

g.setColor(blue);
g.fillOval(735, 130, 51, 51); // right blue fill eye

g.setColor(Color.BLA­CK);
g.drawOval(735, 130, 50, 50); // right black small line eye

g.setColor(Color.BLA­CK);
g.fillOval(650, 145, 20, 20); // left black iris 

g.setColor(Color.BLA­CK);
g.fillOval(750, 145, 20, 20); // right black iris

}
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Tiffany Lee
  • 13
  • 1
  • 4
  • Move requires you to make the elements you want to move variable, to blink, you need the ability to change state over time. Applets are also officially dead, the applet plugin has been deprecated and is no longer been supported – MadProgrammer Apr 05 '16 at 03:23
  • The first thing I would do is finger out which of those numerous values need to change in order to effect the desired results, making those values variable, then I'd worry about how to use threads to update those values – MadProgrammer Apr 05 '16 at 03:26
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson Apr 06 '16 at 01:41
  • .. 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Apr 06 '16 at 01:41

1 Answers1

1

When it comes to animation, everything becomes variable. You also have a lot of repeated code (seriously, if you can paint one eye, you can paint lots).

The first thing you need to is make all the values of the eye as variable as possible.

The follow makes the eye size and position variable and the iris and pupil a scaled value of the eye size, which makes the whole process simpler to animate.

Next, you need an updated loop, which can update the state of the values you want to change. To keep it simple, I've set it up so that the pupil has a variable offset, which is changed over time.

Eyes

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;

public class Pucca extends Applet {

    public Pucca() {
        setSize(700, 700);
        Thread t = new Thread(new Runnable() {
            private int xDelta = -1;
            private int yDelta = 0;
            private int blinkCount = 0;

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(40);
                    } catch (InterruptedException ex) {
                    }

                    xOffset += xDelta;
                    double irisSize = eyeSize.width * irisScale;
                    double range = ((eyeSize.width - irisSize) / 2);
                    if (xOffset <= -range) {
                        xOffset = -(int) range;
                        xDelta *= -1;
                    } else if (xOffset >= range) {
                        xOffset = (int) range;
                        xDelta *= -1;
                    }
                    blinkCount++;
                    if (blink && blinkCount > 10) {
                        blink = false;
                        blinkCount = 0;
                    } else if (blinkCount > 25) {
                        blink = true;
                        blinkCount = 0;
                    }
                    repaint();
                }
            }
        });
        t.setDaemon(true);
        t.start();
    }

    private boolean blink = false;

    private int xOffset, yOffset = 0;
    private Dimension eyeSize = new Dimension(125, 125);
    private Point left = new Point(20, 20);
    private Point right = new Point(left.x + 100, left.y);
    private double irisScale = 0.4;
    private double pupilScale = 0.16;

//paint method
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        paintEye(g, new Rectangle(left, eyeSize));
        paintEye(g, new Rectangle(right, eyeSize));

    }

    protected void paintEye(Graphics g, Rectangle bounds) {

        Color white = new Color(255, 255, 255);

        if (blink) {
            g.setColor(Color.YELLOW);
        } else {
            g.setColor(white);
        }
        g.fillOval(bounds.x, bounds.y, bounds.width, bounds.height); //left white fill eye

        g.setColor(Color.BLACK);
        g.drawOval(bounds.x, bounds.y, bounds.width, bounds.height); // left big black line eye

        if (!blink) {
            Color blue = new Color(0, 160, 198);

            paintEyePartAt(g, bounds, irisScale, blue);
            paintEyePartAt(g, bounds, pupilScale, Color.BLACK);
        }
    }

    private void paintEyePartAt(Graphics g, Rectangle bounds, double delta, Color color) {

        int width = (int) (bounds.width * delta);
        int height = (int) (bounds.height * delta);

        g.setColor(color);
        g.fillOval(
                xOffset + bounds.x + ((bounds.width - width) / 2),
                yOffset + bounds.y + ((bounds.height - height) / 2),
                width, height); // left blue fill eye
        g.setColor(Color.BLACK);
        g.drawOval(
                xOffset + bounds.x + ((bounds.width - width) / 2),
                yOffset + bounds.y + ((bounds.height - height) / 2),
                width,
                height); // left blue fill eye
    }
}

This complicates things, as painting can occur for any number of reasons, many of which you don't have control over or will be notified about, so you should be very careful about where and when you change values.

You should also have a look at Java Plugin support deprecated and Moving to a Plugin-Free Web and Why CS teachers should stop teaching Java applets.

Applets are simply a dead technology and given the inherent complexities involved in using them, you should instead focus you should probably attention towards window based programs.

Personally, I'd start with having a look at Painting in AWT and Swing and Performing Custom Painting

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • OMG! Thank you @MadProgrammer!!!!! :) You are such a big help to my project!!! :) – Tiffany Lee Apr 05 '16 at 05:43
  • Why when I attach the codes you given in my whole spongebob applet there is so many errors? Please help me how can I attach it to my full applet! Thanks! – Tiffany Lee Apr 05 '16 at 12:47
  • import java.awt.*; import javax.swing.*; import java.applet.*; import java.awt.geom.*; public class Pucca extends Applet { public Pucca(){ setSize(700, 700); } //paint method public void paint(Graphics g){ Color yellow = new Color(255, 255, 51); g.setColor(yellow); g.fillRect(500,50,400,400); //katawan Color white = new Color(255,255,255); g.setColor(white); g.fillOval(600, 100, 125, 125); //iba – Tiffany Lee Apr 05 '16 at 13:11
  • g.setColor(Color.BLACK); g.drawOval(600, 100, 125, 125); // left big eye line g.setColor(white); g.fillOval(700, 100, 125, 125); //white eye g.setColor(Color.BLACK); g.drawOval(700, 100, 125, 125); //big white eye Color blue = new Color(0, 160, 198); g.setColor(blue); g.fillOval(635, 130, 51, 51); // left blue eye g.setColor(Color.BLACK); g.drawOval(635, 130, 50, 50); // black line eye g.setColor(blue); g.fillOval(735, 130, 51, 51); // right blue eye – Tiffany Lee Apr 05 '16 at 13:14
  • g.setColor(Color.BLACK); g.drawOval(735, 130, 50, 50); // right black line eye g.setColor(Color.BLACK); g.fillOval(650, 145, 20, 20); // left black line eye g.setColor(Color.BLACK); g.fillOval(750, 145, 20, 20); // right black eye g.setColor(Color.BLACK); g.drawArc(608, 200, 200, 130, 190, 160); // bibig Color red = new Color(252, 0, 25); g.setColor(red); g.drawArc(570, 230, 80, 225, 50, 90); g.setColor(red); g.drawArc(770, 230, 80, 225, 40, 90); g.setColor(white); g.fillRect(670, 328, 26, 30); – Tiffany Lee Apr 05 '16 at 13:24
  • g.setColor(Color.BLACK); g.drawRect(670, 328, 25, 30); // ngipin g.setColor(white); g.fillRect(715, 328, 25, 30); g.setColor(Color.BLACK); g.drawRect(715, 328, 25, 30); Color darkyellow = new Color(188, 160, 31); g.setColor(darkyellow); g.drawArc(700, 205, 25, 100, 0, 220); Color pink = new Color(249, 155, 204); g.setColor(pink); g.drawArc(630, 360, 75, 25, 180,160); g.setColor(pink); g.drawArc(705, 360, 75, 25, 180, 160); – Tiffany Lee Apr 05 '16 at 13:26
  • g.setColor(white); g.fillRect(500, 400, 400, 100); g.setColor(Color.black); g.drawRect(500, 400, 400, 50); Color brown = new Color(222, 142, 41); g.setColor(brown); g.fillRect(500, 450, 400, 100); g.setColor(Color.BLACK); g.drawRect(500, 450, 400, 100); g.setColor(red); g.fillRect(680, 400, 50, 75); g.setColor(Color.BLACK); g.fillRect(520, 500, 50, 15); g.setColor(Color.BLACK); g.fillRect(620, 500, 50, 15); g.setColor(Color.BLACK); g.fillRect(720, 500, 50, 15); g.setColor(Color.BLACK); g.fillRect(820, 500, 50, 15); – Tiffany Lee Apr 05 '16 at 13:28
  • g.setColor(darkyellow); g.fillOval(530, 300, 30, 55); g.setColor(darkyellow); g.fillOval(520, 360, 15, 25); g.setColor(darkyellow); g.fillOval(530, 300, 30, 55); g.setColor(darkyellow); g.fillOval(560, 360, 15, 25); g.setColor(darkyellow); g.fillOval(540, 75, 30, 55); g.setColor(darkyellow); g.fillOval(520, 125, 15, 25); g.setColor(darkyellow); g.fillOval(850, 120, 15, 25); g.setColor(yellow); g.fillArc(480, 300, 50, 100, 90, 180); g.setColor(darkyellow); g.drawArc(480, 300, 50, 100, 90, 180); – Tiffany Lee Apr 05 '16 at 13:31
  • g.setColor(yellow); g.fillArc(480, 200, 50, 100, 90, 180); g.setColor(darkyellow); g.drawArc(480, 200, 50, 100, 90, 180); g.setColor(yellow); g.fillArc(480, 100, 50, 100, 90, 180); g.setColor(darkyellow); g.drawArc(480, 100, 50, 100, 90, 180); g.setColor(yellow); g.fillArc(500, 30, 100, 50, 0, 180); g.setColor(darkyellow); g.drawArc(500, 30, 100, 50, 0, 180); g.setColor(yellow); g.fillArc(600, 30, 100, 50, 0, 180); g.setColor(darkyellow); g.drawArc(600, 30, 100, 50, 0, 180); – Tiffany Lee Apr 05 '16 at 13:35
  • g.setColor(yellow); g.fillArc(700, 30, 100, 50, 0, 180); g.setColor(darkyellow); g.drawArc(700, 30, 100, 50, 0, 180); g.setColor(yellow); g.fillArc(800, 30, 100, 50, 0, 180); g.setColor(darkyellow); g.drawArc(800, 30, 100, 50, 0, 180); g.setColor(yellow); g.fillArc(865, 100, 50, 100, 270, 180); g.setColor(darkyellow); g.drawArc(865, 100, 50, 100, 270, 180); g.setColor(yellow); g.fillArc(865, 200, 50, 100, 270, 180); g.setColor(darkyellow); g.drawArc(865, 200, 50, 100, 270, 180); – Tiffany Lee Apr 05 '16 at 13:37
  • g.setColor(yellow); g.fillArc(865, 300, 50, 100, 270, 180); g.setColor(darkyellow); g.drawArc(865, 300, 50, 100, 270, 180); g.setColor(yellow); g.fillArc(880, 50, 30, 50, 270, 180); g.setColor(darkyellow); g.drawArc(880, 50, 30, 50, 270, 180); g.setColor(yellow); g.fillArc(490, 50, 30, 50, 90, 180); g.setColor(darkyellow); g.drawArc(490, 50, 30, 50, 90, 180); g.setColor(Color.BLACK); g.drawArc(432, 350, 50, 50, 0, 180); g.setColor(Color.BLACK); g.drawArc(911, 350, 50, 50, 0, 180); g.drawLine(482, 375, 432, 375); – Tiffany Lee Apr 05 '16 at 13:39
  • g.drawLine(962, 375, 912, 375); g.setColor(yellow); g.fillRect(450, 375, 20, 180); g.setColor(Color.BLACK); g.drawRect(450, 375, 20, 180); g.setColor(yellow); g.fillRect(928, 375, 20, 180); g.setColor(Color.BLACK); g.drawRect(928, 375, 20, 180); g.setColor(yellow); g.fillRect(650, 550, 20, 180); g.setColor(yellow); g.fillRect(750, 550, 20, 180); g.setColor(Color.BLACK); g.drawRect(650, 550, 20, 180); g.setColor(Color.BLACK); g.drawRect(750, 550, 20, 180); g.setColor(white); g.fillRect(751, 630, 18, 60); – Tiffany Lee Apr 05 '16 at 13:40
  • g.setColor(white); g.fillRect(651, 630, 18, 60); g.setColor(yellow); g.fillOval(435, 530, 50, 50); g.setColor(Color.BLACK); g.drawOval(435, 530, 50, 50); g.setColor(yellow); g.fillOval(915, 530, 50, 50); g.setColor(Color.BLACK); g.drawOval(915, 530, 50, 50); – Tiffany Lee Apr 05 '16 at 13:41
  • } //main method public static void main(String args[]){ JFrame myFrame = new JFrame("Draw Pucca"); myFrame.setSize(700, 700); myFrame.setLocationRelativeTo(null); myFrame.add(new Pucca()); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setVisible(true); } } – Tiffany Lee Apr 05 '16 at 13:42
  • That is all the codes of my spongebob applet. Please help me where to place the codes you give that moves the eye of spongebob. I give it a try but it gives me lot of errors. Hoping for your reply! Thank you so much! – Tiffany Lee Apr 05 '16 at 13:46