开发者

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,

thanks.

To clarify, a quad ca开发者_运维问答n 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.

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


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

Graphics2D draw image inside of defined quadrilateral

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

Graphics2D draw image inside of defined quadrilateral

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜