简体   繁体   English

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

[英]Graphics2D draw image inside of defined quadrilateral

I cannot find a draw image overload inside of Graphics2D which will enable me to perform such a task, can someone help me figure out how one might do this - preferably without swapping to more advanced graphics frameworks such as OpenGl, 我无法在Graphics2D内部找到绘制图像重载,这将使我能够执行这样的任务,有人可以帮助我弄清楚如何做到这一点-最好不用交换到更高级的图形框架(例如OpenGl),

thanks. 谢谢。

To clarify, a quad can be defined by anything with four-sides; 为了明确起见,可以用四边形的任何东西定义一个四边形。 that means a diamond or a rectangle or more elaborate shapes. 表示菱形,矩形或更多精致的形状。

Mre has removed many of his remarks and so It seems as though I am responding to no-one, however all I have said in the comments were responses to what mre had said. 姆雷删除了他的许多言论,因此似乎我没有回应,但是我在评论中只说了对姆雷所说内容的回应。

See Andrew Thomson's solution for the basics. 有关基础知识,请参阅Andrew Thomson的解决方案

Instead of using a "text shape", I created a Shape using: 我没有使用“文本形状”,而是使用以下方法创建了一个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);

Inherited Graphics image drawing methods 继承的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)

Choose your poison. 选择你的毒药。 Since you weren't even able to locate these, I'm assuming that going into detail about Intermediate Images when faced with scaling and frequent rendering would be futile. 由于您甚至无法找到这些图像,因此我假设在遇到缩放和频繁渲染时详细介绍中间图像将是徒劳的。

Example 1 -- drawing a circle in a square 示例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);
    }
}

Output 产量

在此输入图像描述

Example 2 -- draw an image in a square 示例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);
    }
}

Output 产量

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM