Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 49

Thread: How do you get this fountain to animate with paint method?

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile How do you get this fountain to animate with paint method?

    I thought a for loop would do it. While I now know that what I did isn't entirely useless as I could probably use that technique to draw things a whole lot quicker than doing the code for each individual one, I still can't make the drops continually move. All it does is move the blue over and draw the black ovals. I was trying to make the water keep moving and appear to move and perhaps even keep the shape of the droplets to let the user know that the water is moving.

    However, it's not working out just right. I was using this one to become more familiar with paint method so that I could later use features on JToolBar and stuff to mess with images, though I think I'd need to somehow draw to an image to do that. But anyway, I'm worried about the smaller project right now.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.io.*;
     
    public class PaintDemo1 extends JFrame
    {
     
     
    private JPanel aPanel;
     
    public PaintDemo1()
    {
    setVisible(true);
    aPanel = new JPanel();
    aPanel.setVisible(true);
    Graphics graphics = getGraphics();
    System.out.println(graphics);
    drawFountain(graphics);
     
     
     
     setContentPane(aPanel);
    }
     
    public void drawFountain(Graphics g)
    {
    g.setColor(new Color(95,95,95));
    g.drawOval(300, 120, 65, 65);
    g.setColor(Color.BLACK);
    g.drawOval(320, 140, 25, 25);
    g.setColor(new Color(95,95,95));
    g.fillOval(320, 140, 25, 25);
     
    g.setColor(new Color(50,50,50));
    Polygon poly = new Polygon();
    poly.addPoint(325, 153);
    poly.addPoint(325, 125);
    poly.addPoint(330, 115);
    poly.addPoint(340, 125);
    poly.addPoint(340, 153);
    g.fillPolygon(poly);
     
    g.setColor(Color.BLACK);
    g.drawPolygon(poly);
     
     
    }
     
    public void animateFountain(Graphics g)
    {
    int newRed = Color.BLUE.getRed() + 10;
    int newGreen = Color.BLUE.getGreen() + 10;
    int newBlue = Color.BLUE.getBlue();
     
     g.setColor(new Color(newRed, newGreen, newBlue));
     
    for (int x = 325; x < 352; x++)
    {
     
    for (int y = 125; y < 155; y++)
    {
    // this spot is where the animation is supposed to be happening
    g.setColor(new Color(newRed, newGreen, newBlue));
    g.fillOval(x,y, 10, 10);
     g.setColor(Color.BLACK);
     g.drawOval(x,y,10,10);
     
    }
    }
     
     
    // repaint();
    }
     
     
     
    public void paint(Graphics g)
    {
     
    super.paint(g);
    g.setColor(new Color(88,88,88));
    Polygon poly = new Polygon();
     
    poly.addPoint(200, 200);
    poly.addPoint(400, 200);
    poly.addPoint(400, 300);
    poly.addPoint(100, 300);
    g.fillPolygon(poly);
     
    g.setColor(Color.BLACK);
     
    g.drawPolygon(poly);
     
    Polygon poly2 = new Polygon();
    poly2.addPoint(200, 200);
    poly2.addPoint(250, 100);
    poly2.addPoint(450, 110);
    poly2.addPoint(400, 200);
    g.setColor(Color.BLUE);
    g.fillPolygon(poly2);
     
    g.setColor(Color.BLACK);
    g.drawLine(200, 200, 250, 100);
    g.drawLine(250, 100, 450, 110);
    g.drawLine(450, 110, 400, 200);
     
     
    Polygon poly3 = new Polygon();
     
    poly3.addPoint(100, 300);
    poly3.addPoint(100, 200);
    poly3.addPoint(250, 100);
    poly3.addPoint(200, 200);
     
    g.setColor(new Color (88, 88,88));
    g.fillPolygon(poly3);
     
    g.setColor(Color.BLACK);
    g.drawLine(100, 300, 100, 200);
    g.drawLine(100, 200, 250, 100);
     
    Polygon poly4 = new Polygon();
    g.setColor(new Color(88,88,88));
     
    poly4.addPoint(450, 110);
    poly4.addPoint(510, 270);
    poly4.addPoint(400,300);
    poly4.addPoint(400, 200);
    g.fillPolygon(poly4);
    g.setColor(Color.BLACK);
    g.drawPolygon(poly4);
     
     
    System.out.println(g.getColor());
     
    drawFountain(g);
    animateFountain(g);
     
    }
     
    public static void main(String[] args)
    {
    PaintDemo1 pd1 = new PaintDemo1();
    pd1.setVisible(true);
     
    }
     
    }

    While I could avoid drawing the black ovals and just use the blue to make the water appear, I'd like it to move too.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do you get this fountain to animate with paint method?

    First, you really should take the recommendations already given to you in other posts - don't use getGraphics/use paintComponent rather than paint/etc... failure to take this advice will not help you receive more advice, since from our perspective you might just simply ignore said advice once again. You've been told this time and again...

    To address your animation question, Swing is single threaded - use a Swing timer or another thread to do the animation - you can't expect anything to animate in a single paintComponent call, rather when you use a SwingTimer you can alter drawing positions and call repaint.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you get this fountain to animate with paint method?

    Do you really write code without any indentations? Or is there some problem with the way you copy and paste your code on the forum?

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    I usually do, though I can easily indent it.

    Anyway, I was looking at another thing where I got some words to move to random places across a screen and I used a Thread method.

    Should I do the same here?

    Anyway, why do I often get a "you cannot post because the token has expired" or something like that message very often, usually after I leave the internet running for a while as I never shut it off? It's starting to become a little annoying.

    Anyway, am going to try that Thread thing.

    What's a SwingTimer?

    Don't know how to use one.

    I'll look that up too I guess.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    I was able to get words to move across the screen with just this simple code. I didn't use a Swing Timer or anything like that.

       import java.lang.Thread;
       import java.awt.*;
       import java.util.*;
       import java.io.*;
       import javax.swing.*;
     
     
     
       public class Melissa extends Thread 
       {
     
     
          Background b;
          private class Background extends JFrame
          {
             private String text;
             public Background(String name)
             {
                super(name);
                setVisible(true);
                setString(name);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
             }
     
             public void paint(Graphics g)
             {
                super.paint(g);
                Random r = new Random();
                Random r2 = new Random();
                int x = r.nextInt(300);
                int y = r.nextInt(300);
     
                int red = r2.nextInt(256);
                int green = r2.nextInt(256);
                int blue = r2.nextInt(256);
     
     
                g.setColor(new Color(red,green,blue));
                g.drawString(getString(), x, y);
     
     
     
             }
     
             public void setString(String text)
             {
                this.text = text;
             }
     
             public String getString()
             {
                return text;
             }
          }
          public Melissa(String name)
          {
             super(name);
             b = new Background(name);
     
             System.out.println(this);
             start();
          }
     
          public void run() {
          //Display info about this particular thread
             System.out.println(Thread.currentThread().getName());
             try
             {
     
                boolean b2= false;
                while (b2 == false)
                {
                   Thread.sleep(5000);
                   b.repaint();
                }
     
             }
     
                catch(InterruptedException ieRef)
                {
                   System.out.println("Error!");
                }
     
     
          }
     
          public static void main(String[] args)
          {
     
     
             String text3 = JOptionPane.showInputDialog(null, "Enter text", "Input required", JOptionPane.INFORMATION_MESSAGE);
     
     
             Melissa m2 = new Melissa(text3);
     
     
          }
     
       }

    Is this the Timer you guys are talking about? I think there might be more than one Timer class in Java so I have to be certain. It's in swing package so I'm pretty sure this is it, but just making sure. How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you get this fountain to animate with paint method?

    I think there might be more than one Timer class in Java
    Look at the API doc and you'll see how many there are.

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    But if I had an ActionListener, which I'd need to make a Timer object, then should I add some ActionListener to the JPanel or something then?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you get this fountain to animate with paint method?

    You can create a new inner class and make it the listener

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    So I use the paint method for the inner class for repaint()?

    If I'm using the regular repaint(), that will likely repaint the entire image and that's not what I want it to do, just the fountain water only.

    I have paint method calling a Graphics method to draw the fountain and stuff too, in addition to having the main paint method itself draw the base.

    I don't want it calling all of these methods over and over. Just the fountain water part. But how do I tell it that?

    I had thought of using the ActionListener to do that, but how do I call a Graphics method inside the ActionListener to get it to draw something?

    I'm just very confused now.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you get this fountain to animate with paint method?

    how do I call a Graphics method inside the ActionListener to get it to draw something?
    You don't. You compute the values that control what is to be drawn, then call repaint().
    When the paint method is later called by the JVM it uses the computed values to make the drawing.

  11. #11
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How do you get this fountain to animate with paint method?

    You need a Swing Timer if you want to use a timer to make changes to the GUI (painting, etc). All GUI changes must be done in the Swing Event Dispatch Thread (EDT), and a Swing Timer action is designed to enable this. You can use an ordinary Timer (java.util.Timer) for non-GUI timed actions.

  12. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    Should I use a Swing Worker, whatever that is, too?

  13. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    Ok, now it did something weird and drew more of my foundation again a bit upward. Sorta made a transpose but still kept the old one.

    It's an improvement a little bit, but the fountain won't animate and it's not continuing to move.

          import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       import javax.swing.JOptionPane;
       import java.util.*;
       import java.io.*;
       import javax.swing.Timer;
       public class PaintDemo1 extends JFrame implements ActionListener
       {
     
     
          private FountainAnimator animator;
          private Timer timer;
          public PaintDemo1()
          {
             setVisible(true);
             animator = new FountainAnimator();
             animator.setVisible(true);
     
             timer = new Timer(1000, this);
             timer.start();
     
             Graphics graphics = getGraphics();
             System.out.println(graphics);
     
     
     
     
             setContentPane(animator);
          }
     
     
     
          public static void main(String[] args)
          {
             PaintDemo1 pd1 = new PaintDemo1();
             pd1.setVisible(true);
     
          }
     
          public class FountainAnimator extends JPanel implements ActionListener 
          {
     
             public FountainAnimator()
             {
                super(new BorderLayout());
             }
     
             protected void paintComponent(Graphics g)
             {
                super.paintComponent(g);
                g.setColor(new Color(88,88,88));
                Polygon poly = new Polygon();
     
                poly.addPoint(200, 200);
                poly.addPoint(400, 200);
                poly.addPoint(400, 300);
                poly.addPoint(100, 300);
                g.fillPolygon(poly);
     
                g.setColor(Color.BLACK);
     
                g.drawPolygon(poly);
     
                Polygon poly2 = new Polygon();
                poly2.addPoint(200, 200);
                poly2.addPoint(250, 100);
                poly2.addPoint(450, 110);
                poly2.addPoint(400, 200);
                g.setColor(Color.BLUE);
                g.fillPolygon(poly2);
     
                g.setColor(Color.BLACK);
                g.drawLine(200, 200, 250, 100);
                g.drawLine(250, 100, 450, 110);
                g.drawLine(450, 110, 400, 200);
     
     
                Polygon poly3 = new Polygon();
     
                poly3.addPoint(100, 300);
                poly3.addPoint(100, 200);
                poly3.addPoint(250, 100);
                poly3.addPoint(200, 200);
     
                g.setColor(new Color (88, 88,88));
                g.fillPolygon(poly3);
     
                g.setColor(Color.BLACK);
                g.drawLine(100, 300, 100, 200);
                g.drawLine(100, 200, 250, 100);
     
                Polygon poly4 = new Polygon();
                g.setColor(new Color(88,88,88));
     
                poly4.addPoint(450, 110);
                poly4.addPoint(510, 270);
                poly4.addPoint(400,300);
                poly4.addPoint(400, 200);
                g.fillPolygon(poly4);
                g.setColor(Color.BLACK);
                g.drawPolygon(poly4);
     
     
                System.out.println(g.getColor());
     
                drawFountain(g);
                animateFountain(g);
     
             }
     
             public void drawFountain(Graphics g)
             {
                g.setColor(new Color(95,95,95));
                g.drawOval(300, 120, 65, 65);
                g.setColor(Color.BLACK);
                g.drawOval(320, 140, 25, 25);
                g.setColor(new Color(95,95,95));
                g.fillOval(320, 140, 25, 25);
     
                g.setColor(new Color(50,50,50));
                Polygon poly = new Polygon();
                poly.addPoint(325, 153);
                poly.addPoint(325, 125);
                poly.addPoint(330, 115);
                poly.addPoint(340, 125);
                poly.addPoint(340, 153);
                g.fillPolygon(poly);
     
                g.setColor(Color.BLACK);
                g.drawPolygon(poly);
     
     
             }
     
             public void animateFountain(Graphics g)
             {
                int newRed = Color.BLUE.getRed() + 10;
                int newGreen = Color.BLUE.getGreen() + 10;
                int newBlue = Color.BLUE.getBlue();
     
                g.setColor(new Color(newRed, newGreen, newBlue));
     
                for (int x = 325; x < 352; x++)
                {
     
                   for (int y = 125; y < 155; y++)
                   {
     
                      g.setColor(new Color(newRed, newGreen, newBlue));
                      g.fillOval(x,y, 10, 10);
                      g.setColor(Color.BLACK);
                      g.drawOval(x,y,10,10);
     
                   }
                }
     
     
             // repaint();
             }
     
     
             public void actionPerformed(ActionEvent e)
             {
                repaint();
             }
     
          }
     
          public void actionPerformed(ActionEvent e)
          {
             animator.repaint();
          }
       }

    I'm assuming the problem might be with me having a paint method in regular class.

    However, the overall problem now appears to be in the ActionListener class.

    Yep, once I got rid of the paint methods in the regular class, the double drawing goes away.

    But now I still don't have the fountain animating.

    The for loop, while it could be used to draw lots of water quickly, won't make it move.

    Also, what do I do with the ActionListener of the FountainAnimator class? I don't know how to tie it to the animation.

    If I could do that, maybe the Timer would make the fountain continue to move.

    Also, is there a better way to handle this to make water droplets with dark outlines to make sure they don't just look like blue water pouring? I'm trying to make it look like a ton of little drops moving.

    Also, in paintComponent(), I don't want the fountain base and stuff to keep redrawing, just the water (though I may draw and animate other stuff later once I figure out this part.)
    Last edited by javapenguin; May 27th, 2011 at 08:42 PM.

  14. #14
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    I can't run the call to MyThread inside the main method as it keeps saying "non-static variable this cannot be referenced from a static context".

    Why won't it work? I don't know how to work a Timer to do what I want it to and can't find any info that seems to help me. I've looked at some but so far haven't found anything definitive that will help.

       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       import javax.swing.JOptionPane;
       import java.util.*;
       import java.io.*;
       import javax.swing.Timer;
       public class PaintDemo1 extends JFrame implements ActionListener
       {
     
     
          private FountainAnimator animator;
          private Timer timer;
          public PaintDemo1()
          {
             setVisible(true);
             animator = new FountainAnimator();
             animator.setVisible(true);
     
             timer = new Timer(1000, this);
             timer.start();
     
             Graphics graphics = getGraphics();
             System.out.println(graphics);
     
     
     
     
             setContentPane(animator);
             MyThread mth = new MyThread();
             mth.run();
     
          }
     
     
     
          public static void main(String[] args)
          {
             PaintDemo1 pd1 = new PaintDemo1();
             pd1.setVisible(true);
     
          }
     
          public class FountainAnimator extends JPanel implements ActionListener 
          {
     
             public FountainAnimator()
             {
                super(new BorderLayout());
                setBackground(Color.GREEN);
     
             }
     
             protected void paintComponent(Graphics g)
             {
                super.paintComponent(g);
                g.setColor(new Color(88,88,88));
                Polygon poly = new Polygon();
     
                poly.addPoint(200, 200);
                poly.addPoint(400, 200);
                poly.addPoint(400, 300);
                poly.addPoint(100, 300);
                g.fillPolygon(poly);
     
                g.setColor(Color.BLACK);
     
                g.drawPolygon(poly);
     
                Polygon poly2 = new Polygon();
                poly2.addPoint(200, 200);
                poly2.addPoint(250, 100);
                poly2.addPoint(450, 110);
                poly2.addPoint(400, 200);
                g.setColor(Color.BLUE);
                g.fillPolygon(poly2);
     
                g.setColor(Color.BLACK);
                g.drawLine(200, 200, 250, 100);
                g.drawLine(250, 100, 450, 110);
                g.drawLine(450, 110, 400, 200);
     
     
                Polygon poly3 = new Polygon();
     
                poly3.addPoint(100, 300);
                poly3.addPoint(100, 200);
                poly3.addPoint(250, 100);
                poly3.addPoint(200, 200);
     
                g.setColor(new Color (88, 88,88));
                g.fillPolygon(poly3);
     
                g.setColor(Color.BLACK);
                g.drawLine(100, 300, 100, 200);
                g.drawLine(100, 200, 250, 100);
     
                Polygon poly4 = new Polygon();
                g.setColor(new Color(88,88,88));
     
                poly4.addPoint(450, 110);
                poly4.addPoint(510, 270);
                poly4.addPoint(400,300);
                poly4.addPoint(400, 200);
                g.fillPolygon(poly4);
                g.setColor(Color.BLACK);
                g.drawPolygon(poly4);
     
     
                System.out.println(g.getColor());
     
                drawFountain(g);
                animateFountain(g);
                drawLake(g);
             }
     
             public void drawFountain(Graphics g)
             {
                g.setColor(new Color(95,95,95));
                g.drawOval(300, 120, 65, 65);
                g.setColor(Color.BLACK);
                g.drawOval(320, 140, 25, 25);
                g.setColor(new Color(95,95,95));
                g.fillOval(320, 140, 25, 25);
     
                g.setColor(new Color(50,50,50));
                Polygon poly = new Polygon();
                poly.addPoint(325, 153);
                poly.addPoint(325, 125);
                poly.addPoint(330, 115);
                poly.addPoint(340, 125);
                poly.addPoint(340, 153);
                g.fillPolygon(poly);
     
                g.setColor(Color.BLACK);
                g.drawPolygon(poly);
     
     
             }
     
             public void animateFountain(Graphics g)
             {
                int newRed = Color.BLUE.getRed() + 10;
                int newGreen = Color.BLUE.getGreen() + 10;
                int newBlue = Color.BLUE.getBlue();
     
                g.setColor(new Color(newRed, newGreen, newBlue));
     
                for (int x = 325; x < 352; x++)
                {
     
                   for (int y = 125; y < 155; y++)
                   {
     
                      g.setColor(new Color(newRed, newGreen, newBlue));
                      g.fillOval(x,y, 10, 10);
                      g.setColor(Color.BLACK);
                      g.drawOval(x,y,10,10);
     
                   }
                }
     
     
             // repaint();
             }
     
     
             public void drawLake(Graphics g)
             {
                g.setColor(new Color(9,34, 136));
                g.fillOval(500,500, 300, 200);
                g.setColor(Color.BLACK);
                g.drawOval(500,500,300,200);
     
     
     
             }
             public void actionPerformed(ActionEvent e)
             {
                repaint();
             }
     
          }
     
          public void actionPerformed(ActionEvent e)
          {
             animator.repaint();
     
          }
     
          public abstract class MyGraphics extends Graphics
          {
     
     
          }
     
          public class MyThread extends Thread
          {
     
             public MyThread()
             {
                start();
             }
             public void run() {
             //Display info about this particular thread
                System.out.println(Thread.currentThread().getName());
                try
                {
     
                   boolean b2= false;
                   while (b2 == false)
                   {
                      Thread.sleep(5000);
                      animator.repaint();
                   }
     
                }
     
                   catch(InterruptedException ieRef)
                   {
                      System.out.println("Error!");
                   }
     
     
             }
     
     
          }
     
       }

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you get this fountain to animate with paint method?


  16. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    I read it. However, how do I get the code to draw things using Graphics inside an ActionListener if I can't use getGraphics()?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you get this fountain to animate with paint method?

    You don't.
    Call repaint which will cause paintComponent to be called.
    Do your drawing there.

  18. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    I was able to get something that looked like crude animation, not what I wanted, when I called getGraphics() inside the ActionListener that the Timer uses, but you told me not to do this.

    Also, for some reason, despite where I put it, the drawSidewalk() is drawing over my fountain. I thought that if I put it first, it would draw it and then draw the fountain over parts of it, which it's only doing for the animateFountain() part, but the fountain is gone. How do I get it back and yet keep the sidewalk?

    Also, how do I get the Timer to work. Read that thing and it's still not making sense.

       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       import javax.swing.JOptionPane;
       import java.util.*;
       import java.io.*;
       import javax.swing.Timer;
       public class PaintDemo1 extends JFrame implements ActionListener
       {
     
     
          private FountainAnimator animator;
          private Timer timer;
          private JScrollPane jsp;
          public PaintDemo1()
          {
     
             setVisible(true);
             animator = new FountainAnimator();
             animator.setVisible(true);
     
             timer = new Timer(1000, animator);
             timer.start();
     
             Graphics graphics = getGraphics();
             System.out.println(graphics);
     
     
             jsp = new JScrollPane(animator, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS  , JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
             Dimension d = new Dimension(9000,9000);
             animator.setPreferredSize(d);
             animator.revalidate();
             animator.setSize(d);
     
             animator.setAutoscrolls(true);
     
             setContentPane(jsp);
     
     
          }
     
     
     
          public static void main(String[] args)
          {
             PaintDemo1 pd1 = new PaintDemo1();
             pd1.setVisible(true);
     
          }
     
          public class FountainAnimator extends JPanel implements ActionListener 
          {
     
             public FountainAnimator()
             {
                super(new BorderLayout());
                setBackground(Color.GREEN);
     
             }
     
             protected void paintComponent(Graphics g)
             {
                super.paintComponent(g);
                g.setColor(new Color(88,88,88));
                Polygon poly = new Polygon();
     
                poly.addPoint(200, 200);
                poly.addPoint(400, 200);
                poly.addPoint(400, 300);
                poly.addPoint(100, 300);
                g.fillPolygon(poly);
     
                g.setColor(Color.BLACK);
     
                g.drawPolygon(poly);
     
                Polygon poly2 = new Polygon();
                poly2.addPoint(200, 200);
                poly2.addPoint(250, 100);
                poly2.addPoint(450, 110);
                poly2.addPoint(400, 200);
                g.setColor(Color.BLUE);
                g.fillPolygon(poly2);
     
                g.setColor(Color.BLACK);
                g.drawLine(200, 200, 250, 100);
                g.drawLine(250, 100, 450, 110);
                g.drawLine(450, 110, 400, 200);
     
     
                Polygon poly3 = new Polygon();
     
                poly3.addPoint(100, 300);
                poly3.addPoint(100, 200);
                poly3.addPoint(250, 100);
                poly3.addPoint(200, 200);
     
                g.setColor(new Color (88, 88,88));
                g.fillPolygon(poly3);
     
                g.setColor(Color.BLACK);
                g.drawLine(100, 300, 100, 200);
                g.drawLine(100, 200, 250, 100);
     
                Polygon poly4 = new Polygon();
                g.setColor(new Color(88,88,88));
     
                poly4.addPoint(450, 110);
                poly4.addPoint(510, 270);
                poly4.addPoint(400,300);
                poly4.addPoint(400, 200);
                g.fillPolygon(poly4);
                g.setColor(Color.BLACK);
                g.drawPolygon(poly4);
     
     
                System.out.println(g.getColor());
     
                drawSidewalk(g);
     
                drawFountain(g);
     
                animateFountain(g);
     
                drawLake(g);
                g.fillOval(8000,300, 100,100);
     
     
     
             }
     
             public void drawFountain(Graphics g)
             {
                g.setColor(new Color(95,95,95));
                g.drawOval(300, 120, 65, 65);
                g.setColor(Color.BLACK);
                g.drawOval(320, 140, 25, 25);
                g.setColor(new Color(95,95,95));
                g.fillOval(320, 140, 25, 25);
     
                g.setColor(new Color(50,50,50));
                Polygon poly = new Polygon();
                poly.addPoint(325, 153);
                poly.addPoint(325, 125);
                poly.addPoint(330, 115);
                poly.addPoint(340, 125);
                poly.addPoint(340, 153);
                g.fillPolygon(poly);
     
                g.setColor(Color.BLACK);
                g.drawPolygon(poly);
     
     
             }
     
             public void drawSidewalk(Graphics g)
             {
     
                g.setColor(new Color(183,183,183));
                g.fill3DRect(100,50,420,300,true);
     
     
             }
     
             public void animateFountain(Graphics g)
             {
                int newRed = Color.BLUE.getRed() + 10;
                int newGreen = Color.BLUE.getGreen() + 10;
                int newBlue = Color.BLUE.getBlue();
     
                g.setColor(new Color(newRed, newGreen, newBlue));
     
                for (int x = 325; x < 352; x++)
                {
     
                   for (int y = 125; y < 155; y++)
                   {
     
                      g.setColor(new Color(newRed, newGreen, newBlue));
                      g.fillOval(x,y, 10, 10);
                      g.setColor(Color.BLACK);
                      g.drawOval(x,y,10,10);
     
                   }
                }
     
     
             // repaint();
             }
     
     
             public void drawLake(Graphics g)
             {
                g.setColor(new Color(9,34, 136));
                g.fillOval(500,500, 300, 200);
                g.setColor(Color.BLACK);
                g.drawOval(500,500,300,200);
     
     
     
             }
             public void actionPerformed(ActionEvent e)
             {
     
               // what do I do here
     
     
             }
     
          }
     
          public void actionPerformed(ActionEvent e)
          {
            // animator.repaint();
          	// Do I even need this listener?
          }
     
          public abstract class MyGraphics extends Graphics
          {
     
     
          }
     
     
       }

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you get this fountain to animate with paint method?

    how do I get the Timer to work
    Copy the example code from the tutorial and make a small program from it. Have the action listener print out a message to show when it is executed.
    If you have problems copy the code here with your questions.

  20. #20
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    Ok, so I have yet another problem though. I think I may have it drawing everything inside paintComponent() yet I only want the water to move (may add more animation later once I get this part to work), to move, and not to have it keep redrawing the fountain itself.

    How do I fix that problem?

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you get this fountain to animate with paint method?

    You have to redraw everything every time if you clear the panel each paint.

  22. #22
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    I did. I printed the message but still isn't doing what I wanted animation-wise.

    By any chance should the Timer be an object of the class that is the Object I'm painting to, i.e. my FountainAnimaotr class, or is it fine that I made the Timer object to be defined inside the constructor of the actual class (PaintDemo1)?

    Unless repaint(), keeps drawing the sidewalk and that's why I keep losing my fountain! If so, how do I fix that?
    Last edited by javapenguin; May 29th, 2011 at 08:28 PM.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you get this fountain to animate with paint method?

    I printed the message
    What message?
    what I wanted animation-wise.
    Explain what your code is NOW doing that will make it animated?
    Is it using a Timer?

  24. #24
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How do you get this fountain to animate with paint method?

    What code? The AcfionListener or the paintComponent()?

    I told it to print "Execute now" or something like that and it does every 1 or 2 (probably 2) seconds.

       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       import javax.swing.JOptionPane;
       import java.util.*;
       import java.io.*;
       import javax.swing.Timer;
       public class PaintDemo1 extends JFrame implements ActionListener
       {
     
     
          private FountainAnimator animator;
          private Timer timer;
          private JScrollPane jsp;
          public PaintDemo1()
          {
     
             setVisible(true);
             animator = new FountainAnimator();
             animator.setVisible(true);
     
             timer = new Timer(1000, animator);
             timer.setDelay(1000);
             timer.start();
     
             Graphics graphics = getGraphics();
     
     
     
             jsp = new JScrollPane(animator, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS  , JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
             Dimension d = new Dimension(9000,9000);
             animator.setPreferredSize(d);
             animator.revalidate();
             animator.setSize(d);
     
             animator.setAutoscrolls(true);
     
             setContentPane(jsp);
     
     
          }
     
     
     
          public static void main(String[] args)
          {
             PaintDemo1 pd1 = new PaintDemo1();
             pd1.setVisible(true);
     
          }
     
          public class FountainAnimator extends JPanel implements ActionListener 
          {
     
             public FountainAnimator()
             {
                super(new BorderLayout());
                setBackground(Color.GREEN);
     
             }
     
             protected void paintComponent(Graphics g)
             {
                super.paintComponent(g);
                g.setColor(new Color(88,88,88));
                Polygon poly = new Polygon();
     
                poly.addPoint(200, 200);
                poly.addPoint(400, 200);
                poly.addPoint(400, 300);
                poly.addPoint(100, 300);
                g.fillPolygon(poly);
     
                g.setColor(Color.BLACK);
     
                g.drawPolygon(poly);
     
                Polygon poly2 = new Polygon();
                poly2.addPoint(200, 200);
                poly2.addPoint(250, 100);
                poly2.addPoint(450, 110);
                poly2.addPoint(400, 200);
                g.setColor(Color.BLUE);
                g.fillPolygon(poly2);
     
                g.setColor(Color.BLACK);
                g.drawLine(200, 200, 250, 100);
                g.drawLine(250, 100, 450, 110);
                g.drawLine(450, 110, 400, 200);
     
     
                Polygon poly3 = new Polygon();
     
                poly3.addPoint(100, 300);
                poly3.addPoint(100, 200);
                poly3.addPoint(250, 100);
                poly3.addPoint(200, 200);
     
                g.setColor(new Color (88, 88,88));
                g.fillPolygon(poly3);
     
                g.setColor(Color.BLACK);
                g.drawLine(100, 300, 100, 200);
                g.drawLine(100, 200, 250, 100);
     
                Polygon poly4 = new Polygon();
                g.setColor(new Color(88,88,88));
     
                poly4.addPoint(450, 110);
                poly4.addPoint(510, 270);
                poly4.addPoint(400,300);
                poly4.addPoint(400, 200);
                g.fillPolygon(poly4);
                g.setColor(Color.BLACK);
                g.drawPolygon(poly4);
     
     
     
     
                drawSidewalk(g);
     
                drawFountain(g);
     
                animateFountain(g);
     
                drawLake(g);
                g.fillOval(8000,300, 100,100);
     
     
     
             }
     
             public void drawFountain(Graphics g)
             {
                g.setColor(new Color(95,95,95));
                g.drawOval(300, 120, 65, 65);
                g.setColor(Color.BLACK);
                g.drawOval(320, 140, 25, 25);
                g.setColor(new Color(95,95,95));
                g.fillOval(320, 140, 25, 25);
     
                g.setColor(new Color(50,50,50));
                Polygon poly = new Polygon();
                poly.addPoint(325, 153);
                poly.addPoint(325, 125);
                poly.addPoint(330, 115);
                poly.addPoint(340, 125);
                poly.addPoint(340, 153);
                g.fillPolygon(poly);
     
                g.setColor(Color.BLACK);
                g.drawPolygon(poly);
     
     
             }
     
             public void drawSidewalk(Graphics g)
             {
     
                g.setColor(new Color(183,183,183));
                g.fill3DRect(100,50,420,300,true);
     
     
             }
     
             public void animateFountain(Graphics g)
             {
                int newRed = Color.BLUE.getRed() + 10;
                int newGreen = Color.BLUE.getGreen() + 10;
                int newBlue = Color.BLUE.getBlue();
     
                g.setColor(new Color(newRed, newGreen, newBlue));
     
                for (int x = 325; x < 352; x++)
                {
     
                   for (int y = 125; y < 155; y++)
                   {
     
                      g.setColor(new Color(newRed, newGreen, newBlue));
                      g.fillOval(x,y, 10, 10);
                      g.setColor(Color.BLACK);
                      g.drawOval(x,y,10,10);
     
                   }
                }
     
     
             // repaint();
             }
     
     
             public void drawLake(Graphics g)
             {
                g.setColor(new Color(9,34, 136));
                g.fillOval(500,500, 300, 200);
                g.setColor(Color.BLACK);
                g.drawOval(500,500,300,200);
     
     
     
             }
             public void actionPerformed(ActionEvent e)
             {
     
                animator.repaint();
                System.out.println("Executed now");
                timer.restart();
     
     
             }
     
          }
     
          public void actionPerformed(ActionEvent e)
          {
            // animator.repaint();
          	// Do I even need this listener?
          }
     
          public abstract class MyGraphics extends Graphics
          {
     
     
          }
     
     
       }

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you get this fountain to animate with paint method?

    Explain what your code is NOW doing that will make it animated?

    I assume that animation means showing things differently over time.

Page 1 of 2 12 LastLast

Similar Threads

  1. Paint program adding classes to main method class
    By Maxfmc in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 15th, 2011, 07:01 PM
  2. paintComponent() instead of paint()
    By Ciaran54 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 22nd, 2011, 02:46 PM
  3. Paint only part of screen
    By bonus_clip in forum AWT / Java Swing
    Replies: 7
    Last Post: October 16th, 2010, 07:21 PM
  4. Replies: 3
    Last Post: April 18th, 2010, 10:08 AM
  5. How overloaded paint() works?
    By maikeru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2009, 06:13 PM