Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Outlined text and outlineColor and inner fillColor?  (Read 2102 times)
0 Members and 1 Guest are viewing this topic.
Offline whome

Junior Member




Carte Noir Java


« Posted 2006-04-15 14:56:28 »

http://koti.mbnet.fi/akini/java/png/outlinetext/

I have tested outlined text rendering. See link, example png and full sources. Two questions.

1) fill color for outlined text
How should I give a fill color for the outlined text or gradient paint. Now I render it two times. First render a normal text or gradient paint. Then I render outline text on top of it. But see an example picture how background and outline does not always match properly. "W" char is best to how color overflow.

2) reset state of Graphics2D before next method call
I call "sayWorld" method twice but with different parameters. I set various g2d properties before drawing a text. All settings reflect to the end of current paint method. Is there an easy way to reset g2d back to the state it was before calling a method. I would like a method call see it always as a "clean state" object.

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  
import java.awt.*; 
import javax.swing.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;

class THello extends JFrame {

  public THello() {
    super("THello"); setBounds(50,50,500,400);
    Container con = this.getContentPane(); //inherit frame
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    TCanvas canvas = new TCanvas();
    con.add(canvas);
    setVisible(true);
  }
 
  public static void main(String arg[]) {
    new THello();
  }
}

class TCanvas extends Canvas {
  Font font; FontMetrics fontMetrics; // make these global

  TCanvas() {
    font = new Font("Dialog",Font.BOLD,40);
    fontMetrics = getFontMetrics(font);
  }

  public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D)g;
    g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);
    g2D.setBackground(Color.white);
    g2D.setFont(font);

    sayWorld(g2D, 100, 100, true);
    sayWorld(g2D, 100, 160, false);
  }

  //outlined text
 public void sayWorld(Graphics2D g2D, int x, int y, boolean shear) {
      final String txt = "Hello World!";

      // gradient color from blue to red
     GradientPaint gp = new GradientPaint((float)x, (float)y, Color.blue,
                             x+100, y+20, Color.red);
      g2D.setPaint(gp);
      if (shear) g2D.shear(-0.5,0.0);
      else       g2D.shear(+0.5, 0);
      g2D.drawString(txt, x, y);

      FontRenderContext frc = new FontRenderContext(null,false,false);

      TextLayout tl = new TextLayout(txt, font, frc);
      AffineTransform textAt = new AffineTransform();
      //textAt.translate(0, (float)tl.getBounds().getHeight());
     textAt.translate(x,y);
      //textAt.shear(-0.5,0.0);

      Shape outline = tl.getOutline(textAt);
      g2D.setColor(Color.yellow);
      BasicStroke wideStroke = new BasicStroke(2);
      g2D.setStroke(wideStroke);
      g2D.draw(outline);
  }

}
Offline ctrl-alt-dev

Senior Newbie




Today, my bedroom. Tomorrow the World !


« Reply #1 - Posted 2006-04-15 16:29:38 »

Hi Whome,

If you already have the shape, why not use it again with g2D.fill(outline) ? 
Solves the problem :-) Apparently drawString calculates a little different  :-)

1  
2  
3  
4  
5  
6  
7  
8  
      Shape outline = tl.getOutline(textAt);
      g2D.setPaint(gp);
      g2D.fill(outline);
      //
      g2D.setColor(Color.yellow);
      BasicStroke wideStroke = new BasicStroke(2);
      g2D.setStroke(wideStroke);
      g2D.draw(outline);


For your second question, you can create scratch instances of the Graphics object using the create() method,
as you will modify these the original Graphics object will not be changed :-)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
    Graphics2D sub=(Graphics2D)g2D.create();
    try {
      sayWorld(g2D, 100, 100, true);
    } finally {
      sub.dispose();
    }   
    sub=(Graphics2D)g2D.create();
    try {
      sayWorld(g2D, 100, 160, false);
    } finally {
      sub.dispose();
    }


Have Fun,


Erik
Offline whome

Junior Member




Carte Noir Java


« Reply #2 - Posted 2006-04-15 17:30:05 »

Thx, both were so simple answers. I have uploaded outlinetext.zip and image2.png showing how it looks after suggested changes. A lot better results.

I take it that temporary g2d instances constain all attributes I have set at the start of paint method. I set rendering hints, default color and font.

Outlined text does not look good for smaller <20px font sizes, but it must be due to a smaller count of pixels available for rendering. I think I try creating +40px font size texts and then scale image to smaller if that gives better results for smaller text lines. But thx for these two valuable bits.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
Graphics2D g2D = (Graphics2D)g;
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);
g2D.setBackground(Color.white);
g2D.setFont(font);

// create temporary g2d instance for this method call only
Graphics2D gT = (Graphics2D)g2D.create();
sayWorld(gT, 100, 100, true);
gT.dispose();
...continue...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ctrl-alt-dev

Senior Newbie




Today, my bedroom. Tomorrow the World !


« Reply #3 - Posted 2006-04-16 11:06:59 »

To get nicely rendered small outlined text you can also tell the Graphics2D object to
scale all your rendering operations using the scale() method. 

The second thing you can try is to turn on the rendering hint for fractional metrics

1  
2  
3  
4  
    g2D.setRenderingHint( 
          RenderingHints.KEY_FRACTIONALMETRICS,
          RenderingHints.VALUE_FRACTIONALMETRICS_ON
    );


and tell your font render context that you're using these as well :

1  
  FontRenderContext frc = new FontRenderContext(null,true,true);


Ofcourse you must change the thickness of the outline accordingly :

1  
  BasicStroke wideStroke = new BasicStroke(0.5f);  // or something

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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

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

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

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

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

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

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

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
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!