Antialias on JLabels

I've read some about antialias and Graphics2D and find it very complex. Isn't there an easy code to just set the quality of for example JLabels ?
[154 byte] By [N1klasa] at [2007-11-27 8:16:22]
# 1
Here's a quick example: http://www.javalobby.org/forums/thread.jspa?forumID=61&threadID=14179
kevjavaa at 2007-7-12 20:01:16 > top of Java-index,Java Essentials,New To Java...
# 2
I found this line very interesting:$ java -jar lib/application.jar -Dswing.aatext=trueBut exactly where do I put it? :D
N1klasa at 2007-7-12 20:01:16 > top of Java-index,Java Essentials,New To Java...
# 3

> I found this line very interesting:

> $ java -jar lib/application.jar -Dswing.aatext=true

>

> But exactly where do I put it? :D

You put it right there on the command line, when you invoke the JVM and run your application. It's defining a system property that the Swing libraries check to see that you want to do global antialiasing.

kevjavaa at 2007-7-12 20:01:16 > top of Java-index,Java Essentials,New To Java...
# 4
So everyone that wants to run my program has to use that line ?
N1klasa at 2007-7-12 20:01:16 > top of Java-index,Java Essentials,New To Java...
# 5

> So everyone that wants to run my program has to use

> that line ?

You can override the paintComponent() method to use anti-aliasing.

JLabel myLabel = new JLabel()

{

public void paintComponent(Graphics g)

{

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

super.paintComponent(g2);

}

};

That should do the trick.

CaptainMorgan08a at 2007-7-12 20:01:16 > top of Java-index,Java Essentials,New To Java...
# 6
Yeah I've tried this one and it works. But It's a lot of extra code to type that everytime I make a JLabel. Is there a way to make a method of it?
N1klasa at 2007-7-12 20:01:16 > top of Java-index,Java Essentials,New To Java...
# 7

> Yeah I've tried this one and it works. But It's a lot

> of extra code to type that everytime I make a JLabel.

> Is there a way to make a method of it?

Then you can make your own class that overrides the method. That way you would only need to type.

AntiAliasedLabel myLabel = new AntiAliasedLabel("...");

CaptainMorgan08a at 2007-7-12 20:01:16 > top of Java-index,Java Essentials,New To Java...
# 8

Yeah, making a new class is probably the best.

I was helped from a very kind person called Woflborg (#java QNet).

This code works perfect...

<code>

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import javax.swing.*;

public class AAJLabel extends JLabel

{

public AAJLabel(String s)

{

super(s);

}

public AAJLabel()

{

super();

}

public void paint(Graphics g)

{

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,

RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,

RenderingHints.VALUE_FRACTIONALMETRICS_ON);

super.paint(g);

}

}

</code>

Message was edited by:

N1klas

N1klasa at 2007-7-12 20:01:16 > top of Java-index,Java Essentials,New To Java...
# 9
> This code works perfect...You should override the paintComponent() method, not paint().
CaptainMorgan08a at 2007-7-12 20:01:16 > top of Java-index,Java Essentials,New To Java...
# 10
Why?If I override the paintComponent, everything crashes :(.
N1klasa at 2007-7-12 20:01:16 > top of Java-index,Java Essentials,New To Java...
# 11
> Why? http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html> If I override the paintComponent, everything crashes> :(.Probably because you're still calling super.paint(g). Change it to super.paintComponent(g).
CaptainMorgan08a at 2007-7-12 20:01:16 > top of Java-index,Java Essentials,New To Java...