Java Antialiasing Help

closed account (EAUX92yv)
I have been doing some Java work and watching tutorials. I am running into a problem: My program no longer runs. Can someone please point out my error to me?

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
import java.awt.*;
import javax.swing.JFrame;
public class Main extends JFrame {
	public static void main (String [] args){
		DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
		Main m = new Main();
		m.run(dm);
	}
	public void run (DisplayMode dm){
		setBackground(Color.WHITE);
		setForeground (Color.BLACK);
		setFont(new Font("Arial", Font.PLAIN, 24));
		Screen s = new Screen();
		try{
			s.setFullScreen(dm, this);
			try{
				Thread.sleep(5000);
			}catch (Exception ex){}
		}finally{
			s.restoreScreen();
		}
	}
	public void paint (Graphics g) {
	if (g instanceof Graphics2D){
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	}
	g.drawString("This is a test", 200, 200);
	}
}


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
import java.awt.*;
import javax.swing.JFrame;
public class Screen {
	private GraphicsDevice vc;
	public Screen (){
		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
		vc = env.getDefaultScreenDevice();
	}
	public void setFullScreen(DisplayMode dm, JFrame window){
		window.setUndecorated(true);
		window.setResizable(false);
		vc.setFullScreenWindow(window);
		if (dm != null && vc.isDisplayChangeSupported());{
			try{
				vc.setDisplayMode(dm);
			}catch(Exception ex) {}
		}
	}
	public Window getFullScreenWindow(){
		return vc.getFullScreenWindow();
	}
	public void restoreScreen () {
		Window w = vc.getFullScreenWindow();
		if (w !=null){
			w.dispose();
		}
		vc.setFullScreenWindow(null);
	}
}



When I remove this if statement,

1
2
3
4
if (g instanceof Graphics2D){
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	}



my program outputs fine. But with the if statement, I get a black screen for five seconds. Thanks in advance!
closed account (N36fSL3A)
This would be better if you moved it to general C++ programming.
not really considering it isnt c++

this is the perfect place for it
closed account (N36fSL3A)
Meh. I guess so. But if it was me I'd do that.
AFAIK both AWT and Swing are both deprecated in favor of JavaFX. Have you tried messing with JavaFX? I've used it and it's actually quite nice ;)
one problem might be on line 13. The misplaced ;
Haha, a common argument for people who like opening braces on the same line is that it prevents that very mistake.
closed account (EAUX92yv)
I tried removing that ;, but that didn't fix it. And this post should not have been in general C++, being that it is Java.

EDIT: Here is the program output:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Antialiased text mode is not compatible with Global antialiasing enable key
	at sun.java2d.SunGraphics2D.setRenderingHint(Unknown Source)
	at Main.paint(Main.java:26)
	at javax.swing.RepaintManager$3.run(Unknown Source)
	at javax.swing.RepaintManager$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.access$1000(Unknown Source)
	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$200(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

Last edited on
Topic archived. No new replies allowed.