Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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  
  How to obtain a Graphics object and paint on it?  (Read 595 times)
0 Members and 1 Guest are viewing this topic.
Offline gcsaba2

Junior Member




Hello world


« Posted 2005-05-28 13:14:04 »

I've made a class called GFXManager. It contains all the methods that I would be using in my game, such as drawLine, drawHexagon, drawImage, drawBlurred, drawPixelated, etc... GFXManager contains two fields, Graphics g and Component parent. When I want to draw a line, for example, I actually call g.drawLine(...). I can call GFXManager's methods from anywhere I want, I would make GFXManager's instance public static final...
This all works fine, except that I cannot obtain the paren't Graphics object.  :-/ Let's say the parent is a JPanel, then I would create the manager like gfx = new GFXManager(panel, panel.getGraphics()); This doesn't return errors, however panel.getGraphics() will return null!

After looking around a bit all I managed to find out is that its impossible to obtain a component's Graphics object. I am forced to use the paint() method, and get the graphics from there. But this would force me to redesign my entire game, dump the GFXManager class and perhaps have a very long paint() method with everything in it. This is just not a good thing to do.  :-/

So is there a way for me to obtain the component's Graphics object? If not, I figured there are two ways to solve this:
  • Have a queue of actions, and when I call drawBlurred(), for example, I add that action to the queue, and in the end I call, from the component's paint() method, gfxmanager.render() which will do every action that's in the queue until its empty.
    I think this would be a slow and memory-costly solution, not to mention I'd have to invent message identifyers for all the possible actions. I would rather not do like this.
  • Create a Graphics instance inside the GFXManager, draw everything on it, and in the JPanel's paint() method I would write g = GFXManager.localG;
    I'm not sure if this would work though...
    After looking at it, Graphics is an abstract class, so I cannot really do this...  Angry

The best possible solution would be to obtain the JPanel's Graphics object...

So what should I do?  Huh
Offline Linuxhippy

Senior Member


Medals: 1


Java games rock!


« Reply #1 - Posted 2005-05-28 15:08:51 »

of course you can create a Graphics object yourself for let say a JPanel - no problem at all.
Simply use JPanel.getGraphics().

The only limitation you have is that the JPanel must be realized (=added to a visible Windows, JFrame or whatever, otherwise its not realized so there's no Graphics you can paint on...)

lg Clemens

btw. don't you think a SingleTon would better fit your needs for your GFManager-Class?
Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #2 - Posted 2005-05-28 15:50:12 »

Did you have a look at BufferStrategy ? It is used for active rendering (you don't rely on repaint events and use a classic "forever game loop".

example :

In your game, you will paint on a Canvas (AWT), and use

canvas.createBufferStrategy(2);
while (!endOfGame) {
 BufferStrategy bs = canvas.getBufferStrategy();
 Graphics g = bs.getGraphics();
 // insert game logic here
 bs.show();
}

there are some tutorials on this theme a google search should help you.

Lilian

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (43 views)
2013-05-17 21:29:12

alaslipknot (52 views)
2013-05-16 21:24:48

gouessej (80 views)
2013-05-16 00:53:38

gouessej (80 views)
2013-05-16 00:17:58

theagentd (89 views)
2013-05-15 15:01:13

theagentd (82 views)
2013-05-15 15:00:54

StreetDoggy (125 views)
2013-05-14 15:56:26

kutucuk (148 views)
2013-05-12 17:10:36

kutucuk (148 views)
2013-05-12 15:36:09

UnluckyDevil (158 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.098 seconds with 20 queries.