#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Issues with Japplet/loop for Animation(solved)


    SOLVED - (colors were out of range causing the issues, not the repaint method)

    Code:
    /** 
     * @author Jesse E*
     * Picture.java
     * @version 9-15-2013
     * This program draws a picture of pacman who ate poisoned food.
     */
    import java.awt.*;
    import javax.swing.JApplet;
    import java.util.*;
    
    public class nPacman extends JApplet
    {
        public void paint (Graphics g) //start of paint method which will draw the pictures
        {
            super.paint(g);
            
            repaint();
            int x,y,z,t,p,q,w,s;
            
            t = 0;
            w=0;
            p =0;
            q=0;
            s=0;
            
            
            
            g.setColor(Color.BLACK); 
            g.fillRect(0,0,500,500); 
            while(t<100000)
            {
                Random r = new Random();
                x = r.nextInt(301);
                y = r.nextInt(301);
                z = r.nextInt(301);
                p = r.nextInt(301);
                q = r.nextInt(301);
                w = r.nextInt(301);
               
                try 
                {
                    Thread.sleep(70);
                } 
                catch(InterruptedException ex) 
                {
                  
                }
                
            //sets color for the main background
           
            
    
            
            //draws the checkered pattern on the right hand side
            g.setColor(new Color(x,y,z));
            g.fillRect(450,0,50,500);
            g.setColor(Color.BLACK);
            g.fillRect(450,0,50,25);
            g.fillRect(450,50,50,25);
            g.fillRect(450,100,50,25);
            g.fillRect(450,150,50,25);
            g.fillRect(450,200,50,25);
            g.fillRect(450,250,50,25);
            g.fillRect(450,300,50,25);
            g.fillRect(450,350,50,25);
            g.fillRect(450,400,50,25);
            g.fillRect(450,450,50,25);
          
            //draws the green lines in the pattern on the right
            g.setColor(new Color(p,q,w));
            g.drawLine(450,0,500,500);
            g.drawLine(455,0,500,500);
            g.drawLine(460,0,500,500);
            g.drawLine(465,0,500,500);
            g.drawLine(470,0,500,500);
            g.drawLine(475,0,500,500);
            g.drawLine(480,0,500,500);
            g.drawLine(485,0,500,500);
            g.drawLine(490,0,500,500);
            g.drawLine(495,0,500,500);
            
            //draws the red lines in the pattern on the right
           // g.setColor(Color.RED);
            g.drawLine(450,0,450,500);
            g.drawLine(455,0,450,500);
            g.drawLine(460,0,450,500);
            g.drawLine(465,0,450,500);
            g.drawLine(470,0,450,500);
            g.drawLine(475,0,450,500);
            g.drawLine(480,0,450,500);
            g.drawLine(485,0,450,500);
            g.drawLine(490,0,450,500);
            g.drawLine(495,0,450,500);
    
            
            //draws the boxes for the cartoon 
            g.setColor(Color.RED);
            g.drawRect(0,0,90,90);//box 1
            g.drawRect(90,0,90,90);//box 2
            g.drawRect(180,0,90,90);// box 3
            g.drawRect(270,0,90,90);// box 4
            g.drawRect(360,0,90,90);//box 5
    
            //draws the blue dots in the cartoon and the big pacman
            g.setColor(Color.BLUE);
            g.fillOval(65,40,10,10);//blue dot 1
            g.fillOval(165,40,10,10);//blue dot 2
            g.fillOval(255,40,10,10);//blue dot 3
            g.fillOval(345,40,10,10);//blue dot 4
            g.fillOval(435,40,10,10);//blue dot 5
            g.fillArc(0,90,450,410,33,290);//Giant pacman
            
            //start of drawing the yellow pacmen in the cartoon
            g.setColor(Color.YELLOW);
            g.fillArc(0,0,85,85,50,260);//pacman 1
            g.fillArc(100,0,85,85,43,267);//pacman 2
            g.fillArc(185,0,85,85,33,290);//pacman 3
            g.fillArc(275,0,85,85,20,303);//pacman 4
            g.fillArc(365,0,85,85,10,330);//pacman 5
            
            //start of drawing big pacmans eye
            g.fillOval(265,160,50,50);
           
            g.fillOval(265,160,50,50);
            g.setColor(Color.BLACK);
            g.fillOval(275,160,40,40);
            g.setColor(Color.YELLOW);
            g.fillOval(285,160,30,30);
            g.setColor(Color.BLACK);
            g.fillOval(295,160,25,25);
            
            
            t++;
            
        }
    }
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    You really don't want to put a call to repaint inside of paint. The purpose of that method is to have the applet's context eventually call the applet's paint method.

    What do you want the applet to do?

    If it is an animation, you should create a separate Thread.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by bullet
    You really don't want to put a call to repaint inside of paint. The purpose of that method is to have the applet's context eventually call the applet's paint method.

    What do you want the applet to do?

    If it is an animation, you should create a separate Thread.
    on the same forum? yes it is an animation should I just change the title? sorry first post
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    This is an example of using a separate thread.

    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class MoveCircles extends JApplet {
       private int radius;
       private boolean keepGoing;
       private int factor;
    
       public void init() {
          setBackground(Color.white);
    
          radius = 100;
    
          factor = -5;
       }
    
       public void start() {
          keepGoing = false;
    
          keepGoing = true;
    
          new Thread(new MyThread()).start();
       }
    
       public class MyThread implements Runnable {
    
          public void run() {
             while (keepGoing) {
                radius = radius + factor;
    
                if (radius == 100)
                   factor = -5;
    
                if (radius == 50)
                   factor = 5;
    
                repaint();
    
                try {
                   Thread.sleep(1000);
                } catch (InterruptedException ie) {
                   System.out.println(ie);
                }
             }
          }
    
       } 
    
       public void stop() {
          keepGoing = false;
       }
    
       public void paint(Graphics g) {
          super.paint(g);
    
          g.setColor(Color.red);
    
          g.fillOval(getWidth()/2-radius,getHeight()/2-radius,2*radius,2*radius);
    
          g.setColor(Color.white);
    
          g.fillOval(getWidth()/2-(radius-10),getHeight()/2-(radius-10),2*(radius-10),2*(radius-10));
       }
    
    }
    After you compile it, you can load in a browser with this applet tag.

    Code:
    <applet code=MoveCircles width=400 height=400>
    </applet>

IMN logo majestic logo threadwatch logo seochat tools logo