• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Drawing Problems with JPanel /Jframe need optimize solution.

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Experts,

I m trying to create a GUI for puzzle game following some kind of "game GUI template", but i have problems in that
,so i tried to implement that in various ways after looking on internet and discussions about drawing gui in swing, but i have problem with both of these, may be i m doing some silly mistake, which is still out of my consideration. please have a look at these two and recommend me one of them, which is running without problems (flickring and when you enlarge window the board draw copies (tiled) everywhere,

Note: i don't want to inherit jpanel or Jframe

here is my code :



but i wanted to do something with this template which has the same problem:


thanks a lot.
jibby
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TLDR
 
Jibby Lala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know, but atleast somebody can tell me about some obvious mistake or some optimize solution or guaranteed improvement.
also cross posted, for "No offense" :http://forums.sun.com/thread.jspa?messageID=11051503�
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jibby Lala wrote:I know, but atleast somebody can tell me about some obvious mistake


Do you really think anything in that marathon code that isn't obvious to you would be obvious to someone who didn't write it? If you have a specific problem/question, post a SSCCE that illustrates it.
 
Jibby Lala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is what i m trying to do with that code :

the same design and this has also problem so if i solved the problem in this i could solve the problem in that:


i thought there are also some mistake in this program while FAN disappear after 1st key press. and i astonished why i have to call the drawworld() method from run, why it is not drawing atleast firsttime when i m initializing the mykeylistener class.
thanks
jibby
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never never never use getGraphics() in connection with custom painting. Learn the correct approach here:
http://download.oracle.com/javase/tutorial/uiswing/painting/index.html
 
Jibby Lala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know but i hadn't another way and that is not the current concern. ideally paintcomponent() should call that method first with its "graphics" argument,but it is not happening.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you remember to call repaint()?
 
Jibby Lala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where and why, i m calling drawworld(),how could i call repaint when there is no paintcomponent or paint in class.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jibby Lala wrote:where and why, i m calling drawworld(),how could i call repaint when there is no paintcomponent or paint in class.



You would use a reference to the drawing JPanel and call repaint() on this. Perhaps this can be done from within the drawworld method? Just be sure that you have no program logic (such as changing the state of variables) within the painting (paintComponent) method, that all logic occurs outside of paintComponent (sucah as in a Timer's ActionListener's actionPerformed) and that after you change your variables, you call repaint() on the JPanel, and the JPanel then uses the changed variables so that it paints correctly.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) This will cause problems:

Since the MyKeyListener class uses the size of the JPanel to determine what to do, creating a second one that isn't added to the JFrame means that the size of the panel will be zero, causing some bad side effects. Better is to add the current visualized object:

Better still is to separate your key listener from the JPanel completely. Also, I still recommend key binding. Your code may work now, but if you start adding other components that may steal the focus, you'll run into problems. Key bindings will get you around those problems.

2) I'd call drawworld from within paintComponent and recommend that you not call this method directly as you're doing from your while(true) game loop. Again, it is better to change system logic (tick()), and then call repaint on the drawing JPanel:

Better still is to use a Swing Timer.

3) As mentioned by Darryl, get rid of your frame.getGraphics() but instead use the Graphics object passed into drawworld from the paintComponent method. Also if you follow our recommendations, you'll want to get rid of g.dispose() in the drawworld() method.


Best of luck.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More regarding use of a KeyListener:

To test out focus problems with key listeners, add a JButton to your component like so:


and then run your code. Do the key presses work?


The code below shows a similar small compilable program that uses key binding and also has a component, a JButton added to it. If you test it, you'll see that it works even if a JButton has been added. Again this is important as we often add our drawing JPanels onto complex Windows that have menus, and multiple other JComponents held in another JPanel, and often the drawing panel loses or never gets the focus.



This code also shows use of a Swing Timer for simple Swing animation.
 
Jibby Lala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you shared the marvelous bulk of information,first i need to digest that and then see whether i could exploit that with respect to my need and design and get back if there is some need.

Back to my real puzzle GUI problem, i noticed and checked that one of the puzzle design is totally matched now with what we have for running animation and realized that there isn't any problem of keylistener and other things which you discussed for running animation because that is not using that so much and may be i am still on the stage on drawing the puzzle board correctly and with adapting the design as i have for animation, i thought then there is some minor problem or lack of knowledge or redundant property or method which is creating the problem which i mentioned and that is still open issue.

The example which you shown is quite awesome but bit advanced (definitely not for you but for beginners) and i have to work in quite simple and so called limited design in which the programmer can work with minimum of mathematik, java coding and programming knowledge, when he has all those then that is plus point but if that is not then still by just following template he could able to create a game like as you noticed i wanted to adapt the same design for puzzle game as for the animation which is actually the aim and goal


God bless.
jibby
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jibby Lala wrote:
Back to my real puzzle GUI problem, i noticed and checked that one of the puzzle design is totally matched now with what we have for running animation and realized that there isn't any problem of keylistener and other things which you discussed for running animation because that is not using that so much and may be i am still on the stage on drawing the puzzle board correctly and with adapting the design as i have for animation, i thought then there is some minor problem or lack of knowledge or redundant property or method which is creating the problem which i mentioned and that is still open issue.


You may wish to better clarify just what the open issue is since the better you define the problem and the better the example code you post, the easier it will be us to help you solve it.

Luck!
 
Jibby Lala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't mind but then simple is you can run the code and see the problem as i already mentioned before flickring and other annoying things, which is not letting me to finalized a simple stable GUI within the same design contraints.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic