Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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  
  getting the graphics context  (Read 1065 times)
0 Members and 1 Guest are viewing this topic.
Offline Serethos

Junior Member




Java games rock!


« Posted 2004-10-21 12:28:10 »

to initially (only once) set soe properties like font size etc  need to get the graphics context withoud the use of the paint(Graphics g) method.
im used to get that object by calling getGraphics() out of canvas (j2sdk), but this method doesnt exist in j2me.

is there any way to do it ?
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #1 - Posted 2004-10-21 18:44:47 »

Quite simply, you shouldn't.

Although the Font class does provide factory methods for obtaining font objects, you should steer away from using them as they are buggy as hell on many phones. (For instance, Font objects obtained from the Font factory methods on Samsung phones will return incorrect stringWidths)

Font objects obtained from Graphics contexts are usually correct - therefor the best option is to do lazy initialisation of whatever properties need intialising. (i.e. don't do the initialisation until the 1st repaint event)

Having said all that, i'm sure there are phones out there that the reverse is true! (but changing lazy init to use static factory methods is dead ezier)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Serethos

Junior Member




Java games rock!


« Reply #2 - Posted 2004-10-22 07:53:54 »

hmmm, im quite not sure if we really point to the same problem, so a little code chunk :

1  
2  
3  
4  
5  
6  
7  
public void paint(Graphics g)
{
      g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));

      renderMenu();
      ....
}

-> dont want that, cause every repaint() the font is set even if its hasnt changed


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public MyClass()
{
     Graphics g = myCanvas.getGraphicsFromCosmos();
      g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));

}

public void paint(Graphics g)
{
      renderMenu();
      ....
}

=> mucho better, but not possible ...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline caraiz

Senior Newbie




Java games rock!


« Reply #3 - Posted 2004-10-22 08:38:27 »

try to do an image with Image.createImage(width,height), and set/get the font in the graphics context of that image.

Offline Serethos

Junior Member




Java games rock!


« Reply #4 - Posted 2004-10-22 13:48:59 »

oehm ... but then i have to use that (useless) image because the graphic context is only attached to this image. i would need to make that image mutable too and ...  ... ...
dont think its a too good idea
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #5 - Posted 2004-10-22 19:58:51 »

Setting the font every game loop is what you should be doing !

Obviously, you don't want to be recreating the Font object every repaint, so simply create it once from the factory method, and store it.

1  
2  
3  
4  
5  
6  
7  
8  
9  
final Font SYSTEM_PLAIN_SMALL = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);

public void paint(Graphics g)
{
 g.setFont(SYSTEM_PLAIN_SMALL);
 
 renderMenu();
 ....
}


However, as I highlighted in my previous post, relying on the factory methods to obtain your Font objects can be unwise, it potencially makes you code a little more awkward to port to phones that have buggy Font factory methods (many of them do).


Incidentally, your example contains the method renderMenu(), yet I don't see any parameters passed to it.

A correctly structured program would have a renderMenu() with a signature more like :-

1  
renderMenu(Graphics g, String [] menuOptions, int selectedIndex)


ofcourse, you could abstract further, having a MenuItem [], or even further, delegating the menu rendering to a Menu class, etc etc.
However, for most J2ME games, the simple suggestion above should suffice.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Serethos

Junior Member




Java games rock!


« Reply #6 - Posted 2004-10-25 13:58:32 »

first, the renderMenu() method was shortened by me, indeed it gets the graphics context g as parameter. it does not need more than this.

but im still a little confused about some things.
i dont understand the thing, that im forced to call a method
more often, than i really need too:
if i only need small letters i only need to call setFont() one time (godd position would be constructor). after that, i does not change anymore.
but because i need to modify the graphics context, i have to call it every reapaint()  (or test if i have to).

next odd thing is, that i do not understand the technical difference between the factory methods and the font-setter.
why should setFont(SYSTEM_PLAIN_SMALL) work different to getFont(...) ?!
Offline davidaprice

Junior Member





« Reply #7 - Posted 2004-10-26 12:13:54 »

As Abuse says, with Canvas you need to set the font in each call to paint. Check the Javadocs for Canvas's paint method, and you'll see that at the start of each call to paint the graphics object's font is always the default font.

If you can use MIDP 2.0's GameCanvas instead, then you can just do getGraphics().setFont(...) once. But this isn't much help unless your target phone supports MIDP 2.0.
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 (15 views)
2014-07-24 01:59:36

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

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

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

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

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

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

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

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

Riven (50 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!