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 2 of 2 FirstFirst 12
Results 26 to 49 of 49

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

  1. #26
    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 thought that this code in the paintComponent() might do it:

     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();
             }

    I had hoped it would make the water move.

    What it's doing is drawing the black over all the blue instead of drawing little moving blue droplets with black outlines and is only drawing a blue drop at the very bottom.

    That's not what I'd hoped it'd do.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    For animation you need to change the drawing every few time units. The Timer controls your code being executed every few time units. The code called as the Timer listener needs to change the values controlling the painted output and then call repaint ==> then paintComponent gets called and it draws the stuff at the new location.
    Repeat as necessary for the effect you want.

    Your code probably makes the SAME drawing every time unit.
    No animation if the image is always the SAME.

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

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

    You're saying that my Timer should take some int values or whatever and change them and then have those values be also used in the paintComponent() method?

    That kinda looked like what they were doing in that example, though I didn't get the image so the example wouldn't work, but since it showed an example of what it did on the demo page anyway, I noticed it was moving an image.

    I'd certainly like to learn how to do that this summer too, but don't know how to get a GIF so that it keeps pretty much all the colors and formatting as a JPEG and also loses the white background, or whatever color and format background, that I can't seem to get rid of and always wonder how they make those GIF images in such a way that I can insert them into a webpage and they can just fit in whit the background and not be surrounded, for example, by the white space that I can't seem to keep out of the image when I edit it with Microsoft Paint no matter how hard I try.

    But anyway, I don't want the water in my fountain moving back up when I restart the Timer or anything like that.

    If I wanted that, I'd make a sprinkler system.


    I'm going to try out what you said.

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    I recommend that you write a simple drawing program (a Frame with a Panel). Have the paint method in the panel draw a circle at some location. Add a Timer and have the listener code change the location for the circle and call repaint.
    The result should be a circle moving around in the panel.

  5. #30
    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 an it worked. Now how do I do it for this one?

    Same way?

    This time though I don't want to just have the water appear at random places. So I don't need a Random object like I did in my demo code you told me to make
       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.JOptionPane;
       import javax.swing.*;
       import java.util.*;
       import java.io.*;
       import java.security.*;
       import javax.swing.Timer;
     
     
       public class MovingCircle extends JFrame implements ActionListener
       {
     
          private Timer t;
          private JPanel panel;
          private int xLoc;
          private int yLoc;
          private Random randx, randy;
     
          public MovingCircle()
          {
             super("Moving Circle Demo");
     
             setVisible(true);
             t = new Timer(3000,this);
             t.setDelay(500);
             t.start();
             panel = new JPanel();
             setContentPane(panel);
             randx = new Random();
             randy = new Random();
     
          }
     
          public void paint(Graphics g)
          {
             super.paint(g);
     
             g.drawOval(xLoc, yLoc, 30,30);
             g.setColor(Color.GREEN);
             g.fillOval(xLoc, yLoc, 30,30);
     
          }
     
          public void actionPerformed(ActionEvent e)
          {
             xLoc = randx.nextInt(3001);
             yLoc = randy.nextInt(3001);
             repaint();
     
     
          }
     
          public static void main(String[] args)
          {
             MovingCircle mc = new MovingCircle();
     
          }
       }

    The circle moves, though it's hard to see as I think I gave it a delay of half a second.

  6. #31
    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 tried it on the other one and it won't work quite, though there is a change. It got rid of all the black stuff that had been drawn there before.
    But it's still not animating.

       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;
          private int xLoc, yLoc;
     
          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));
     
                g.setColor(new Color(newRed, newGreen, newBlue));
                g.fillOval(xLoc,yLoc, 10, 10);
                g.setColor(Color.BLACK);
                g.drawOval(xLoc,yLoc,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)
             {
     
                for (int x = 325; x < 352; x++)
                {
     
                   for (int y = 125; y < 155; y++)
                   {
                      xLoc = x;
                      yLoc = y;
     
                      repaint();
                   }
                }
               // 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
          {
     
     
          }
     
     
       }

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Your changes to x and y are to radical to look like motion. Think of a better way to have a slow random motion.

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    But it's still not animating.
      for (int x = 325; x < 352; x++)
                {
     
                   for (int y = 125; y < 155; y++)
                   {
                      xLoc = x;
                      yLoc = y;
     
                      repaint();
                   }
                }
    how long a time is there between the calls to repaint?

    Go back and look at your simple program. You set the x,y values and called repaint ONCE per timer event call to the listener.

  9. #34
    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?

    An equation that makes the circles x2 + y2 = r2;
    x >=0 and y >=0 that starts at point(0, r) and ends at point (r,0) might work, but how do I set that up?

    Also, should I be restarting the Timer like I am at the end of the ActionListener or is that a bad idea?

    Also, as a side note, I, and it's worked before, tried to draw a sidewalk and some it would end up over the fountain, but as I said, in the past I found that if I called drawOval() or whatever after I called fill, then it would show the black outline along it.

    However, the sidewalk is being drawn before the fountain yet it's covering pretty much all of the fountain except part of it, which makes me wonder if that's what the repaint() keeps drawing.

    Or it could just happen that that part of the fountain is outside the bounds of the sidewalk.

    Also, are you suggesting that my Timer is moving too fast that it's moved by the Time I open the window, which by the way isn't starting out totally opened and has to be opened manually.
    Last edited by javapenguin; May 30th, 2011 at 01:13 PM.

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Go back to your simple program and work on it.
    should I be restarting the Timer
    Why?
    Did you do that in your simple program?
    Did your simple program work as expected?

  11. #36
    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?

    The simple program did move the Circle every half second like I told it to and had about 3 seconds of initial delay.

    No, I did not restart the Timer in my simple program.

    However, I used Randoms in the simple program.

    Not sure what Randoms could do here as I want the animation contained within a specific region, and want it to appear like a downward slope like the graph of a circle in the positive x-axis and positive y-axis region.

    Also, even I could get one droplet of water to keep moving, I was trying to get two, though I did get something to happen when I slowed down the animation.

    All that's happening is a dot is appearing out of nowhere.

    That's the extent of the animation that the code right now is giving me.

    I'm going to see what happens when I don't restart the Timer.

    Hmmmm..even before compiling it just now, something just occurred to me.

    If I only call repaint at the end, then won't it just keep redrawing the last dot only?

    Maybe that's the problem.....

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    get one droplet of water to keep moving
    draw it at a x,y location. The code executed at next call to the Timer listener will change that x,y location to a new value and call repaint which will go back to the beginning of this sentence and draw it at the new x,y location. And on and on and on. Each time around this loop the x,y values will be different, making the droplet appear to move.

  13. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (May 30th, 2011)

  14. #38
    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?

    How would I do that without using a Random?

    Should I use some kind of counter variable that will keep track of how many times listener is called and have a bunch of if and else if statements inside the listener that will draw it a different location each time the listener is called?

    That might work, though how do I get the drop back at the top at the end?

    Yep, that worked.

    However, still it's stuck down there unless I can get it to come back and keep moving like that over and over.
    Last edited by javapenguin; May 30th, 2011 at 01:41 PM.

  15. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    How would I do that
    how do you want the droplets to move. Along a line or randomly?

    The listener does not do any drawing. It computes the next location at which to draw.
    The paintComponent method uses that location to draw.

  16. #40
    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 got it to animate a droplet and finally figured out that I needed to reset my counter of the times it was called it 0 so that the droplet would move back to the start again.

    How do I get more droplets though? (I am wondering why my 3d rectangle is overdrawing my fountain (which I'm now thinking), since I was able to figure out how to make the scroll bars work without adding more components by simply calling revalidate() method, I have a lot of more room.

    However, even a regular rectangle covers the fountain I think.

  17. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    why my 3d rectangle is overdrawing my fountain
    Which is drawn first and which last?

  18. #42
    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?

    3D Rect drawn first.

    I figured that would work as I had noticed that if, for instance, I called drawOval() with a black outline and then called fillOval() say with blue shape, it would only show the blue shape and the fillOval() would cover up what I did with the drawOval() so I called drawOval() second.

    However, as this time with the sidewalk being not on top of the fountain, I called the drawSidewlak() first so that it would draw the fountain over the parts that I wanted it drawn over anyway. However, it's drawing the sidewalk over the fountain and I don't know why.

    Is it because it's 3D?

    However, I could have sworn I ran into the same problem with drawRectangle.

    However, perhaps a regular fountain draw over a cement background might be better looking anyway than the one I drew.

    Still, how do I fix this?

    I think I may have found a way to draw lots of drops too.

    However, I think I'll need an ArrayList or a LinkedList.

    I think an ArrayList will work best.

    That way I can access them quickly and also add more without having to keep changing the size like I would have to if I made just a plain array.

  19. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    how do I fix this
    Can you show what you are talking about? Give each layer you are printing an easily named color: Eg RED, CYAN, BLUE, YELLOW and then describe the problem by saying I want the RED layer on top of the YELLOW layer.

  20. #44
    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?

    How did you get to be a forum VIP?

    aussiemcgr appears to be one too.

    I want the drops to appear at the previous stages so that the drops will appear always moving down.

    After starting, there will be just one moving..

    After a certain interval, the first one will have moved and there will be a second drop in the place of the old one.
    After that interval again, a third drop will now be in the first spot, the second drop will be in the spot of the old original drop and the original drop will be further down. Then a new drop will be in the first spot and the others will move down and the same for a fifth drop. I want up to five drops and after the fifth appears, I always want there to be 5.

    Actually, I'd like a ton of drops to make it appear more like a great bunch of water moving, but I'm willing to try it out in steps.

    Also, as for the sidewalk thing, I actually had hoped to have the fountain appear to be on the sidewalk and the sidewalk only show up on outside of the fountain and just a little bit.

    However, only the top of the fountain is showing up when I draw the sidewalk.

    I will need to expand the fountain as it is anyway as the water appears to be moving more out that the size I originally had planned for it and I now know how to make the scroll bars expand for more room so space isn't too much of an issue like it had been at the beginning.

    I think the fountain was a pretty dark grey, you'll have too look at the code, which I'll post again, to see for sure. I think I'll comment it to show what's what. The sidewalk was a much lighter gray.

    How do I get all the drops to be blue with black outlines and still be moving? I did get one blue drop with a black outline to keep moving but I tried to make more and now it's drawing one blue one (may have black outline) and a bunch of black drops where the first drop should be moving after that, but none more after the fifth drop is drawn.

    I find that an ArrayList could be quite difficult to fiddle with after all, so went to plain arrays again.


       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;
          private int[] xLoc;
          private int[] yLoc;
          private int timesCalledCounter;
     
          public PaintDemo1()
          {
             timesCalledCounter = 0;
             setVisible(true);
             animator = new FountainAnimator();
             animator.setVisible(true);
     
             timer = new Timer(8000, animator);
             timer.setDelay(3000);
             timer.start();
     
             Graphics graphics = getGraphics();
     
             xLoc = new int[4];
             yLoc = new int[4];
     
             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);
     
     
     
     
             }
     
             public void drawParkShelter(Graphics 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 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));
     
                g.setColor(new Color(newRed, newGreen, newBlue));
     
                for (int x = 0; x < xLoc.length; x++)
                {
                   g.fillOval(xLoc[x],yLoc[x], 10, 10);
                   g.setColor(Color.BLACK);
                   g.drawOval(xLoc[x],yLoc[x], 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)
             {
                if (timesCalledCounter ==0)
                {
     
                   xLoc[0] = 325;
                   yLoc[0] = 125;
                   timesCalledCounter++;
                }
     
                else if (timesCalledCounter ==1)
                {
                   xLoc[1] = 373;
                   yLoc[1] = 150;
                   timesCalledCounter++;
                }
     
                else if (timesCalledCounter ==2)
                {
                   xLoc[2] = 410;
                   yLoc[2] = 190;
                   timesCalledCounter++;
                }
     
                else
                {
                   xLoc[3] = 425;
                   yLoc[3] = 230;
                   timesCalledCounter = 0;
                }
     
     
     
                animator.repaint();
               // 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
          {
     
     
          }
     
     
       }

    Also, that new Graphics class I had been making but never used had originally been used, hoping that if I made my own Graphics subclass, I could have better success. Now I think that I may still use it to make methods like

    drawSphere(int radius);

    drawCube(int side);

    drawTriangle(Point point1, Point point2, Point point3);

    etc.

  21. #45
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Good luck with your project. I guess you have lots of decisions and coding to do to work thru your wish list.
    Come back if you have any specific problems or questions.

    A suggestion:
    Pick ONE of your problems, write a SMALL program that executes to figure out how to solve it. When you have problems getting it to work, post the small program and ask your questions.
    Last edited by Norm; May 31st, 2011 at 02:48 PM. Reason: a suggestion

  22. #46
    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?

    No, an ArrayList would be better. Right now it'll be running into Null Pointer Exceptions, but how to....., hmmmm...

    is it possible to make a Point or something as a HashSet?

    Yes, that's what I'll do. It'll be expandable, won't throw Null Pointer Exceptions, and won't keep adding the same Points over and over.


    I think I may have just solved that problem for myself, but I still need help with the sidewalk thing.

    No, that won't work. You can't use a for loop to iterate through a Hash Set, at least not the way I'm used to.

    So what do I do then for that one?

    Well, I wrote my own HashSet class by using an object of my DoublyLinkedList class and the equals() method to ensure that nothing will end up there twice.

    Appears to be working.
    Last edited by javapenguin; May 31st, 2011 at 03:05 PM.

  23. #47
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    what do I do then for that one
    please explain. Are you asking how to use an Iterator?

  24. #48
    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?

    Well, I figured out that I had called some of the code in the paint method itself and before I drew the sidewalk so that's why it kept getting drawn over.

    So I think I've got it for now.

    May come back later to try and figure out how to set up lots of drops, but first I need to know what I'm aiming at more specifically.

  25. #49
    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?

    Well, I actually made my own HashSet class that used my old DoublyLinkedList object, basically my own Linked List class, as an object and added to it only if, by using the equals() method, none of the other things in the DoublyLinkedList object were equal to the new item being added.

    I have heard of Iterators but don't think I understood them that well.

    My HashSet class has no Iterators.

    But that's besides the point. I decided to just use a ton of regular ints to make all the droplets.

Page 2 of 2 FirstFirst 12

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