Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 740 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.

 

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

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (24 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!