This discussion is archived
7 Replies Latest reply: Jul 15, 2011 2:03 PM by 801313 RSS

Draw an Updating JPanel

875749 Newbie
Currently Being Moderated
Hello, everybody. :] This is my first post here, and I'm just happy it's not about something entirely mundane, like array iterating or something.

Basically, I have a JPanel that I'm having a bunch of emulated pixels (drawLines) drawn on randomly on a panel that's 250 x 250. I'm learning Java right now, but I'm tinkering with this because I want to program games, so I figure this would be a decent first step. I still don't fully understand everything that's going on here, but let me post the code I have for the program.
// DrawPanel.java

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import java.util.Random;

public class DrawPanel extends JPanel
{
   public void paintComponent(Graphics g)
     {
          super.paintComponent(g);
          Random randomGenerator = new Random();
          int x, y; 
          int r, gr, b;
          
          while (true)
          {
             x = randomGenerator.nextInt(250) + 1;
             y = randomGenerator.nextInt(250) + 1;
             r = randomGenerator.nextInt(255) + 1;
             gr = randomGenerator.nextInt(255) + 1;
             b = randomGenerator.nextInt(255) + 1;
             
             g.setColor(new Color(r, gr, b));
             g.drawLine(x, y, x, y);
          }
     }
}
// DrawPanelTest.java

import javax.swing.JFrame;

public class DrawPanelTest
{
     public static void main(String args[])
     {
          DrawPanel panel = new DrawPanel();
          
          JFrame application = new JFrame();
          
          application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
          application.add(panel);
          application.setSize(250, 250);
          application.setVisible(true);
     }
}
I'd like to know what I can do to make the JPanel actively updating itself... once it gets drawn once, it stays in its drawn position, even though the program is constantly looping and drawing "lines". Like I said, I'm not exactly sure of what's going on, this has just been my tinkering... but if anyone could let me know what I can do or point me in the right direction, it would be much appreciated. Thank you!

Colton
  • 1. Re: Draw an Updating JPanel
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Never have long running code inside a painting method override. a while(true) loop is a worst case of that.

    Update state outside any painting method and call repaint(). And don't block the EDT.
    http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html

    Oh and
    I'm just happy it's not about something entirely mundane, like array iterating or something.
    Not much above that either.

    db
  • 2. Re: Draw an Updating JPanel
    875749 Newbie
    Currently Being Moderated
    Thanks for the response, Darryl. I'll read over that concurrency information and try to make as much sense of it as I can... it may be I need to master more of the Java language before I dabble in all of this so that I can understand it more clearly. I'll try and make something workable with what I know in the meantime, though.
    Darryl Burke wrote:
    Oh and
    I'm just happy it's not about something entirely mundane, like array iterating or something.
    Not much above that either.

    db
    Haha, you mean it's about as mundane? I guess someone beyond my highly limited ;] expertise would probably think so... it sure gets crazy. Anyways, thank you very much again.

    Colton
  • 3. Re: Draw an Updating JPanel
    gimbal2 Guru
    Currently Being Moderated
    872746 wrote:
    Haha, you mean it's about as mundane? I guess someone beyond my highly limited ;] expertise would probably think so... it sure gets crazy. Anyways, thank you very much again.
    Mundane in the sense that at least once a week some bloke comes along with a "weird bug in Java" that ends up being caused by blocking the Event Dispatch Thread of Swing. The main reason is because people jump into code without understanding what they are doing...

    You at least are two steps ahead:

    - you did not call this a weird bug
    - you admitted to jumping into coding without understanding it first ; )

    Swing is not for the faint of heart (for one it is a multithreaded environment, quite advanced stuff), I would start your gaming enterprises by creating for example a command line hangman game. It isn't as easy as you would think and it will help you to understand basic application flow. Keep it simple though, just print out how many turns the player has left.

    When you've done that you might consider translating it to a Swing version where you actually draw a man being hanged. Morbid fun!
  • 4. Re: Draw an Updating JPanel
    875749 Newbie
    Currently Being Moderated
    gimbal2,

    Actually, I've done a bit of simple game programming before with C++ and a graphics library called SFML.... I was able to create Pong, a simple helicopter flying game, and a simple space-type shooter. Even those very simple games ended up being highly complex, at least compared to what I expected before making them. As far as command-line games go, I've made Hangman and Tic-Tac-Toe so far with straight-up C++, as well. It's a very fun and rewarding process to program even the simplest of games, but when you get into graphics and GUI programming, there's so much to absorb, which I think is both good and bad.

    Your recommendation on the Swing version of Hangman, though, sounds like it would be fun. I'll probably end up giving that a shot when I learn more about Swing. Tinkering can unfortunately get me only so far. Tell me something, though... I've heard that for game programming, using the AWT is more efficient than using Swing. Is this true? Thank you very much for your reply.

    Colton
  • 5. Re: Draw an Updating JPanel
    801313 Explorer
    Currently Being Moderated
    1. You really should be using an Image. The easiest ways to get an Image are either to use Component.createImage or new BufferedImage. Look up the specs in the javadoc

    2. Create a JPanel subclass

    3. Add an Image member variable (ie "Image myImage") to this class

    4. override its paintComponent method with
    public void paintComponent(Graphics g) {
       super.paintComponent(g);
       g.drawImage(myImage,0,0,null);
    }
    5. Check out javax.swing.Timer you are going to want to use one of these to draw your lines on your Image (see Image.getGraphics())

    6. Create an AbstractAction (I think the swing Timer requires an Action / ActionListener - there are two timers in Java and I get confused sry) In the actionPerformed(ActionEvent ev) method add your line drawing without the loop.

    Hope this helps
  • 6. Re: Draw an Updating JPanel
    DarrylBurke Guru Moderator
    Currently Being Moderated
    tjacobs01 wrote:
    there are two timers in Java and I get confused sry
    Three ; - )

    db
  • 7. Re: Draw an Updating JPanel
    801313 Explorer
    Currently Being Moderated
    ... also remember to call repaint() when you are done updating the image

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points