0

Currently, I have a JFrame that contains a JPanel. The JPanel is the paint for my program. Paint meaning all of my Graphics g.drawString things. Right now, it only updates the display whenever the user interacts with the JFrame, but I want it to continuously update (repaint()) itself WITHOUT using a while loop (too much CPU usage).

Anyone know how I could do this?

Mat
  • 202,337
  • 40
  • 393
  • 406
Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80

2 Answers2

0

What you need to do is to inform the swing component whenever you know that part of the image on your panel changed. The normal way to do this is to, as you said, call repaint() and if you know the rectangle that was 'invalidated' you can also indicated that.

Depending on the events that cause the contents of the JPanel, you need to change your application design so that the presentation part of your application 'listens' to changes in the data underneath and repaints when these changes occur.

There is another method called paintImmediately() which might do for certain situations, but you have to describe a little more your scenario and in which cases are you needing to repaint continuously.

jbx
  • 21,365
  • 18
  • 90
  • 144
  • Thanks, but basically: I have implemented a fully functional internet relay chat system which involves paint to see what you and other people are saying. So I just need it to update continuously to show what people are saying. – Dylan Wheeler Sep 04 '11 at 18:30
  • Yes so what you need to do is when you get a new message from the network you know that you need to repaint(). The standard approach is to have the part handling the chat messages notify some object which also has a reference to the JPanel, which calls repaint() when there is a new message. This is known as the observer pattern (as indicated by Marcus above) which just means an object registers interest in events of another object to be notified each time something happens to it (such as a new message). – jbx Sep 04 '11 at 18:37
  • But wouldn't it be a LOT simpler to just have it AUTOMATICALLY repaint? Isn't there a way to do that? – Dylan Wheeler Sep 04 '11 at 18:49
  • Yes, but how can it know 'automatically' that something changed and thus it should repaint? Only you know it (in terms of your messaging protocol you are using for chatting). You are expecting a JPanel component to know what the rest of the program is doing. – jbx Sep 07 '11 at 10:32
  • You might want to have a look at the Model View Controller (MVC) design pattern to understand a bit more the typical approach used. This pattern simply states that the Model (i.e. the data, in your case the messages exchanged in chat) should be separate from the View (i.e. what is being presented as a UI, in your case your JPanel), and a Controller handles events (in your case messages) and updates the data model and triggers UI updates. That is the standard approach. http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller – jbx Sep 07 '11 at 10:36
0

I have implemented a fully functional internet relay chat system which involves paint to see what you and other people are saying

It sounds like you are trying to "pull" updates from the server, then you can use a SwingWorker to query the server for updates and then pulish the updates to the GUI. You would want the SwingWorker to sleep so it doesn't continuously poll the server.

Read the section from the Swing tutorial on Worker Threads and SwingWorker for more information.

Maybe a better design is for the server should "push" updates to the client and the client should listen for changes and then repaint itself. Maybe the All About Sockets section will help you out.

Edit:

Here is some old code I havent looked at in over 5 years. The "client" is a simple text pane which sends each character typed to the "server". The server then sends each character to any other client that is connected to the server. The idea it that the server has the most up to date Document. As one client sends a change all other clients are notified of the change. This way all clients always contain the same data. Your code should be simpler since you will only send complete messages to the server. To run the code open a dos window and type:

java DocumentServer 1234

This will start a server that listens to port 1234

Then open another window and type

java DocumentClient 1234 3

This will create 3 client frames that connect to the server. Typing in either of the frames will update the server.

You can get the zip file here:

https://www.camick.com/java/source/echo.zip

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks, I guess. But how would I "push" the updates to the client? – Dylan Wheeler Sep 04 '11 at 20:03
  • The example is the tutorial. For a more complex example see my edit. – camickr Sep 04 '11 at 22:38
  • A couple things: 1. Thank you SO much for helping me! 2. Is there a way to delete servers? 3. In the DocumentServer.java, there is a part that says: Enumeration enum = attributes.getAttributeNames(); This code results in an error. Do you know how I could fix this? – Dylan Wheeler Sep 04 '11 at 23:48
  • As I mentioned this is old code. I believe "enum" is a reserved word in newer JDK versions. Just rename the variable. Not sure what you mean be delete servers. YOu don't delete a server because is has all the information about the connected clients. YOu connect and disconnect clients from a server. Don't remember if my code does this or not. – camickr Sep 05 '11 at 00:08
  • Well, it is sweet. How can I make this code work for clients on many different computers? – Dylan Wheeler Sep 05 '11 at 00:41
  • First of all you need a web hosting site that will allow you to run Java applications as a server or you need to run the application on your computer which means you need a permanent IP address to other clients can connect with you.Then you need to change the way the client connects to the server. You need to use the website name (www.yourdomain.com) or the IP address of your host server. Those are the basics but I haven't done it in 7 years so I don't remember the details. Good luck. – camickr Sep 05 '11 at 00:53
  • Ug. Thanks though! I will try my best! – Dylan Wheeler Sep 05 '11 at 11:11