Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Using Canvas object  (Read 4020 times)
0 Members and 1 Guest are viewing this topic.
Offline TigRoO

Senior Newbie




HouHou oOoO° oOoO°! (c:


« Posted 2003-09-15 07:47:19 »

HeLLo! Smiley

I made a simple graphic editor (grid/tile), and I was previously using a JPanel object to draw on. Now that I use this editor as my game editor, I need performances. So I read some advices on bufferStrategy and made out that JPanel does not allow buffer strategies... GnGnGnGn Solution : using a similar component to draw on --> Canvas. Am I right? Is everybody using Canvas object to render 2D games?

My problem is that when I have a menu bar just above my Canvas (using a double buffering strat'), the Canvas render CUTs the menu when expanded... Is there a superposition tip whith Swing components? Do anybody use menus in there games?
Offline immudium

Junior Member




Gorram it!


« Reply #1 - Posted 2003-09-15 16:34:38 »

I am using a Canvas object along with a BufferStrategy for a little side scroller that runs in window mode only.  However, like you, I found that menus were cut off, I'm guessing because the BufferStrategy drawing no longer lives in the realm of normal 2D drawing.  My solution was to resort to buttons instead of a menu.  Then, of course, the tooltips were cut off at which point I subclassed the JTooltip class and hooked it into my BufferStrategy.  I suspect you could do the same with menus, derive a class from JMenu and override the appropriate paint methods.  I'll play around with adding menus to my scroller and post if I discover anything else.

On a side note, I've noticed that Canvas doesn't seem to perform very well with multiple video cards/monitors.  Haven't been able to pin down exactly why yet, but that about the only big obstacle I've found with Canvas so far.
Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #2 - Posted 2003-09-15 16:38:53 »

I think this is to do with heavyweight vs lightweight components. I think alot of the Swing components have an option to set whether their rendering in one way or the other.

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #3 - Posted 2003-09-15 18:15:57 »

You can set the system to use heavyweight pop-ups which fixes the cut-off problems on menus and tooltips

JPopupMenu.setDefaultLightWeightPopupEnabled( false );


Offline immudium

Junior Member




Gorram it!


« Reply #4 - Posted 2003-09-15 18:40:07 »

I wouldn't have recognized that cause or thought of that solution.  Thanks to you both for pointing me in the right direction.  It saved me alot of time!   Smiley
Offline TigRoO

Senior Newbie




HouHou oOoO° oOoO°! (c:


« Reply #5 - Posted 2003-09-16 04:09:05 »

swpalmer,

   First, thanx for your help. I tried to use the method you indicated but...... it doesn't work :-/  Note that I use JMenuBar / JMenu / JMenuItem in my code, not JPopupMenu. I guess there's no very difference between the concepts but perhaps these components are not managed in the same way. Do you see where the problem's coming from else?

(I've a screenshot of my application running -with the menu, cut when expanded over the canvas- but I have no personal server to put it on so you can see the bug)

Cheers!  Grin
Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #6 - Posted 2003-09-16 04:56:11 »

If you're not really stuck to Swing, you could try AWT instead, I think they all have native peers and hence should be heavyweight.

Kev

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #7 - Posted 2003-09-16 06:07:10 »

Quote
swpalmer,

   First, thanx for your help. I tried to use the method you indicated but...... it doesn't work :-/  Note that I use JMenuBar / JMenu / JMenuItem in my code, not JPopupMenu. I guess there's no very difference between the concepts but perhaps these components are not managed in the same way. Do you see where the problem's coming from else?

(I've a screenshot of my application running -with the menu, cut when expanded over the canvas- but I have no personal server to put it on so you can see the bug)

Cheers!  Grin


I doubt this is the problem, but it has the same symptom...

I've seen the same problem before from a student's project, where he had made his program multi-threaded by overriding the AWT update method to do all his own "updates". He had no idea how to start a Thread / Runnable, but had managed to make an MT program this way Smiley.

Very cunning, completely inadvertent - and of course he had no idea what was going on Sad. One of the side effects was that everytime that re-painting was done by AWT for the Frame, he was overpainting with his own code. So the menus all got cut off (you could see them flick on very briefly before they were cut off).

Perhaps you are having a similar problem - check HOW, WHEN and WHY your paint() code gets invoked (nb: this is non-trivial. It can take quite a long time. My suggestion is to use the following little trick to find out:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
// Stick this anywhere in your code when you want to find out where it's being called from
try
{
  String a = null;
  a.toString();
}
catch( Exception e )
{
  // Wouldn't it be lovely if printStackTrace were accessible from Thread?
 // ...surely that would be the correct place for it to live - NOT in Exception?
 // Anyway, this works:
 System.out.println( e.printStackTrace() );
}


Once you've worked that out, check the Swing (and/or AWT) docs for the methods involved and make sure you are not accidentally abusing the spec.

malloc will be first against the wall when the revolution comes...
Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #8 - Posted 2003-09-16 06:16:34 »

I normally go for just:

1  
new Exception("I need a trace").printStackTrace();


Kev

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #9 - Posted 2003-09-16 07:22:54 »

Quote
I normally go for just:

1  
new Exception("I need a trace").printStackTrace();




Indeed; much better. It's been a while since I've needed it, and I wasn't sure whether you had to "throw" it in order to generate the stack-trace; in fact, IMHO, it should NOT generate the stack trace in the constructor (as it appears to), but should be lazy.

I suppose the verbose way I suggested might have a small advantage in that you see it in a source file and immediately think "WTF???! Do I want this?" - which is probably the correct reaction, since you probably shouldn't be leaving that kind of thing lying around Smiley

malloc will be first against the wall when the revolution comes...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline TigRoO

Senior Newbie




HouHou oOoO° oOoO°! (c:


« Reply #10 - Posted 2003-09-16 07:37:42 »

Roll Eyes Dear professor,

   I tried to repaint my menu components one by one at just after my bufferStrategy.show() invokation but there's nothing new, it is still cut by my canvas. About the spy suggestion : I only override the Canvas.paint() method, and did not touch the MenuComponent's, so I guess that re-draw them just after the canvas would overlay them to the front... But... Hem... Doesn't work Wink

Quote
If you're not really stuck to Swing, you could try AWT instead, I think they all have native peers and hence should be heavyweight.

Kev


I want to use Swing ('cause I love toolTips Grin)

TigRoO°
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #11 - Posted 2003-09-16 12:00:23 »

Quote
Note that I use JMenuBar / JMenu / JMenuItem in my code, not JPopupMenu. I guess there's no very difference between the concepts but perhaps these components are not managed in the same way. Do you see where the problem's coming from else?


That JPopup method does set a global flag that is used for all popups - regular menus and tool tips included.  At least it worked that way in my application.

I still believe this is caused by the mixing of heavyweights with lightweights.. I would read what Sun's Java pages have to say about this...

http://java.sun.com/products/jfc/tsc/articles/mixing/index.html

In particular are you doing the JPopup call before instantiating your menu objects? As mentioned in this paragraph:
"If you place Swing popup components in a window containing heavyweight components and it's possible that the popup windows will intersect a heavyweight, then invoke
1  
JPopupMenu.setDefaultLightWeightPopupEnabled(false)

before the popup components are instantiated."

I bet you simply have to move that code so it is called before you construct your menus.


Blah,
Note that you can probably also re-use a single Throwable and explicitly call fillInStackTrace() as needed.  The reason that all constructors call fillInStackTrace is that there is no method called to do this work at any other suitable time.  Unless they change the language so that 'throw' automatically calls fillInStackTrace on the Throwable prior to throwing... but then that might not be what you want and it would complicate re-throwing exceptions etc.

Offline TigRoO

Senior Newbie




HouHou oOoO° oOoO°! (c:


« Reply #12 - Posted 2003-09-16 12:31:14 »

Yeah, U got it  Cheesy Thanx a lot!

I'm going to catch a headache reading your stuff on expection (even more that I am French!). Spying is so simple with a System.out.print... Wink

But I'll read this all soon. Not yet, 'cause I'm making test on my PathFinder  Roll Eyes

Perhaps that I will give a little project soon to the community Smiley
Offline MGodehardt

Junior Member




why does the chicken cross the road?


« Reply #13 - Posted 2003-09-16 18:34:48 »

Do u already have a existing game which draws the tiles for you ?

If the answer is yes, why dont u use your app as editor ?

I have a WorldEditor which is plugged into the real game, but its your decision.
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #14 - Posted 2003-09-16 22:25:58 »

Quote

Blah,
Note that you can probably also re-use a single Throwable and explicitly call fillInStackTrace() as needed.  The reason that all constructors call fillInStackTrace is that there is no method called to do this work at any other suitable time.  Unless they change the language so that 'throw' automatically calls fillInStackTrace on the Throwable prior to throwing... but then that might not be what you want and it would complicate re-throwing exceptions etc.


Hmm, interesting...in what situation might that not be what you want?

My only reason against the constructor-based filling in is that I really really don't like heavyweight constructors. I wouldn't mind so much if Sun had documented them, but there are quite a lot of constructors in the standard libraries that take - in some cases - as much as 100 (or more!) times as long to execute as the average. This makes development much more difficult than it should be Sad. (although I think some of them are gradually being removed, thank goodness).

Over the years I've found heavy constructors tend to be more trouble than they're worth - in the long run, with any complex app, sooner or later you nearly always find you'd prefer to be able to do a two-stage init:

1. allocate memory, perform any essential but lightweight init
2. perform any heavyweight or multiple-code-path init

The first would be the constructor, the second is an "init()" method or similar. In practice, the current encouragement to do everything in the constructor is anti-OOP - you end up with a lot of unnecessary code duplication.

Even worse, the compiler / JLS is broken (IMHO) w.r.t. constructor delegation semantics:

"If you wish to delegate to another constructor, the delegation MUST be the first line of your constructor"...
...but that "first line" can contain any number of nested method calls etc (e.g. instantiation of objects etc). If you move them out of being nested in the first line, to the line above, the compiler says "no way" although - at a compilation level - AFAICS there is absolutely no change in the code produced. There is certainly no semantic change, other than that you generate additional references (assuming you wish to use your newly-instantiated objects in the delegation command).</rant Smiley>

malloc will be first against the wall when the revolution comes...
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #15 - Posted 2003-09-17 01:09:31 »

Well it was re-throwing that made me say that...  but I guess that could be handled by not filling in the stack trace if it was already filled in.

But then, why would you want to construct a Throwable if you didn't intend to get the stack trace or throw it?

Re: delegating to other constructors on the first line only.. yeah that is just odd.

Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #16 - Posted 2003-09-17 03:34:19 »

Why not use Thread.dumpStack()? It's designed exactly for what you seem to need..
Offline TigRoO

Senior Newbie




HouHou oOoO° oOoO°! (c:


« Reply #17 - Posted 2003-09-17 04:17:33 »

Quote
Do u already have a existing game which draws the tiles for you ?

If the answer is yes, why dont u use your app as editor ?

I have a WorldEditor which is plugged into the real game, but its your decision.


My work :

   I designed a World object, that is composed of tiles, each of them able to contain an entity (or more if they are "crossable"). Entity is the generic object for an... entity (Smiley) living in the world. They can be Tree or Actor for the moment. Tree is a simply not-crossable sprite and Actors are the default intelligent entities. (I am coding a bit of AI to embed Entities). Leading the whole of this, Game is the master class, managing Players. Each of them has a vision on the real World, this latter ruled by a super player : MasterOfPuppets (= "Mother Nature" Wink).  And other stuff but this Thread has not been dedicated to my work yet Wink

   My editor is mostly my developpment tool because I have no effective game ready. Actually, I take back my end-of-study school work that was nearly a strategy game (economy simulation) coded with a friend, with no time... So I rework the entire program to make a GREAT API  Cool

   I'd like to use an existing editor but mine rely on my own World architecture (as yours I guess) so it would be another work amount to lead on my work to maturity. But I thank you Smiley

   If some are interrested in seeing my work, ask me! Wink
Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #18 - Posted 2003-09-17 05:44:08 »

Quote

Why not use Thread.dumpStack()? It's designed exactly
for what you seem to need..


Cooo, now I look like a proper idiot Smiley Thanks for the info tho, when did that turn up?

Kev

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #19 - Posted 2003-09-17 09:52:58 »

Quote
Why not use Thread.dumpStack()? It's designed exactly for what you seem to need..


Because *that's what they'd EXPECT us to do*!

Or actually just because I'm an idiot Wink.

I think I failed to find it on previous occasions because:

 1. it won't show up when you do a search for "printstacktrace" - and I use the full-text-indexed versions of the API docs whenever possible, e.g. the windows help file versions. So I usually search for stuff.
 2. ...if/when I did find it, it would have been in the pre-1.4 days, and I would probably have dismissed it because it still didn't let me print the stack trace to an arbitrary stream - making it useless in lots of situations. I believe this is what's called a "blind spot"? i.e. you completely fail to make a logical connection between two things that's staring you in the face (in this case "no, it doesn't print to stream...but it would let me avoid faffing with Exceptions just to get a stack trace").

Chuckle.

malloc will be first against the wall when the revolution comes...
Offline srlindemann

Innocent Bystander




Java games rock!


« Reply #20 - Posted 2003-09-25 13:03:06 »

I've been playing with some basic animation code lately, and put together a small demo program for running in windowed mode.  I'm using Canvas/BufferStrategy, and am having horrible results.  About once every second I get a ~300ms lag.  
It certainly has nothing to do with gc (I can see that with the -verbose:gc option), and using the -Xrunhprof:cpu=samples option, I get:

  1 66.47% 66.47%     666    81 sun.awt.motif.MComponentPeer.swapBuffers
  2 16.47% 82.93%     165    31 sun.awt.motif.MToolkit.run

Any clues as to what's going on?  My animation loop is very standard.  Also, I'm working on Linux and have a new nVidia card.

These problems are causing me to think about manually doing double-buffering using the standard techniques.  For windowed-only apps, is this a better solution?
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #21 - Posted 2003-10-06 15:01:24 »

Quote
Why not use Thread.dumpStack()? It's designed exactly for what you seem to need..


Also, as I just belatedly realised, that method hasn't been upgraded to support the new 1.4 way of doing stack-traces - i.e. programmatic access. In most of the development I do, System.out and System.err are NOT safe places to be outputting stuff (you can't guarantee they are being saved to disk, you can't necessarily divert them over a network, etc).

So my logging library does everything with the programmatic access, with the added benefit of doing nice reformatting, highlighting of the most relevant code (HTML output: select the style based on the package of each frame in the trace; makes it easier to differentiate between 3rd party, standard library, and your own code when looking at massive stack traces Smiley) etc.

But I think you have to have a Throwable for that at the moment  ?

malloc will be first against the wall when the revolution comes...
Offline MGodehardt

Junior Member




why does the chicken cross the road?


« Reply #22 - Posted 2003-10-07 13:33:02 »

I do not use Swing components, its not available on all PC's, i use only 1.1 components, and i am really happy with AWT lightweight components, for applications i use newest jdk ( i can supply them on CD's so no problem even if the jdk is 500MB big )

But for RT games there is no need for swing components, i have my own controls, maybe swing is nice for money orientated apps for stock charts etc, but thats nothing for me, zzZZZZZzzzzz
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #23 - Posted 2003-10-07 15:25:12 »

Quote
I've been playing with some basic animation code lately, and put together a small demo program for running in windowed mode.  I'm using Canvas/BufferStrategy, and am having horrible results.  About once every second I get a ~300ms lag.  
It certainly has nothing to do with gc (I can see that with the -verbose:gc option), and using the -Xrunhprof:cpu=samples option, I get:


This is probably a red herring, but a long time ago I had similar problems that in the end turned out to be that I was accidentally reloading the sprites from disk lots of times, instead of just once and caching them (one line mistake!). I also had similar problems, but only on windows IIRC, that seemed to be bad event handling. When I removed all the keylistener mouselistener code etc - as an experiment - it went away.

I really feel for you on the profiling problem - in some graphical games on linux it seems you just get 60% - 90% of your time spent in a monolithic set of Toolkit classes, with no further breakdown of WTF is happening Sad.

malloc will be first against the wall when the revolution comes...
Offline MGodehardt

Junior Member




why does the chicken cross the road?


« Reply #24 - Posted 2003-10-08 09:54:01 »

blahblah are u using multiple threads in your program ?
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #25 - Posted 2003-10-09 03:47:25 »

srlindemann, it all depends on the kind of rendering you're doing.

On linux currently we're using Pixmaps for storing managed and volatile images and double-buffer extension for buffer-flipping, so it should work pretty fast if the x server supports the extension. But, as usual, if you're using lot of transparency/compositing, antialiasing, etc, it'll be slow because X11 doesn't support these operations, so we must use our software loops.

Run your test app with -Dsun.java2d.trace=count, and post the output - it'll help to determine what primitives you're using.
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #26 - Posted 2003-10-09 04:49:30 »

Quote
blahblah are u using multiple threads in your program ?


In my current one, yes. If you mean the one where I had the funny pauses, yes of course I was - because using the AWT events imposes that on you (one of the oft-cited reasons for using LWJGL, IIRC), and I can't remember how things went with invokeLater etc...

I have had "fun" in the past with the Windows Scheduler (which *sucked*), but on the whole no threading problems (except where caused by threads inside the JVM that I had no control over).

Does this cover what you wanted to know?

malloc will be first against the wall when the revolution comes...
Offline MGodehardt

Junior Member




why does the chicken cross the road?


« Reply #27 - Posted 2003-10-09 09:14:55 »

hm really strange blahblah, i noticed this little pauses in some programs. if it is caused by a thread from the jaba framework then we are in big trouble
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!