Hi,

The Java Tutorials [1] say this about the dash_array parameter of BasicStroke():

The dash array defines the dash pattern. Alternating elements in the array represent the dash length and the length of the space between dashes in user coordinate units. Element 0 represents the first dash, element 1 the first space, and so on.

However, in openjdk6 (and openjdk7 - I havent tested explicitly, just looked at the code) it seems to be the other way around: element 0 represent the first space and element 1 the first dash (and so on). The attached program, when run with openjdk6, produces a solid black border around the button text instead of no border.

The attached patch tries to fix the problem by initializing the renderer with dashOn = true so that the first element of the array produces a dash. The second element then produces a space and so on.

Cheers,
Omair

[1] http://java.sun.com/docs/books/tutorial/2d/geometry/strokeandfill.html
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class DashStrokeButton extends JButton {

    public DashStrokeButton(String string) {
        super(string);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D graphics = (Graphics2D) g;

        // BasicStroke dashStroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND,
        // BasicStroke.JOIN_ROUND, 1.0f, new float[] { 0.0f, 2.0f }, 0.0f);

        BasicStroke dashStroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND, 5.0f, new float[] { 0.0f, 10.0f },
                17.0f);

        graphics.setStroke(dashStroke);
        graphics.setColor(Color.BLACK);

        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_OFF);

        Insets insets = this.getInsets();
        graphics.drawRect(insets.left - 1, insets.top - 1, this.getWidth()
                - insets.left - insets.right + 2, this.getHeight() - insets.top
                - insets.bottom + 2);

    }

    public static void drawGui() {

        JFrame window = new JFrame("main window");

        DashStrokeButton dsb = new DashStrokeButton("Dash Button");
        window.getContentPane().add(dsb);

        window.pack();
        window.setVisible(true);
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                drawGui();
            }

        });
    }
}
--- jdk/src/share/classes/sun/java2d/pisces/Dasher.java.orig	2009-01-13 12:14:53.000000000 -0500
+++ jdk/src/share/classes/sun/java2d/pisces/Dasher.java	2009-01-13 12:15:09.000000000 -0500
@@ -120,7 +120,7 @@
 
         // Normalize so 0 <= phase < dash[0]
         int idx = 0;
-        dashOn = false;
+        dashOn = true;
         int d;
         while (phase >= (d = dash[idx])) {
             phase -= d;

Reply via email to