Graphics2D在定义的四边形内部绘制图像

时间:2011-06-24 00:44:16

标签: java image swing graphics2d

我无法在Graphics2D中找到绘图图像重载,这将使我能够执行这样的任务,有人可以帮我弄清楚如何做到这一点 - 最好不要交换到更高级的图形框架,如OpenGl,

感谢。

为了澄清,四边形可以由具有四边的任何东西定义;这意味着钻石或矩形或更复杂的形状。

Mre已经删除了他的许多言论,所以看起来好像我对没有人做出回应,但是我在评论中所说的只是回应了曾经说过的话。

2 个答案:

答案 0 :(得分:4)

请参阅Andrew Thomson's solution了解基础知识。

我没有使用“文字形状”,而是使用:

创建了一个Shape
Polygon polygon = new Polygon();
polygon.addPoint(250, 50);
polygon.addPoint(350, 50);
polygon.addPoint(450, 150);
polygon.addPoint(350, 150);
g.setClip(polygon);
g.drawImage(originalImage, 0, 0, null);

答案 1 :(得分:3)

继承Graphics图片绘制方法

  1. drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
  2. drawImage(Image img, int x, int y, ImageObserver observer)
  3. drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
  4. drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
  5. drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
  6. drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
  7. 选择你的毒药。由于你甚至无法找到这些,我假设在面对缩放时会详细介绍中间图像,频繁渲染将是徒劳的。

    示例1 - 在正方形中绘制圆圈

    public class DrawCircleInSquare {
    
        public static void main(String[] args){
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        private static void createAndShowGUI(){
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            final JPanel panel = new JPanel(){
    
                @Override
                protected void paintComponent(Graphics g){
                    Graphics2D g2 = (Graphics2D)g.create();
    
                    // Clear background to white
                    g2.setColor(Color.WHITE);
                    g2.clearRect(0, 0, getWidth(), getHeight());
    
                    // Draw square
                    g2.setColor(Color.BLACK);
                    g2.drawRect(50, 50, 100, 100);
    
                    // Draw circle inside square
                    g2.setColor(Color.RED);
                    g2.fillOval(88, 88, 24, 24);
    
                    g2.dispose();
                }
    
                @Override
                public Dimension getPreferredSize(){
                    return new Dimension(200, 200);
                }
            };
    
            frame.add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    

    <强>输出

    enter image description here

    示例2 - 以正方形

    绘制图像
    public class DrawImageInSquare {
    
        private static BufferedImage bi;
    
        public static void main(String[] args){
            try {
                // Load image
                loadImage();
    
                // Create and show GUI
                SwingUtilities.invokeLater(new Runnable(){
                    @Override
                    public void run() {
                        createAndShowGUI();
                    }
                });
            } catch (IOException e) {
                // handle exception
            }
        }
    
        private static void loadImage() throws IOException{
            bi = ImageIO.read(new File("src/resources/psyduck.png"));
        }
    
        private static void createAndShowGUI(){
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            final JPanel panel = new JPanel(){
    
                @Override
                protected void paintComponent(Graphics g){
                    Graphics2D g2 = (Graphics2D)g.create();
    
                    // Clear background to white
                    g2.setColor(Color.WHITE);
                    g2.clearRect(0, 0, getWidth(), getHeight());
    
                    // Draw square
                    g2.setColor(Color.BLACK);
                    g2.drawRect(50, 50, 100, 100);
    
                    // Draw image inside square
                    g2.setRenderingHint(
                            RenderingHints.KEY_INTERPOLATION, 
                            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                    g2.drawImage(bi, 50, 50, 100, 100, null);
    
                    g2.dispose();
                }
    
                @Override
                public Dimension getPreferredSize(){
                    return new Dimension(200, 200);
                }
            };
    
            frame.add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    

    <强>输出

    enter image description here