• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Instantiated components from within a thread aren’t repainting into a JFrame in Java

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NOTE: This thread is already at GameDev forum, but I think this problem should be of interest also in this forum. Thanks for your comprehension over this n00b.

Hi guys, I'm making this tetris variation, a piece should be spawned every 2 secs. I try to construct this piece (Block) from within a thread like this:


I'm using swingUtilities.invokeLater to send this thread to the swing event dispatcher, everything looks to work normal (i.e. the blocks instantiate each timeToSpawn) milisecs... but there is one BIG problem: they don't show on the Jframe! On the other hand whenever I just instantiate 1 or n blocks from within the same JFrame class like this for example:



They work perfectly fine and paint on the JFrame. What a mess.. I've been like crazy figuring out what's happening! My Block class extends Component and implements Runnable, it's run method looks like this:


It's a thread to update it's position each 450 milisecs... could this be the problem? although as I mentioned before, I just test this code and it worked, the problem is when I instantiate an object of this class within another thread.

As final and extra info. whenever I check the method toString on the Instantiated cubes from threads, they give me this [,0,0,0x0], but when I instantiate them within the same JFrame class they give me this result [,0,0,328x552] and they appear on the frame. this 328x552 value it's the same as the component's Dimension returned by getPreferredSize()... I tried to force them to that dimension by instantiate them like this:



But it didn't work, anyone knows what this [,0,0,328x552] value could mean?

Thanks in advance Everyone!

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Myself, I don't see a solution to your problem given the evidence you've presented, and it's a good bet that your bug is somewhere deep in code that you've not posted here. Keep using println statements of a debugger to try to isolate the problem, such as what you've already done (and that data sounds suspicious that your blocks have a 0x0 size).
 
Rigoberto Vides
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for double-open a discussion, I didn't wanted to Is there any way that any admin or myself delete the one at GameDev?

I change the run() method on my BlockSpawner (that extends Thread) class to something like this:


And it apparently worked! spawns the first pair of blocks and they show on the JFrame, then, just shows the log "SPAWN" but doesn't paints the cubes... The way I construct this BlockSpawner Thread is something like this:



at the JFrame class. The toString() method continued to behave as I mentioned before:

Block:mainPkg.Block[,0,0,328x552] for the first (painted) pair of blocks
Block:mainPkg.Block[,0,0,0x0] for the rest of the non-painted blocks

Thank you for the support and sorry again.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
snippets are better than nothing, but I'd prefer to copy/paste/compile/run a demo program, so that I can
a) see what you see (or not see)
b) test some theories

strip everything unrelated to the problem, and post that
 
Rigoberto Vides
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That Thread.sleep() is the bug's source... I placed it before instantiate the Blocks and now not even the first pair appears! Plus, i debbug the instantiated block with getSize(), and here are the results:

with pre Thread.sleep() instantiated blocks (only the first pair): size x:328 y:552
with post Thread.sleep() instantiated blocks (after the first loop): size x:0 y:0

just as @pete stein predicted...


Any ideas on how to fix this?
 
Sheriff
Posts: 22266
119
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you already aware how to handle concurrency in Swing?
 
Create symphonies in seed and soil. For this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic