3

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.

tenorsax
  • 21,123
  • 9
  • 60
  • 107
Jeremy
  • 41
  • 1
  • 5
  • Do you mean a quadrilateral like a rectangle (all angles at 90 degrees)? If not it might get complicated. I guess you would have to redefine your image, still as a rectangle, but with some transparent parts and then superpose this image wherever. – toto2 Jun 24 '11 at 01:43
  • @mre You;ve misunderstood my question. @toto No, not all angle are of 90 degrees. Thanks for your suggested solution but the system I've setup would be defeated if I changed the image source itself. – Jeremy Jun 24 '11 at 01:57
  • @mre Define Quadrilateral: A four-sided figure. That is not to assume all angles of which are 90 degrees. Hence why I said Quadrilateral and not Rectangle. – Jeremy Jun 24 '11 at 02:00
  • @mre I cannot believe you went so far as to rate down my question... This is a lot of work for asking what I thought would be a questions easily understood. Your example showed how I might render a rectangle - a particular sort of quad; that isn't what I asked for. I am looking for a method which will render a defined quad of any sort. Calm down, if you don't want to help me find an answer, this questions ins't for you. I'm not going to accept your excuses, and you don't need to make any. It was a simple misunderstanding. – Jeremy Jun 24 '11 at 02:33
  • @Jeremy, I'm not giving you excuses. I answered the question you asked. The examples I provided are irrelevant -- they are simply examples. Look at the list of image drawing methods I provided. If none of those work for you, then use another framework. Stop telling me to calm down; it's condescending and rather annoying. – mre Jun 24 '11 at 02:44
  • "Use another framework" does not constitute a satisfactory answer; and the methods described in your post do not sum of the capabilities of the entire graphics framework shipped with java and so I will wait for more answers and not "examples" of what I'm looking for. You "answer," which explains how to draw a specific quad does not explain how to generically draw a quad of any form. Your "Example" of drawing a generic quad of x is not a solution. Please, leave if you are as stubborn as you seem to be. I didn't come here to argue what I asked but rather for an answer for what I asked. – Jeremy Jun 24 '11 at 02:54
  • Sorry if it seems as though I'm talking to myself in this conversation, mre has apparently removed many of his comments to which I've responded to. However I lack the ability to remove my own remarks as I am only a guest to the site. – Jeremy Jun 24 '11 at 03:01
  • @Jeremy, Look at items 5 and 6. A quadrilateral consists of 4 points -- this method allows you define those 4 points. If that's unsatisfactory, then I'd recommend editing your question and elaborating so others don't waste their time and get belittled in the process. :) – mre Jun 24 '11 at 03:04
  • It allows one to define the top left and the bottom right points. The remaining two are assumed to form a rectangle. This still does not allow me to do what I wished for. d*1 & d*2 are the x and y coords for the destination's two points, s*1 & s*2 are the same but for the source. I believe you misunderstand the function of items 5 & 6. – Jeremy Jun 24 '11 at 03:08

2 Answers2

7

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);
Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
4

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

mre
  • 43,520
  • 33
  • 120
  • 170
  • 1
    I do not recall stating that all angles of the quad would be of 90 degrees and so this solution isn't what I'm looking for. I am well aware of what you've said. Thanks for the effort though :) – Jeremy Jun 24 '11 at 01:55
  • 1
    @Jeremy use transforms to rotate, scale and shear the image – Romain Hippeau Jun 24 '11 at 02:01
  • 1
    @Mre, I am aware of the shear transform however it isnt what I am looking for. I am given four vertices in 2d space of which form a quad, I cannot simply shear the image - well I could but it would likely require an unmentioned mathematical formula and so your solution has still not answered the question. I didn't mean to anger or offend you, so please lets keep the conversation clean. – Jeremy Jun 24 '11 at 02:10
  • @mre sorry for that. I must've gotten mixed up in the conversation. – Jeremy Jun 24 '11 at 02:16