Results 1 to 3 of 3
  1. #1
    GeoffTK is offline Member
    Join Date
    Nov 2008
    Rep Power

    Default Help with repaint() command

    Ok, here is my task in my CS Class.

    [quote]Your task this week is to create a program which places a circle on the screen where the user click the mouse. The circle will then move and bounce around inside the window. The circles created will be of random size, color, and direction. Allow the user to create any number of circles in this manner[quote]

    My Problem is that currently my program creates these circles on the click and all that but does not repaint them every 20milliseconds like the Timer should do. It only repaints them when I click again. I am fairly certain my problem is regarding the repaint() command. Here is the code, all help is appretiated!

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    public class MySprite extends JPanel implements MouseListener{
      private Random ran;
      private Vector v;
      private int width;
      private int height;
      private int posx;
      private int posy;
      private int dx;
      private int dy;
      private Color c;
      private JFrame frame;
      private javax.swing.Timer animationTimer; // Timer drives animation
      private final int ANIMATION_DELAY = 20; // millisecond delay
      public MySprite(){
        ran = new Random();
        c = new Color ( ran.nextInt(256), ran.nextInt(256), ran.nextInt(256) );
        width = 5 + ran.nextInt(45);
        height = 5 + ran.nextInt(45);
        posx = 0;
        posy = 0;
        if (ran.nextBoolean())
          dx = 1;
        else dx = -1;
        if (ran.nextBoolean())
          dy = 1;
        else dy = -1;
        v = new Vector();
        animationTimer = new javax.swing.Timer( ANIMATION_DELAY, new TimerHandler() );
        public int getWidth() {
          return width;
        public int  getHeight() {
          return height;
        public int  getDX() {
          return dx;
        public int  getDY() {
          return dy;
        public Color getColor() {
          return c;
        public void setWidth(int b) {
          width = b;
        public void  setHeight(int b) {
           height = b;
        public void  setDX(int b) {
          dx = b;
        public void  setDY(int b) {
          dy = b;
        public int getPOSX() {
          return posx;
        public int  getPOSY() {
          return posy;
        public void  setPOSX(int b) {
           posx = b;
        public void  setPOSY(int b) {
          posy = b;
            // Create a Sprite
        public void mousePressed(MouseEvent e) {
          MySprite m =new MySprite();
          // DO NOTHING!!!!!
        public void mouseClicked(MouseEvent e) {
          // DO NOTHING!!!!!
        public void mouseReleased(MouseEvent e) {
          // DO NOTHING!!!!!
        public void mouseEntered(MouseEvent e) {
          // DO NOTHING!!!!!
        public void mouseExited(MouseEvent e) {
      public void paintComponent(Graphics g){
        for (int i = 0; i<v.size(); i++) {
          MySprite m = (MySprite)v.elementAt(i);
          g.setColor( m.getColor() );
          g.fillOval( m.getPOSX() - (int)(.5* m.getWidth()), m.getPOSY() - (int)(.5* m.getHeight()), m.getWidth(),m.getHeight());
       // inner class to handle action events from Timer
       private class TimerHandler implements ActionListener {
          public void actionPerformed( ActionEvent actionEvent )
            for (int i = 0; i<v.size(); i++) {
             MySprite m = (MySprite)v.elementAt(i);
              if (m.getPOSX() == 0)
             if (m.getPOSX() == 500-15-m.getWidth())
             if (m.getPOSY() == 0)
             if (m.getPOSY() == 500-36-m.getHeight())
           repaint(); // repaint animator 
      public void main() {
        frame = new JFrame();

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power


    I think it's better that you should ask your question with a specific code segment. Just posting the whole code you have, readers can get board.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    You have a lot more problems here than just a repaint issue and I'm sure I'm not going to be able to find them all, but here are some:

    1) You have one class here trying to do everything. Instead you should refactor this program so that the Sprite class does nothing more than change its location (perhaps after calling a public method such as move()) and draw itself. It should know its own location, size, color, direction and speed, and probably the bounding JPanel's width and height.
    2) Have a main class that holds a JPanel for the display, an ArrayList of these Sprite objects and in your Timer actionPerformed method iterate through the list calling move() on each Sprite object and then call repaint on the graphics JPanel.
    3) Have the paintComponent first call the super method (as you are doing), then iterate through the spriteList calling draw(g) on each Sprite object; again this is a method that allows the Sprite object to draw itself. The paintComponent will not call frame.add(...) or do any other non-graphics processing.
    4) Have a MouseAdapter subclass whose mousePressed method will create a new Sprite object at the mouse position and then call repaint() on the graphics JPanel. Never call paintComponent directly unless you are a graphics guru and know precisely why you are doing this. Don't call getGraphics but let the JVM pass the correct Graphics object to the paintComponent method without your help.

    Do yourself a favor and read up on Swing and graphics as well as Graphics2D on the Sun tutorial site.

    Good luck.

Similar Threads

  1. other than paint repaint
    By amith in forum Java 2D
    Replies: 1
    Last Post: 07-01-2008, 11:39 PM
  2. repaint problem
    By amith in forum Java 2D
    Replies: 2
    Last Post: 07-01-2008, 12:10 AM
  3. Repaint problem
    By swimberl in forum Java 2D
    Replies: 1
    Last Post: 02-16-2008, 09:12 PM
  4. Repaint problem
    By swimberl in forum Java 2D
    Replies: 0
    Last Post: 01-06-2008, 03:28 AM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts