【问题标题】:Texture/background Image for polygon多边形的纹理/背景图像
【发布时间】:2014-02-07 16:05:24
【问题描述】:

我正在使用 java swing 编写一个带有六角形瓷砖的瓷砖游戏板。我可以在this SOF 线程的帮助下绘制多边形。

现在我想为这些六边形添加背景图像,但我完全不知道该怎么做。 Here 是一个在“矩形”上绘制背景的教程,但我如何在 Hexagon 上做同样的事情?

【问题讨论】:

    标签: java swing textures polygon


    【解决方案1】:

    像这样:

    import java.awt.*;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.GeneralPath;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    class TexturedShape {
    
        public static BufferedImage getTexturedImage(
                BufferedImage src, Shape shp, int x, int y) {
            Rectangle r = shp.getBounds();
            // create a transparent image with 1 px padding.
            BufferedImage tmp = new BufferedImage(
                    r.width+2,r.height+2,BufferedImage.TYPE_INT_ARGB);
            // get the graphics object
            Graphics2D g = tmp.createGraphics();
            // set some nice rendering hints
            g.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING, 
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(
                    RenderingHints.KEY_COLOR_RENDERING, 
                    RenderingHints.VALUE_COLOR_RENDER_QUALITY);
            // create a transform to center the shape in the image
            AffineTransform centerTransform = AffineTransform.
                    getTranslateInstance(-r.x+1, -r.y+1);
            // set the transform to the graphics object
            g.setTransform(centerTransform);
            // set the shape as the clip
            g.setClip(shp);
            // draw the image
            g.drawImage(src, x, y, null);
            // clear the clip
            g.setClip(null);
            // draw the shape as an outline
            g.setColor(Color.RED);
            g.setStroke(new BasicStroke(1f));
            g.draw(shp);
            // dispose of any graphics object we explicitly create
            g.dispose();
    
            return tmp;
        }
    
        public static Shape getPointedShape(int points, int radius) {
            double angle = Math.PI * 2 / points;
    
            GeneralPath p = new GeneralPath();
            for (int ii = 0; ii < points; ii++) {
                double a = angle * ii;
    
                double x = (Math.cos(a) * radius) + radius;
                double y = (Math.sin(a) * radius) + radius;
                if (ii == 0) {
                    p.moveTo(x, y);
                } else {
                    p.lineTo(x, y);
                }
            }
            p.closePath();
    
            return p;
        }
    
        public static void main(String[] args) throws Exception {
            URL url = new URL("http://i.stack.imgur.com/7bI1Y.jpg");
            BufferedImage bi = ImageIO.read(url);
            Shape hxgn = getPointedShape(6, 32);
            final BufferedImage txtr = getTexturedImage(bi, hxgn, -200, -120);
            Runnable r = new Runnable() {
                @Override
                public void run() {
    
                    JOptionPane.showMessageDialog(null,
                            new JLabel(new ImageIcon(txtr)));
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
            SwingUtilities.invokeLater(r);
        }
    }
    

    【讨论】:

    • 您能否解释一下“将形状设置为 Graphics2D 对象的剪辑”?我完全不明白
    • 意识到我忘记了一两步,我决定通过实施将我的清单放入“酸性测试”中。完整实现见上文(在代码中)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多