LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2004, 10:41 AM   #1
darkRoom
Member
 
Registered: Mar 2004
Location: Valencia, espaņa
Distribution: Slack, Gentoo, Custom
Posts: 162

Rep: Reputation: 30
Java, implemention problem


Hi
I have a program which constructs 2 objects with AI to play against each other in a card game. Simplified it works more or less it works as below

- a function (functionA) queries object1 to make a move
- object1 decides what it wants to play and it sets a JLabel(a card) in a JPanel

[edit]
in order to set the JLabel a function is called in another class, is this class were to make a callback it would have to be to a static method in the class where functionA is, there is no inheritance between these 2 classes
[/edit]

a repaint() is called but as repaint() only paints when it feels like it the image is not painted.

-the same function then queries object2 to make a move
-object2 decides what it wants to play and it sets a JLabel in a JPanel

object1 is queried once again and the function continues to cycle until there is a winner, however java does not paint anything until this function finally returns (ie a winner has been found).

without rewriting the whole program (i was not expecting this behaviour when i first started to write the program, and when it was just text it worked fine) is there a way i can force a repaint and cause the function to not continue until the label has been painted ?

----------------------------------

Unfortunately i can't set images instead of JLabels because i need functions that JLabel provides. This means (i don't think) i can use imageObserver or mediaTracker.

Also i experimented unsuccessfully with repaint(), i sent it parameters to only repaint the specific area that had changed and to give it a prioty to repaint. ie. repaint(0); where '0' is the number of miliseconds to wait before to repaint().

I've been thinking about this problem for a while now and i can't think of a solution so if anyone can help, i'd be most appreciative

thanks

Last edited by darkRoom; 10-23-2004 at 11:33 AM.
 
Old 10-23-2004, 10:58 PM   #2
scorbett
Member
 
Registered: May 2002
Location: Canada
Distribution: Slackware, Mandriva, RedHat
Posts: 46

Rep: Reputation: 15
If I understand correctly, you have two classes talking to each other, making moves back and forth... do you have any delay in there at all? Because if not, these things are going to be flying too fast to see anyway.

Here's my idea: do the game logic in a thread, and have the thread pause for a given period of time (say, 100 milliseconds or so) between moves. This gives Java a chance to update the display while your worker thread is sleeping. Depending on how you've coded your game this shouldn't be too much work.
 
Old 10-26-2004, 07:15 AM   #3
darkRoom
Member
 
Registered: Mar 2004
Location: Valencia, espaņa
Distribution: Slack, Gentoo, Custom
Posts: 162

Original Poster
Rep: Reputation: 30
Hi, thanks for the advice
I really don't understand this aspect of java, if i want to paint an object in a program i want to paint it now, not when the GUI deicides its ready to paint, what kind of logic is that ? So imagine how how happy i was to find 'paintImmediately()', just what i need, but wait, its not recommended by sun, why ? Because it doesn't work. I know swing isn't really for games but the only visual aspect of this app is JLabels appearing in a JPanel.

Anyway, yes i had a delay using Thread.sleep() but now i've rewritten the program with multi threads and its working fine, apart from an error thrown up by java's component class (it doesn't affect anything, and doesn't stop execution). I havn't changed any code at all apart from using a thread to set and remove the JLabels. I think this is another repaint problem as swing isn't threadsafe.

Code:
Java.lang.ArrayIndexOutOfBoundsException: No such child: 21
at java.awt.Container.getComponent(Container.java: 237)
at javax.swing.JComponent.rectangleIsObscured . .
Any idea how i can solve this ? my program never calls getComponent() directly, and there is no link back to where the error occured in my classes.
 
Old 10-26-2004, 09:06 PM   #4
scorbett
Member
 
Registered: May 2002
Location: Canada
Distribution: Slackware, Mandriva, RedHat
Posts: 46

Rep: Reputation: 15
Yes, that sounds like a repaint problem. How are you setting and removing the JLabels in your thread code? Make sure that you're doing it in a threadsafe way. You might want to look up the SwingWorker class, it's somewhere in the Java tutorial. It's really useful, I don't know why they don't make it part of the actual class library that ships with Java. Anyway, if you need to update GUI stuff like JLabels from a thread, you should either handle it very delicately or use a SwingWorker to do it in a safe way.

And yeah, the repaint() thing is kind of painful, but if you want more low-level control over the graphics your application puts out, you can always ditch swing and draw them yourself. That's a lot more work, though.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi-Langual implemention proble in application biru_vanshi Programming 1 11-05-2004 04:04 AM
Multi-Langual implemention problem in application biru_vanshi Linux - Software 1 11-04-2004 03:00 AM
Multi-Langual implemention problem in application biru_vanshi Linux - General 1 11-04-2004 02:08 AM
Design & implemention of a new network protocol on linux rasselin Linux - Networking 2 08-08-2003 02:14 PM
Java problem loganwva Linux - General 16 04-01-2002 02:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration