Results 1 to 3 of 3
Thread: Help with repaint() command
- 11-26-2008, 01:24 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 1
- Rep Power
- 0
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(){ super(); 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; //addMouseListener(this); v = new Vector(); animationTimer = new javax.swing.Timer( ANIMATION_DELAY, new TimerHandler() ); animationTimer.start(); } 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(); m.setPOSX(e.getX()); m.setPOSY(e.getY()); v.add(m); paintComponent(frame.getGraphics()); } // 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){ super.paintComponent(g); for (int i = 0; i<v.size(); i++) { MySprite m = (MySprite)v.elementAt(i); frame.add(m); 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) m.setDX(1); if (m.getPOSX() == 500-15-m.getWidth()) m.setDX(-1); if (m.getPOSY() == 0) m.setDY(1); if (m.getPOSY() == 500-36-m.getHeight()) m.setDY(-1); m.setPOSY(m.getPOSY()+m.getDY()); m.setPOSX(m.getPOSX()+m.getDX()); } repaint(); // repaint animator } } public void main() { frame = new JFrame(); frame.setVisible(true); frame.setSize(500,500); frame.addMouseListener(this); } }
- 11-26-2008, 03:34 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
-
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
-
other than paint repaint
By amith in forum Java 2DReplies: 1Last Post: 07-01-2008, 11:39 PM -
repaint problem
By amith in forum Java 2DReplies: 2Last Post: 07-01-2008, 12:10 AM -
Repaint problem
By swimberl in forum Java 2DReplies: 1Last Post: 02-16-2008, 09:12 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 0Last Post: 01-06-2008, 03:28 AM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks