Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
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  
  Creating a text image on the fly?  (Read 2026 times)
0 Members and 1 Guest are viewing this topic.
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Posted 2004-12-02 18:02:05 »

I can currently paint strings no problem using.
1  
2  
3  
4  
Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
g.setColor(Color.RED);
g.setFont(new Font("Arial",Font.BOLD,8));
g.drawString("BooM",x,y);


That easy, but now I want to create an Image, that is text.  Like a score, kill monster a score value appears and floats upwards.

The acting and all that I got no problem, but I can't seem to figure out how to create a dynamic text image.  Since I don't want to create a gif for each score value.  I'd rather just create it on the fly.

So the question is:
Is this possible to do?
Which class should I use?

I assume I should be able to create it as a Graphics2D and some how change that graphic into a new image.
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #1 - Posted 2004-12-02 18:13:13 »

You do that like you did it in that snipped... the only difference is that you should create the font object outside the loop.

That's all.

The other way is to use bitmap fonts like this one:
Click to Play

弾幕 ☆ @mahonnaiseblog
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Reply #2 - Posted 2004-12-02 18:18:45 »

Yea thanks, hit me to after I went and grabbed a sandwich.

Add the text grapics to a HashMap and just manage them in there.  Thanks.

yea just creating a method called updateFloatingMessages()

That will cycle and handle it all.  Thanks.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Reply #3 - Posted 2004-12-02 19:08:29 »

For anyone else wonderring, create a class called FloatingMessage with a string, x and a y value.  Then used these methods.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
    public void paintFloatingMessages(){
        FloatingMessage fm;
       
        for(int i=0; i<messages.size(); i++){
            fm = (FloatingMessage)messages.get(i);
            Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
            g.setColor(Color.RED);
            g.setFont(new Font("Arial",Font.BOLD,8));
            g.drawString(fm.getMessage(),fm.getX(),fm.getX());
        }
    }

    public void updateFloatingMessages(){
        FloatingMessage fm;
       
        for(int i=0; i<messages.size(); i++){
            fm = (FloatingMessage)messages.get(i);
            if(fm.isMarkedForRemoval()){
                messages.remove(i);
            }
            fm.move();
        }
    }
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #4 - Posted 2004-12-02 19:30:12 »

that move() method should probably handle the floating up and fading out too.  When the Alpha for the fade is <= 0, isMarkedForRemoval() should return true.  Also, when you call remove(), you should decriment i by 1 otherwise you'll be skipping the next entry.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Reply #5 - Posted 2004-12-02 20:12:40 »

Yea thats all in the FloatingMessage class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
/*
 * Created on Dec 2, 2004
 *
 */

package game;


public class FloatingMessage {
    protected String message;
    protected int x;
    protected int y;
    protected int vY = -3;
    protected boolean markedForRemoval;
    
    public FloatingMessage(String m, int x, int y){
        message = m;
        this.x = x;
        this.y = y;
    }

    public FloatingMessage(){
        message = null;
        this.x = 0;
        this.y = 0;
    }
    
    public static void createFloatingMessage(Stage stage, String m, int x, int y){
        FloatingMessage fm = new FloatingMessage(m,x,y);
        stage.addFloatingMessage(fm);
    }
    
    public void setMessage(String m){
        message = m;
    }
    
    public String getMessage(){
        return message;
    }
    
    public void setX(int i){
        x = i;
    }
    
    public int getX(){
        return x;
    }
    
    public void setY(int i){
        y = i;
    }
    
    public int getY(){
        return y;
    }
    
    /**
     * Am I going to be deleted?
     * @return true or false
     */

    public boolean isMarkedForRemoval(){
       return markedForRemoval;    
    }
    
    public void move(){
        y+=vY;
        
        if(y < 0){
            remove();
        }
    }
    
    public void remove(){
        markedForRemoval = true;
    }
}



The only problem is the Y position is sometimes where I think it should be, but sometimes its way way off like right on my head, when it should be at like y 100, on a 800x600 screen.

I can't seem to figure out why that is.  I can't step through in eclipse because the window is blank, so its hard to tell.
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Reply #6 - Posted 2004-12-02 20:55:23 »

DOH!

Found the error, I pasted above there.

g.drawString( fm.getMessage(), fm.getX(), fm.getX() )

Passed in getX where it should have been my getY.
Offline Grazer

Junior Member




My other avatar is much more flattering.


« Reply #7 - Posted 2004-12-02 23:09:19 »

Quote
That easy, but now I want to create an Image, that is text.
<snip/>
I assume I should be able to create it as a Graphics2D and some how change that graphic into a new image.


The opposite way around, I believe: i.e. you'd create an Image and get the Graphics 2D from this.
1  
2  
3  
4  
5  
6  
BufferedImage floatingImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2d g = floatingImage.createGraphics();
g.setColor(Color.RED);
g.setFont(new Font("Arial",Font.BOLD,8));
g.drawString("BooM",x,y);
g.dispose();

If you did the above in the constructor of your FloatingMessage class, you'd then have a cached (and almost definitely "managed") image that you could just draw into your BufferStrategy's graphics rather than drawing the string every time. I don't know if copying a managed image is quicker than drawing the text each time, but I assume it would be. (Anyone know?) The downside (there's always one) is that all these managed images will use your precious video memory.

The trick with the above (the hard bit that I didn't write for you!) would be working out what size to make your image and what y co-ordinate to paint the text at (x will almost definitely be zero).
I'm pretty sure you can work it out with FontMetrics, but I've never done it myself.

Current Project: Easy Decal
Other Stuff: grlea online
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #8 - Posted 2004-12-03 03:04:39 »

if you have a Graphics2D object handy somewhere, set the font, and call
1  
Rectangle2D rect = g2d.getFontMetrics().getStringBounds("BooM!", g2d);

That'll cover your dimensions.  Keep in mind that Rectangle created is also effected by stuff like whether or not you have anti-aliasing turned on.  As in, if g2d has it turned off, but the Graphics2D object you draw onto has it turned on, you may find that the String you draw doesn't entirely fit.  So make sure you keep that consistent.

You could also just call new FontMetrics(Font f) but you have to have a Graphics2D object to pass in later anyway for the reasons I described... so you might as well just use it I guess Wink  I dunno, maybe there's some inefficiency in that.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Grazer

Junior Member




My other avatar is much more flattering.


« Reply #9 - Posted 2004-12-03 09:50:51 »

Quote
I don't know if copying a managed image is quicker than drawing the text each time, but I assume it would be. (Anyone know?)

It looks like I was right.
Chet Hasse recommends doing exactly this (caching rendered text in BufferedImages) in his latest article:
http://www.sys-con.com/story/?storyid=46983&DE=1

Current Project: Easy Decal
Other Stuff: grlea online
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Reply #10 - Posted 2004-12-03 18:03:33 »

Thanks for the info all.

Good read grlea, thanks.


Another article for those interested.
http://java.sun.com/developer/technicalArticles/Media/intimages/
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Reply #11 - Posted 2004-12-03 20:20:51 »

This works real nice, and the framerate is better as well.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
/*
 * Created on Dec 2, 2004
 *
 */

package game;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.geom.Rectangle2D;

/**
 * @author Jeremy
 *
 */

public class FloatingMessage {
    protected String message;
    protected int x;
    protected int y;
    protected int vY = -3;
    protected boolean markedForRemoval = false;
    protected Image intermediateImage;
    protected static Font font = new Font("Arial",Font.BOLD,11);
    
    public FloatingMessage(String m, int x, int y){
        message = m;
        this.x = x;
        this.y = y;
    }

    public void paintComponent(Graphics2D g) {
        if (intermediateImage == null) {
            // First, measure the size of the text
           Rectangle2D rect = g.getFontMetrics().getStringBounds(message, g);
            int imageW = (int)(rect.getWidth() - rect.getX() + 2);
            int imageH = (int)(rect.getHeight() - rect.getY());
            // We must also account for text "descent" in determining where to draw string in image
           int descent = (int)(g.getFontMetrics().getDescent() + .5f);
            // Now, create the intermediate image
           GraphicsConfiguration gc = g.getDeviceConfiguration();
            intermediateImage = gc.createCompatibleImage(imageW, imageH, Transparency.BITMASK);
            // And render the transparent background and the text into the image
           Graphics2D gImg = (Graphics2D)intermediateImage.getGraphics();
            gImg.setComposite(AlphaComposite.Src);
            gImg.setColor(new Color(0,0,0,0));
            gImg.fillRect(0, 0, imageW, imageH);
            renderText(gImg, 0, imageH - descent);
            gImg.dispose();
        }
        g.drawImage(intermediateImage, x, y,null);
    }
    
    public void renderText(Graphics g, int x, int y) {
        g.setColor(Color.WHITE);
        g.setFont(font);
        g.drawString(message, x, y);
    }
    
    public static void createFloatingMessage(Stage stage, String m, int x, int y){
        FloatingMessage fm = new FloatingMessage(m,x,y);
        stage.addFloatingMessage(fm);
    }
    
    public void setMessage(String m){
        message = m;
    }
    
    public String getMessage(){
        return message;
    }
    
    public void setX(int i){
        x = i;
    }
    
    public int getX(){
        return x;
    }
    
    public void setY(int i){
        y = i;
    }
    
    public int getY(){
        return y;
    }
    
    /**
     * Am I going to be deleted?
     * @return true or false
     */

    public boolean isMarkedForRemoval(){
       return markedForRemoval;    
    }
    
    public void move(){
        y+=vY;
        
        if(y < 0){
            remove();
        }
    }
    
    public void remove(){
        markedForRemoval = true;
    }
}


In my main class I just call these subroutines when I need them.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public void paintFloatingMessages(Graphics2D g){
    FloatingMessage fm;
    
    for(int i=0; i<messages.size(); i++){
        fm = (FloatingMessage)messages.get(i);
        fm.paintComponent(g);
    }
}

public void updateFloatingMessages(){
    FloatingMessage fm;
    
    for(int i=0; i<messages.size(); i++){
        fm = (FloatingMessage)messages.get(i);
        if(fm.isMarkedForRemoval()){
            messages.remove(i);
        }
        fm.move();
    }
}
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.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!