How can I turn off anti-aliasing for one specific JLabel? It uses a very small font which might look better without anti-aliasing.
If important, I'm using Java 1.5 on Mac OS X.
Have you tried overriding paint(Graphics g) or paintComponent(Graphics g), setting rendering hints for text AA on the graphics object and calling the super method?
see RenderingHints:
https://docs.oracle.com/javase/7/docs/api/java/awt/RenderingHints.html#VALUE_TEXT_ANTIALIAS_OFF
I think you'll have to override the paint(Graphics g) method and draw the text yourself.
Related
I am currently programming a game which includes lots of graphical elements. Currently the code looks like this:
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(ImageLoader.gui_iconbar, xToRel(480), yToRel(810), xToRel(960), yToRel(270), null);
g.drawImage(ClientVariables.getSplashart(Main.selectedCharacter, 0), xToRel(495), yToRel(825), xToRel(135), yToRel(240), null);
//... (don't get confused with xToRel, it just makes changes to the drawing position if you move the camera)
}
The problem is: there are TONS of these g.draw(whatever); lines. I want to split them in multiple classes but just making 4 classes and copy paste gameFrame.add(GraphicclassXY); doesn't work, logically. Does anyone know how to fix this issue? (It's not a problem if the solution includes making changes from jlabel to canvas for example)
Create logical non-component classes (that don't extend JLabel, JPanel, JFrame, JComponent or other Swing component), give them a public void draw(Graphics g) method, give the class that does the drawing instances of your logical classes, perhaps within some sort of collection, and iterate through the collection and call draw on your instances within the paintComponent method.
I was taught in my class and also seen in this book (Big Java Early Objects) to include draw instructions in a class extending JComponent as:
public class Component extends JComponent {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Rectangle r = new Rectangle(0,0,20,10);
g2.draw(r);
}
}
However, someone pointed out that the first line in the paintComponent method should be: super.paintComponent(g);
Based on my limited knowledge, I believe it's calling JComponent's version of the method (now overridden). Why does this need to happen? What happens if I just ignore this statement as I've been doing until now?
Why does this need to happen? What happens if I just ignore this statement as I've been doing until now?
A component is responsible for painting itself completely. The default painting may be different for each LAF, so by invoking super.paintComponent() you make sure you get the default painting which will basically just be the background.
If you don't invoke this method you have the potential for painting artifacts to occur. Then may not always occur, but you don't want to waste time debugging.
Read the API for the paintComponent() method of JComponent. Among other things it states:
Further, if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color. If you do not honor the opaque property you will likely see visual artifacts.
So at a minimum you need:
g.fillRect(0, 0, getWidth(), getHeight());
to make sure the background is cleared before you start your custom painting.
I was wondering why displaying text in my frame has to be so blury, and i came across this piece of code, which is working by the way
public void paint(Graphics graphicsObject){
if(graphicsObject instanceof Graphics2D){
Graphics2D g2D = (Graphics2D) graphicsObject;
g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
graphicsObject.drawString("not blurry text display", 200, 200);
}
having dificulties trying to understand how this work out.
how come g2D.setRenderingHint is fixing my blurry problem, but i dont use it to paint my string?
First, what this fixed was not blurring. It was aliasing.
Aliasing occurs when drawing functions try to create curvy shapes using a raster image - a matrix of pixels, which are squares. If the lines are not vertical or horizontal, you end up with "stairs" - edges that are jaggy.
Antialiasing is a way to make this effect less visible to the eye, by using additional pixels around the drawn line, which are painted in different tones between the foreground and the background. This cheats our eyes to see the line as "smooth". If you zoom an image drawn with antialiasing, you may notice those pixels around the actual line.
So, actually, antialiasing blurs the line, and this makes it seem smoother to our eyes.
As to your actual question - you are using the graphics object to draw the line. You set the hint in the graphics object by accessing the object in its form as a Graphics2D. Even though you then go on and use the graphicsObject using its regular Graphics reference, the method drawString() is overridden. This means that it will be activated in the concrete object that implements it, which sees - and uses - the RenderingHint hash map where your hint is stored.
but i dont use it to paint my string?
Why do you think so? Because, you are actually using it.
Nowadays, Graphics is always a Graphics2D object, so your if-statement will be executed.
g2D and graphicsObject both point to the same object. For the compiler they are two variables of different types, allowing different methods to be called, but at execution time, these two variables actually point to the same object.
So by setting the rendering hint on g2D, and this being the same object as graphicsObject, drawing the string respects that hint and fixes the blurryness by using anti-aliasing as specified in the hint.
I am kind of newbie in Java and in my program, I have to draw some lines, arcs etc. in a function different from paintComponent() in some cases in a class that extends JPanel.
I generally work with ActionScript for visual stuff and I can draw any geometric figure in any functions when I work with AS.
So, is there way to do the same thing in Java, and how?
Thank you!
Why not draw in paintComponent(...)? You state your new, and perhaps you might misunderstand how to do Swing graphics best, so knowing your requirements and the rationale behind them can help us help you.
You can draw on a BufferedImage at any time and in any code
Then that image can be displayed either in a JComponent's paintComponent(...) method
or in a JLabel's Icon via its setIcon(...) method.
Be sure to read the Graphics2D API which will have all the methods needed to do your drawings.
Be sure to read the Swing graphics tutorials because doing this type of work requires a paradigm shift.
Do not get your Graphics object by calling getGraphics() on a component such as a JPanel. Either get it via calling getGraphics() on a BufferedImage or from the paintComponent(Graphics g) parameter.
in the a swing app i'm rendering text with a custom JComponent, using Graphics.drawString(). here is a sample:
aa text http://img525.imageshack.us/img525/4928/drawstringsample.jpg
in that same app, i'm rendering text using a JTextPane. here is a sample:
alt text http://img28.imageshack.us/img28/1134/jtextpanesample.jpg
can you notice how the lower sample is a little 'smudged'? well, i can't figure out how to make it look like the upper sample.
thanks, asaf :-)
update:
System.setProperty("awt.useSystemAAFontSettings","false") and "lcd" too aren't working.
((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF) in paint() isn't working
putClientProperty(sun.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, Boolean.TRUE) gives java.lang.ClassCastException: java.lang.Boolean cannot be cast to sun.swing.SwingUtilities2$AATextInfo
This will result in an antialiased font in a JLabel. Make sure you call super.paintComponent(g); after setting the RenderingHints.
JLabel lblFont = new JLabel(){
#Override
public void paintComponent(Graphics g) {
Graphics2D graphics2d = (Graphics2D) g;
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
super.paintComponent(g);
}
};
putClientProperty(SwingUtilities2.AA_TEXT_PROPERTY_KEY, null);
If you want your result to look like the the upper sample, then you want to disable anti-aliasing.
The first sample in your question has anti-aliasing disabled and the second sample has it enabled.
According to http://mindprod.com/jgloss/antialiasing.html the following code should help:
jtextArea.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, Boolean.TRUE);
Notice that the reference to com.sun.java.* will make your application non-portable to non-Sun JVMs (and possibly to different versions of Sun JVMs).