Results 1 to 6 of 6
Thread: Problem with repaint command.
- 01-27-2012, 06:18 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Problem with repaint command.
Hello. I am new to java and using a slightly outdated book, so I'm sure I'm just missing something silly.
This program is supposed to take a block and move it up the screen, getting slightly faster as it goes. I'm pretty sure that part works, but I just keep getting a blank screen. I've tried moving around the repaint command to different parts of the program, but the best I can do is get the screen to flicker. The book says the place it inside the move thread, but it doesn't do anything when I put it there. Does anybody know what I'm doing wrong?
I am using NetBeans 7.1, but I have also tried it in Eclipse, and it does the same thing.
Java Code://import everything: import javax.swing.*; import java.awt.*; //JFrame class public class movingBlock extends JFrame { //screen size final int WIDTH = 900, HEIGHT = 650; //block speed double blockSpeed=0.5; //block Rectangle block = new Rectangle(WIDTH/9,HEIGHT/2,WIDTH/30,WIDTH/30); //constructor public movingBlock() { //creates JFrame super("Moving Block"); setSize(WIDTH,HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); //inner class Move m = new Move(); m.start(); } //draws stuff public void paint(Graphics g) { super.paint(g); //background g.setColor(Color.DARK_GRAY); g.fillRect(0,0,WIDTH,HEIGHT); //drawing the block g.setColor(Color.BLUE); g.fill3DRect(block.x,block.y,block.width,block.height,true); repaint(); } private class Move extends Thread { public void run() { while(true) { try { if(blockSpeed<=5) blockSpeed+=.2; block.y-=blockSpeed; Thread.sleep(75); } catch(Exception e) { break; } } } } //calls constructor public static void main (String[] args) { new movingBlock(); } }
- 01-27-2012, 06:26 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Problem with repaint command.
ouch! Don`t use repaint inside the paint / paintComponent !
Try a movingBlock.this.repaint(); inside the while loop (maybe before the sleep command)
And take a look at the NamingConventions
And Usually we override paintComponent instead of paint in Swing applications!
- 01-27-2012, 06:33 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Re: Problem with repaint command.
I know it's supposed to go in the while loop, but when I put it there I get a blank screen. Where it is the screen flickers, and that's the closest I've gotten.
movingBlock.this.repaint(); also gives a me a blank screen that flickers every 5 seconds...Last edited by zgmonkey; 01-27-2012 at 06:36 PM.
-
Re: Problem with repaint command.
Have you read any of the tutorials on Java Swing graphics? If not, you will want to. If so, you'll want to read some more: Don't draw on the top level window but rather in a component that derives from JComponent such as a JPanel or a JComponent itself. Don't paint in paint but in paintComponent. Also, consider using a Swing Timer rather than a distinct background thread.
- 01-27-2012, 07:41 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Re: Problem with repaint command.
I copied that program word for word from the book, and you're telling me to change the whole thing...
-
Re: Problem with repaint command.
Similar Threads
-
problem with repaint()
By ankit1801 in forum New To JavaReplies: 2Last Post: 03-29-2011, 09:26 AM -
repaint() problem ?
By santa in forum New To JavaReplies: 4Last Post: 02-09-2011, 11:47 AM -
Repaint problem
By citizenXL in forum New To JavaReplies: 4Last Post: 10-28-2009, 03:02 PM -
Help with repaint() command
By GeoffTK in forum New To JavaReplies: 2Last Post: 11-26-2008, 04:33 AM -
Problem in repaint
By Preethi in forum AWT / SwingReplies: 16Last Post: 03-18-2008, 08:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks