Results 1 to 6 of 6
  1. #1
    zgmonkey is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

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

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default 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!

  3. #3
    zgmonkey is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  5. #5
    zgmonkey is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default 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...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem with repaint command.

    Quote Originally Posted by zgmonkey View Post
    I copied that program word for word from the book, and you're telling me to change the whole thing...
    Yes. What book are you using? Please post the name so I can studiously avoid it.

Similar Threads

  1. problem with repaint()
    By ankit1801 in forum New To Java
    Replies: 2
    Last Post: 03-29-2011, 09:26 AM
  2. repaint() problem ?
    By santa in forum New To Java
    Replies: 4
    Last Post: 02-09-2011, 11:47 AM
  3. Repaint problem
    By citizenXL in forum New To Java
    Replies: 4
    Last Post: 10-28-2009, 03:02 PM
  4. Help with repaint() command
    By GeoffTK in forum New To Java
    Replies: 2
    Last Post: 11-26-2008, 04:33 AM
  5. Problem in repaint
    By Preethi in forum AWT / Swing
    Replies: 16
    Last Post: 03-18-2008, 08:10 PM

Tags for this Thread

Posting Permissions

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