0

just to preface, I am very new to java. So expect dumb mistakes.

I am trying to do a project with java's drawing panel, within BlueJ, and I cannot figure out how to make a program that has a moving object. This is a project, so the code is provided. We are having to modify it in whatever way we want. We are not able to add any other packages.

I know it has to do with some sort of loop, but I am making some type of mistake where it is just printing a ton of circles, instead of a new type every time I press refresh. Here is the code.

import java.awt.*;
import javax.swing.*;

public class DrawingPanel extends JPanel {
  public void paintComponent(Graphics g)
  {
   // clear screen
    g.setColor(Color.white);
    g.clearRect(0,0,500,500);
    {
      System.out.printf("Spring Design Barker Spring 2018%n");
      int x = 125;
      int y = 125;
      int w = 50;
      int h =80;
      int b = 50;
      int rd = 255 ;    
      int gn = 255 ;    
      int bl = 0 ;
      Circle c1,c2;
      Rectangle r1,r2;
      Triangle t1,t2;
      Color clr1,c;
      clr1 = new Color(rd,gn,bl);  
      r1 = new Rectangle(x,y,w,h,clr1);
      clr1 = new Color(106,96,200);  
      t1=new Triangle(x,y,w,h,clr1);
      clr1 = new Color(220,15,15);  
      c1=new Circle(x,25,25,clr1);
      r1.draw(g);      /*display the rectangle  */
      t1.draw(g);      /*display the triangle  */
      c1.draw(g);      /*display the circle  */
      t1.setH(-h);      /*display the triangle  */
      t1.setColor(new Color(15,220,15));      /*display the triangle  */
      t1.draw(g);      /*display the triangle  */

      x=200;
      y=200;
      for(int k=0;k<9;k++)
       {
         c=new Color(255-k*20,0+k*15,0+k*25);   // vary color
         c1=new Circle(200,10 * k,50,c);
         c1.draw(g);      /*display the new circle  */
       }
      //c=new Color(0,255,0);   // change paint in can to green
      //c2=new Circle(300,50,10,c);
      //c2.draw(g);      /*display the new circle  */
    }
  }
}
Aaron
  • 97
  • 12
  • Why would it print a new type? You're only telling it to print circles – MMelvin0581 May 01 '18 at 17:08
  • As I said, i am new, so I may not know what I am talking about. But say in the for loop. With that I am trying to make it print a new circle everytime I click refresh, given the new parameters with "k". – Aaron May 01 '18 at 17:11
  • So you're trying to print a new circle each time but you keep printing the same circle? – MMelvin0581 May 01 '18 at 17:12
  • Exactly. So the code given, was given to us, because we never got anything else to help us from the professor ._. So there is going to be a lot of extra things in the code I have provided currently, but the for loop, is my attempt at making one circle, that when refreshed, will appear another given location, without keeping the previous one, or printing them all at the same time. – Aaron May 01 '18 at 17:20

1 Answers1

1

The mistake is that you are drawing the circle again and again. Every time the code in the loop runs a new circle is drawn. You have to understand that when you draw the circle you are not actually redrawing the same circle but drawing a new circle. What I understand you want to do is to make a circle moving. You can do that by running this whole method again and again. The way I prefer to do this is by using the Swing Timer. It is a way to run a loop calling the paintComponent() method in simple words.

I am actually working on something and I am using this library to display graphics. The only thing I don't like about that is that it uses a lot of CPU. Maybe there is better way to do this.

konmal88
  • 111
  • 4
  • Okay, I think I see what you are saying. So say I want to make a circle, and it's coords move up by a certain number every time I press refresh. How would I do that? – Aaron May 01 '18 at 17:38
  • If you want to use Timer 1)Remove the loop , 2) Use Timer ... See this tutorial https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html – konmal88 May 01 '18 at 17:40
  • Any tips on doing it without the timer? Thanks for the help so far! – Aaron May 01 '18 at 17:46
  • Maybe you want to take a look in this link ... https://codereview.stackexchange.com/questions/29630/simple-java-animation-with-swing ... and this ... https://stackoverflow.com/questions/21247776/java-swing-how-to-smoothly-animate-move-component – konmal88 May 01 '18 at 18:06