2011-09-08 42 views
3

我有Line2D和Arc2D对象布局在我的JPanel Graphics2D绘图。你可以在这个问题上看看它的一部分“How to make pixel perfect Line2D in - Graphics2D”。现在我想实现的是,我想为所有Line2D和Arc2D对象创建两条平行线和弧线。在视觉上,Line2D装饰提示需要 - Graphics2D

普通的Line2D和的Arc2D目前得出,

enter image description here

想装饰它这样,

enter image description here

我的思想至今,

我可能通过创建两个来实现这一点不同的线条,并从我的法线位置给出偏移+间隙和间隙。但是,这会使很多我不想要的对象。

现在,是有可能使我的法线厚这样,

enter image description here

,给他们一个边界,并从中删除中间位?

是否有可能实现这一目标?如果是的话,请给我一些方向。

谢谢你的任何帮助。

+1

*“这将使得大量的对象” * A“第二行”(或它们中的100,用于一百初级线)是没有多少内存来保存。 –

回答

3

使用BasicStroke并绘制两次,更厚更薄。

One line drawn twice

import java.awt.*; 
import java.awt.image.*; 
import java.awt.geom.*; 
import javax.swing.*; 

import javax.imageio.ImageIO; 
import java.io.File; 

class PaintThick { 

    public static void main(String[] args) throws Exception { 
     int size = 150; 
     final BufferedImage bi = new BufferedImage(
      size,size,BufferedImage.TYPE_INT_RGB); 
     Graphics2D g = bi.createGraphics(); 

     double pad = 20; 
     Line2D.Double line1 = new Line2D.Double(
      pad,pad,(double)(size-pad),(double)(size-pad)); 
     int cap = BasicStroke.CAP_BUTT; 
     int join = BasicStroke.JOIN_MITER; 
     BasicStroke thick = new BasicStroke(15,cap,join); 
     BasicStroke thinner = new BasicStroke(13,cap,join); 

     g.setColor(Color.WHITE); 
     g.fillRect(0,0,size,size); 

     g.setColor(Color.BLACK); 
     g.setStroke(thick); 
     g.draw(line1); 

     g.setColor(Color.WHITE); 
     g.setStroke(thinner); 
     g.draw(line1); 

     ImageIO.write(bi,"png",new File("img.png")); 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JOptionPane.showMessageDialog(
        null, new JLabel(new ImageIcon(bi))); 
      } 
     }); 
    } 
} 
+0

它应该是白色的内部? – dacwe

+0

+1这是一个功能! – trashgod

+0

@dacwe你期望什么颜色?你建议什么颜色? –

3

您可以实现Stroke界面创建CompositeStroke,如图所示here

enter image description here

import java.awt.*; 
import java.awt.image.*; 
import java.awt.geom.*; 
import javax.swing.*; 

/** 
* @see http://www.jhlabs.com/java/java2d/strokes/ 
* @see http://stackoverflow.com/questions/7342979 
*/ 
class StrokeTest { 

    private static final int SIZE = 200; 
    private static final double PAD = 20d; 

    private static class CompositeStroke implements Stroke { 

     private Stroke stroke1, stroke2; 

     public CompositeStroke(Stroke stroke1, Stroke stroke2) { 
      this.stroke1 = stroke1; 
      this.stroke2 = stroke2; 
     } 

     @Override 
     public Shape createStrokedShape(Shape shape) { 
      return stroke2.createStrokedShape(
       stroke1.createStrokedShape(shape)); 
     } 
    } 

    public static void main(String[] args) throws Exception { 
     final BufferedImage bi = new BufferedImage(
      SIZE, SIZE, BufferedImage.TYPE_INT_RGB); 
     Graphics2D g = bi.createGraphics(); 
     g.setRenderingHint(
      RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 
     Arc2D.Double shape = new Arc2D.Double(PAD, 2 * PAD, 
      (SIZE - 2 * PAD), (SIZE - 2 * PAD), 0, 180d, Arc2D.OPEN); 
     g.setColor(Color.white); 
     g.fillRect(0, 0, SIZE, SIZE); 
     BasicStroke s1 = new BasicStroke(16f); 
     BasicStroke s2 = new BasicStroke(1f); 
     g.setStroke(new CompositeStroke(s1, s2)); 
     g.setColor(Color.black); 
     g.draw(shape); 

     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JFrame f = new JFrame(); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.add(new JLabel(new ImageIcon(bi))); 
       f.pack(); 
       f.setVisible(true); 
      } 
     }); 
    } 
} 
+1

我在代码和屏幕截图之前投了票!我可以再次投票吗?对抗锯齿的好消息。 –

+0

非常感谢,我希望StackOverFlow让用户选择他们想要授予多少名誉:)就像我面前有两个答案,他们都很好。 – doNotCheckMyBlog