Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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  
  Disable Anti-Aliasing in Swing?  (Read 2563 times)
0 Members and 1 Guest are viewing this topic.
Offline JonathanC

Senior Newbie





« Posted 2009-01-07 17:22:18 »

For a given Swing component, I'd like to explicitly DISABLE anti-aliasing just for that component, not globally.

What's the preferred way of doing this?
Offline Mr. Gol

Senior Devvie


Medals: 1



« Reply #1 - Posted 2009-01-07 18:08:56 »

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics2D.html#setRenderingHint(java.awt.RenderingHints.Key,%20java.lang.Object)

Call it in paintComponent(Graphics g) of the component who's appearance you want to alter.
Offline JonathanC

Senior Newbie





« Reply #2 - Posted 2009-01-07 22:54:07 »

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics2D.html#setRenderingHint(java.awt.RenderingHints.Key,%20java.lang.Object)

Call it in paintComponent(Graphics g) of the component who's appearance you want to alter.

That was actually one of the first things I tried. It didn't work at all. To be specific, I'm turning off text anti-aliasing (since small fonts look terrible when anti-aliased), so I used both the normal AA key as well as the text-specific one. To ensure that my eyes weren't deceiving me, I zoomed in 800% in Photoshop to verify this.

Is there something else I'm missing?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trembovetski

Senior Devvie




If only I knew what I'm talking about!


« Reply #3 - Posted 2009-01-07 23:24:11 »

Try this:
1  
yourComponent.putClientProperty(SwingUtilities2.AA_TEXT_PROPERTY_KEY, new Boolean( false ) );


Dmitri
Offline JonathanC

Senior Newbie





« Reply #4 - Posted 2009-01-08 00:57:05 »

I get a CCE.

"java.lang.Boolean cannot be cast to sun.swing.SwingUtilities2$AATextInfo"

Is there some documentation for SwingUtilities2, or is it one of those internal classes?
Offline trembovetski

Senior Devvie




If only I knew what I'm talking about!


« Reply #5 - Posted 2009-01-08 06:26:59 »

Unfortunately it's one of those internal classes, and I guess the interface has changed.

You can tell the application to ignore the desktop hints (basically tell every text control
not to be antialiased) but I guess that's not what you're after.

http://java.sun.com/products/java-media/2D/reference/faqs/index.html#Q_How_can_I_specify_the_text_ant

Dmitri


Offline woogley
« Reply #6 - Posted 2009-01-08 07:37:28 »

That's one of the great reasons you should never use a sun.* class, as they are subject to change without notice.

Listen to Mr. Gol, he is correct. Just extend the component, override the default paint/paintComponent method, then disable the antialiasing there.

Run this test as an example:

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  
import javax.swing.*;
import java.awt.*;
class AAJLabel extends JLabel {
   boolean antialias;
   public AAJLabel(String str,boolean antialias) {
      super(str);
      this.antialias = antialias;
   }
   public void paintComponent(Graphics g) {
      Graphics2D g2d = (Graphics2D)g;
      if (!antialias) g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
      else g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      super.paintComponent(g2d);
   }
}

public class Test extends JFrame {
   AAJLabel label[];
   public Test() {
      super("Test");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      label = new AAJLabel[2];
      label[0] = new AAJLabel("Test with Antialias",true);
      label[0].setFont(label[0].getFont().deriveFont(48.0f));
      label[1] = new AAJLabel("Test without Antialias",false);
      label[1].setFont(label[0].getFont());
      Container c = getContentPane();
      c.setLayout(new GridLayout(0,1));
      c.add(label[0]);
      c.add(label[1]);
      pack();
      setVisible(true);
   }
   public static void main(String args[]) {
      new Test();
   }
}


Or if you're feeling lazy, you can just run the attached JAR.
Offline trembovetski

Senior Devvie




If only I knew what I'm talking about!


« Reply #7 - Posted 2009-01-08 21:45:46 »

The problem is that super.paintComponent() will override your setting.

Dmitri
Offline woogley
« Reply #8 - Posted 2009-01-09 02:36:11 »

The problem is that super.paintComponent() will override your setting.

Dmitri


Did you run the example I attached? It works.
Offline JonathanC

Senior Newbie





« Reply #9 - Posted 2009-01-13 08:13:25 »

Did you run the example I attached? It works.

Not on my Mac, for the reasons that Dmitri stated. It does work on my PC, which I should have stated in the OP.
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.

PocketCrafter7 (14 views)
2014-11-28 16:25:35

PocketCrafter7 (9 views)
2014-11-28 16:25:09

PocketCrafter7 (10 views)
2014-11-28 16:24:29

toopeicgaming1999 (76 views)
2014-11-26 15:22:04

toopeicgaming1999 (67 views)
2014-11-26 15:20:36

toopeicgaming1999 (16 views)
2014-11-26 15:20:08

SHC (30 views)
2014-11-25 12:00:59

SHC (28 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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