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

发布于 2024-11-16 20:02:37 字数 218 浏览 3 评论 0原文

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

谢谢。

澄清一下,四边形可以由任何具有四个边的物体来定义;这意味着菱形或矩形或更复杂的形状。

Mre 删除了他的许多言论,所以我似乎没有回应任何人,但我在评论中所说的只是对 mre 所说内容的回应。

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,

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遇见了你 2024-11-23 20:02:37

有关基础知识,请参阅 Andrew Thomson 的解决方案

我没有使用“文本形状”,而是使用以下方法创建了一个形状:

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);

See Andrew Thomson's solution for the basics.

Instead of using a "text shape", I created a Shape using:

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);
寂寞花火° 2024-11-23 20:02:37

继承图形 图像绘制方法

  1. drawImage(图像 img, int x, int y, 颜色 bgcolor, ImageObserver 观察者)
  2. drawImage(图像 img, int x, int y, ImageObserver 观察者)
  3. drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserverobserver)
  4. drawImage(图像 img, int x, int y, int width, int高度,ImageObserver 观察者)
  5. drawImage(图像 img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, 颜色 bgcolor, ImageObserver 观察者)
  6. <一个href="http://download.oracle.com/javase/6/docs/api/java/awt/Graphics.html#drawImage%28java.awt.Image,%20int,%20int,%20int,%20int,% 20int,%20int,%20int,%20int,%20java.awt.image.ImageObserver%29" rel="nofollow noreferrer">drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserverobserver)

选择你的毒药。由于您甚至无法找到这些,我假设在面对缩放和频繁渲染时详细了解中间图像是徒劳的。

示例 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);
    }
}

输出

输入图像描述这里

示例 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);
    }
}

输出

在此输入图像描述

Inherited Graphicsimage drawing methods

  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

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

enter image description here

Example 2 -- draw an image in a square

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

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文