3

I have a java application that somehow has a different behavior when launched in windows by double clicking the jar file compared to launching it using the command prompt.

The behavior that i'm noticing specifically is when I override a JLabel to render better using the following

 lblDate = new ATimeLabel(ATimeLabel.DATE_LETTERS) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void paintComponent(Graphics g) {

            Graphics2D g2d = (Graphics2D) g;

            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                    RenderingHints.VALUE_FRACTIONALMETRICS_ON);

            super.paintComponent(g2d);
        }
    };

The effect appears when I launch the app from my IDE or launch the jar from the command prompt but when I double click my app it will show the label without the paintComponent() overriden effects.

would appreciate help to figure out exactly how to be able to have the same effect happen on double click of my app.

EDIT:

I should also mention that I add the following font changes after creating one of 2 JLabels

lblDate.setForeground(Color.gray);
lblDate.setFont(boldFont.deriveFont(Font.PLAIN, timeFontSize));

Here is a screen shot of what it looks like. The one on the left clearly has the anti-aliasing and clean text rendering i'm looking for while the one on the right is fatter and not as sharp. (I also temporary added the red border to show that the paint method is in effect)

EDIT 2:

Seems that when I double click the JVM is 1.7 and my IDE uses JDK 1.6 I don't understand why the RenderingHints for the fonts wouldn't look the same on Java6 and Java7 they are in both APIs and run without exceptions...

Any help would be appreciated.

Community
  • 1
  • 1
Sammy Guergachi
  • 1,986
  • 4
  • 26
  • 52
  • 2
    You should be careful making adjusments to the graphics context. Anything you set will be used to render all the following components. In this example, that means everything before you label won't have the antialiasing rendering hints, but everything after it will. Better to use `g.create()` to create a temporary copy and `Graphics#dispose` when your done (at the end of the method) – MadProgrammer Mar 18 '13 at 00:13
  • 2
    Verify same JVM levels for both. See http://stackoverflow.com/questions/5103121/how-to-find-the-jvm-version-from-a-java-program – Java42 Mar 18 '13 at 01:00

1 Answers1

1

Seems to work just fine for me...

From left to right, in IDE, from command line and double clicked...

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestPaintLabel {

    public static void main(String[] args) {
        new TestPaintLabel();
    }

    public TestPaintLabel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JLabel lblDate = new JLabel("Hello, is it me you're looking for?") {
                    private static final long serialVersionUID = 1L;

                    @Override
                    protected void paintComponent(Graphics g) {

                        Graphics2D g2d = (Graphics2D) g.create();

                        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                                RenderingHints.VALUE_RENDER_QUALITY);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
                        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                                RenderingHints.VALUE_FRACTIONALMETRICS_ON);

                        super.paintComponent(g2d);
                        g2d.setColor(Color.RED);
                        g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
                        g2d.dispose();

                    }

                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(200, 50);
                    }

                };

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(lblDate);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

Try adding some additional painting to determine if the paint method is actually been called.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I tried adding the red line and its visible in all cases, but the text still looks weird. I will update with a snapshot – Sammy Guergachi Mar 18 '13 at 00:49
  • What does the small graphic near the "G" in the title area do? Did you find a way to add objects to the title area? – Java42 Mar 18 '13 at 01:36
  • @Charles Nup, it's from an pre-existing application called DisplayFusion, which I use to produce multi-screen wallpapers and other interesting stuff. – MadProgrammer Mar 18 '13 at 01:45