Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (856)
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 1583 times)
0 Members and 1 Guest are viewing this topic.
Offline gcsaba2

Junior Devvie

Hello world

« Posted 2005-05-28 11: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 Devvie

Medals: 1

Java games rock!

« Reply #1 - Posted 2005-05-28 13: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 Devvie

Projects: 1

Java games will probably rock someday...

« Reply #2 - Posted 2005-05-28 13: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

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

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


Pages: [1]
  ignore  |  Print  

hadezbladez (728 views)
2018-11-16 13:46:03

hadezbladez (363 views)
2018-11-16 13:41:33

hadezbladez (715 views)
2018-11-16 13:35:35

hadezbladez (181 views)
2018-11-16 13:32:03

EgonOlsen (2376 views)
2018-06-10 19:43:48

EgonOlsen (2530 views)
2018-06-10 19:43:44

EgonOlsen (1467 views)
2018-06-10 19:43:20

DesertCoockie (2133 views)
2018-05-13 18:23:11

nelsongames (1929 views)
2018-04-24 18:15:36

nelsongames (2611 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 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‑
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!