How to make text in swing more clear - java

May I ask if it is possible to make the text in swing more clear?
Here's how the word "File" looks in JavaFX:
And here's how it looks in Swing:
The JavaFX one is absolutely better. Is there any way to make the font in Swing look like the one in JavaFX?
P.S. Both of them are using the font "Microsoft YaHei" at 12px
I found a method to open/close anti-alias like
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,RenderingHints.VALUE_STROKE_DEFAULT);
g2d.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g2d.drawString("File", 50, 50);
}
But it seems that it does not work well
These are my result in testing different fonts:
Times New Roman
Swing
"File" in times new roman
JavaFX
"File" in times new roman

I suppose it is the problem of my jdk. After I change it to jdk 11, it works well. (it is my font in jdk 11)
jdk11's "File"

Related

Can I print the rendered HTML of a JLabel? [duplicate]

I just started my Java programming 3 months before and here with my issue that is-
How to copy a JLabel or JPanel Graphics to another JLabel or JPanel.
I have used -
<!--Source JLabel srcLabel-->
JLabel dest = new JLabel();
dest.paint(srcLabel.getGraphics());
panel.add(dest);
dest.validate();
but due to lack of knowledge I stucked here. Please help.
Start by having a look at Painting in AWT and Swing
and Performing Custom Painting for more information about how painting works.
Never use getGraphics, this is just a bad idea and will cause you no end of issues.
Generally speaking, you should avoid calling paint directly, and instead use print or printAll. This will disable the double buffering inherent in the normal painting process which can issues
JLabel srcLabel = new JLabel();
JLabel dest = new JLabel();
BufferedImage img = new BufferedImage(srcLabel.getWidth(), srcLabel.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
srcLabel.printAll(g2d);
g2d.dispose();
dest.setIcon(new ImageIcon(img));
This assumes that srcLabel has already been displayed and laid out.
Now the question is why? Wouldn't be easier to simply set the text and icon properties of the second label so it matches the first?

How to make JPanel use JTabbedPane UI without tabs using Aqua/system LAF?

First off, I have tried other solutions. Unfortunately they rely on you using a cross platform LAF.
A panel in OS X's System preferences:
looks identical to a JTabbedPane:
but without the tabs. I am trying to make something that feels native, but I can't find any other components with this UI, and I can't figure out how to hack a JTabbedPane so it displays normally just without tabs. Any ideas?
I can't figure out how to hack a JTabbedPane so it displays normally just without tabs.
I assume you are trying to display multiple panels in the same space. If so, then you can use a CardLayout. You can swap panels by specifying the name of the panel to display in the CardLayout.
Check out the section from the Swing tutorial on How to Use CardLayout for more information and examples.
So I ended up just working on my paintComponent method, and I think this makes it look pretty accurate:
#Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(new Color(212, 212, 212));
g2.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
g2.setColor(new Color(223, 223, 223));
g2.fillRoundRect(0, 1, this.getWidth(), this.getHeight()-1, 10, 10);
g2.setColor(new Color(227, 227, 227));
g2.fillRoundRect(1, 1, this.getWidth()-2, this.getHeight()-2, 10, 10);
}
I think it could be optimized a bit by changing fill() to draw() but this gets the job done. Unfortunately it won't look great on other operating systems, so I will have to handle those separately.
Edit: matched colors using Photoshop.

Slick2D - How to use Fonts

I am coding a game in fonts and I encountered a problem.
I have no idea how to use fonts even though I tried everything.
Can you also please explain what is a glyph?
And where to get .fnt files if that is needed?
A lot of that stuff is either no longer used, or just not necessary anymore. Basically all you need is a Graphics instance, and to use something like the drawString method. You can also use setFont on the Graphics object to set the font you want to use for rendering.
You can get a Graphics instance from your GameContainer, and then just render to it. If you want to see an example, the render method of this file in my project on github has some lines in it that render debug information to the screen (fps, memory usage, etc.). The relevant part of the code is:
if (ConfigValues.renderSystemInfo) {
g.setColor(new Color(50, 50, 50, 180));
g.fillRect(0, 0, 300, 70);
g.setColor(Color.white);
g.drawString("MEM total(used): " + (Runtime.getRuntime().totalMemory()/1000000) + "(" + ((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1000000) + ") MB", 10, 25);
g.drawString("Ped. history size: " + (peds.size()*peds.get(0).getMovementHistory().size()) + " nodes", 10, 40);
}
Just for reference, a glyph, at least conceptually, is just the visual representation of a character in a font. So, the way that the letter a looks in a given font, for example, is that font's glyph for that letter. There's a different glyph for upper and lowercase (a or A), which is what makes the upper and lowercase letters appear different in fonts.
Font font = new Font('Verdana', Font.BOLD, 32);
TrueTypeFont ttf = new TrueTypeFont(font, true);
In your update method:
ttf.drawString(32.0f, 32.0f, "Your words here", Color.green);

Drawing Glassy Buttons

I'm going to draw some sort of glassy buttons in java me (targeting devices with MIDP 2.0).
An Example:
Actually I need to impelement Gradient and Bevel-Emboss effects in Java ME, do you have any opinion or "guideline" on how to implement this?
EDIT: Now I know how to draw gradient background, but That is not sufficient.
Is it possible to draw such glassy button in Java ME?
I've worked with C# and I can draw these kinds of glassy buttons there, but I'm struggling on how to simulate something like these buttons in Java ME or at least something near to them, Note that I'm looking for a good guidance and help to move forward.
Do I need to provide more information? If so, please let me know.
Thanks In advance.
You can do it by using an alpha gradient paint. Heres an example:
Graphics2D g = (Graphics2D)screen.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setFont(new Font("Serif", Font.BOLD, 30));
Color c1 = new Color(0,0,0,0);
Color c2 = new Color(0,0,0,100);
GradientPaint gradient = new GradientPaint(10,8,c1,10,40,c2,true);
g.setColor(Color.GREEN);
g.fillRect(0, 0, screen.width, screen.height);
g.setColor(Color.BLACK);
g.setPaint(gradient);
g.fillRoundRect(100, 100, 200, 50, 25, 25);
g.setPaint(Color.BLACK);
g.drawRoundRect(100, 100, 200, 50, 25, 25);
g.drawString("Hello World!", 118, 135);
it will look like this:
You can use LWUIT framework for developing java-me (MIDP 2.0) applications. Its good GUI framework for java-me application. Here you can create the theme manually using ResourceEdit. For more info see this discussion and LWUIT blog also.
yoy can not do so in me because it dont have such reach effect on UI
get image that have effect like that gredient and make it transparent using
btn.getStyle().setBgTransparency(100) // from 0-255
note: you must use images that is already semi transparent , if not than setBgTransparency would not work properly
I can not recommend you to use canvas
i recommend you to use use LWUIT
it have many more effects ads you required ,like
btnBuzz.getStyle().setBorder(Border.createBevelLowered());
it have different layout as well

how to make JTextPane paint anti-aliased font?

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).

Categories