Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
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  
  Text won't align right.  (Read 1280 times)
0 Members and 1 Guest are viewing this topic.
Offline SubzeroX5

Senior Newbie





« Posted 2011-09-09 21:55:40 »

I've been trying to make better fonts, using java, so I can apply it to my game later. But for some reason, I can't draw more then 2 fonts.

Here's the code

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  
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.*;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FontTest extends JPanel {

    public MyFont f;
    public MyFont f2;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        f = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "2/4");
        f.drawText();
        f2 = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "3/4");
        f2.drawText();
    }

    public class MyFont {

        public int centerX;
        public int centerY;
        public Shape text;
        public Graphics2D g;
        public String kind;
        public int x;
        public int y;
        public int s;
        public int str;
        public Color oc;
        public Color ic;
        public String word;
        public int w;
        public int h;

        public MyFont(Graphics2D g2d, String font, int x2, int y2, int s2, int stroke, Color outColor, Color inColor, String word2) {
            g = g2d;
            kind = font;
            x = x2;
            y = y2;
            s = s2;
            str = stroke;
            oc = outColor;
            ic = inColor;
            word = word2;
        }

        public void drawText() {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.getAllFonts();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            FontRenderContext frc = g.getFontRenderContext();
            Font font = new Font(kind, Font.PLAIN, s);
            TextLayout tl = new TextLayout(word, font, frc);
            AffineTransform transform = new AffineTransform();
            text = tl.getOutline(null);
            w = text.getBounds().width;
            h = text.getBounds().height;
            centerX = (w / 2);
            centerY = (h / 2);
            transform.translate(x - centerX, y + centerY);
            g.transform(transform);
            g.setStroke(new BasicStroke(str, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            g.setColor(oc);
            g.draw(text);
            g.setColor(ic);
            g.fill(text);
            g.clip(text);
        }

        public int getWidth() {
            return w;
        }

        public int getHeight() {
            return h;
        }

        public int centerX() {
            return centerX();
        }

        public int centerY() {
            return centerY();
        }
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new FontTest());
        f.setSize(400, 400);
        f.setVisible(true);
    }
}
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-09-09 22:40:42 »

That's because you call "g.clip(text)" on the last line of MyFont.drawText(). The clip(Shape) method intersects the current clip with the shape, causing all rendering outside of the resulting shape to be ignored. I ran your code, removed that line, and both strings rendered Wink

Offline SubzeroX5

Senior Newbie





« Reply #2 - Posted 2011-09-09 22:46:58 »

Thanks, I didn't know that line would do that.XD
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SubzeroX5

Senior Newbie





« Reply #3 - Posted 2011-09-10 01:19:29 »

For some reason, my texts get messed up, even though I put in the same coordinates for each text.

Here is the new code
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  
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.*;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FontTest extends JPanel {

    public MyFont f;
    public MyFont f2;
    public MyFont f3;
    public MyFont f4;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        f = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "2/4");
        f.drawText();
        f2 = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "3/4");
        f2.drawText();
        f3 = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "9999");
        f3.drawText();
        f4 = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "9989");
        f4.drawText();
    }

    public class MyFont {

        public int centerX;
        public int centerY;
        public Shape text;
        public Graphics2D g;
        public String kind;
        public int x;
        public int y;
        public int s;
        public int str;
        public Color oc;
        public Color ic;
        public String word;
        public int w;
        public int h;
        AffineTransform transform = new AffineTransform();

        public MyFont(Graphics2D g2d, String font, int x2, int y2, int s2, int stroke, Color outColor, Color inColor, String word2) {
            g = g2d;
            kind = font;
            x = x2;
            y = y2;
            s = s2;
            str = stroke;
            oc = outColor;
            ic = inColor;
            word = word2;

        }

        public void drawText() {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.getAllFonts();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            FontRenderContext frc = g.getFontRenderContext();
            Font font = new Font(kind, Font.PLAIN, s);
            TextLayout tl = new TextLayout(word, font, frc);
            text = tl.getOutline(null);
            w = text.getBounds().width;
            h = text.getBounds().height;
            centerX = (w / 2);
            centerY = (y / 2);
            transform.translate(x - centerX, y - centerY);
            g.transform(transform);
            g.setStroke(new BasicStroke(str, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, BasicStroke.JOIN_BEVEL));
            g.setColor(oc);
            g.draw(text);
            g.setColor(ic);
            g.fill(text);
        }

        public int getWidth() {
            return w;
        }

        public int getHeight() {
            return h;
        }

        public int getCenterX() {
            return centerX;
        }

        public int getCenterY() {
            return centerY;
        }
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new FontTest());
        f.setSize(400, 700);
        f.setVisible(true);
    }
}
Offline ReBirth
« Reply #4 - Posted 2011-09-10 03:03:57 »

what do you mean by it wont align? you gave same point exactly (same X, same Y) to these string. without run it, I guess they overwrite each other.

Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-09-10 03:24:52 »

No that is not the reason, I noticed that peculiar behavior when I ran it. You have just experienced a common pitfall of OOP. You are passing the same Graphics object to both MyFont objects. In MyFont #1, you set the Transform to translate (x-centerX,y-centerY). Then you keep that transform and re-translate in the next MyFont object, causing each MyFont to be offset by the translation of all previous MyFonts.
You have 2 options here:
-Either reset the old transform after drawText() returns like this:
1  
2  
3  
AffineTransform old = g.getTransform();
//drawing code
g.setTransform(old);

-Use Graphics.create() for each MyFont to get a new instance of Graphics that is an exact replica of the original.

I personally recommend the second option since it lets you do whatever you want to the Graphics object without affecting the others.

Offline SubzeroX5

Senior Newbie





« Reply #6 - Posted 2011-09-10 16:00:48 »

Thanks for the reply, but for some reason, when i did it like this

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
@Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        f = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "2/4");
        f.drawText();
        g2d.create();
        f2 = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "3/4");
        f2.drawText();
        g2d.create();
        f3 = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "9999");
        f3.drawText();
        g2d.create();
        f4 = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "9989");
        f4.drawText();
        g2d.create();
    }


The Graphics still wouldn't align correctly(i want them to overlap, so that I can tell if the align correctly).
I tried many alternatives, but they just get the same result, or worse.
I'm I doing something wrong?
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-09-11 01:04:30 »

create() returns a new instance, it doesn't affect the original one. They will both render to the same destination, but they are two different object references.

However, remember that create() returns an exact replica of the original, so any transformations that you did will be copied over. Try calling create() inside the constructor:
1  
2  
3  
4  
public MyFont(....) {
    this.g = (Graphics2D)g2d.create();
    ...
}

Offline SubzeroX5

Senior Newbie





« Reply #8 - Posted 2011-09-11 03:36:03 »

Thanks that worked. Smiley
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-09-11 05:05:03 »

Glad to help Cheesy

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.

radar3301 (9 views)
2014-09-21 23:33:17

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (46 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
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!