Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java paint image on negative coordinates

I want to do something best explained by this picture:

image on jpanel

In words, i need to right-down part of picture into left-top part of jpanel, i have tried setting x and y to negative, but with no succes, the image is just not displayed at all. It there a simple way to do this?

I am currently using

g.drawImage(img, x, y, null)
//x and y are negative

to draw it.

like image 806
kajacx Avatar asked Aug 25 '26 15:08

kajacx


1 Answers

I would verify you x/y position is being calculated correctly and add a ImageObserver reference to your g.drawImage if you're painting to the screen

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class NegativeImage {

    public static void main(String[] args) {
        new NegativeImage();
    }

    public NegativeImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;

        public TestPane() {
            try {
                img = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/Evil_Small.jpg"));
            } catch (IOException ex) {
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            int x = (getWidth() - img.getWidth()) / 2;
            int y = (getHeight()- img.getHeight()) / 2;
            // Center...
            g.drawImage(img, x, y, this);

            // Off to the left...
            x = -(img.getWidth() / 2);
            g.drawImage(img, x, y, this);

            // Off to the right...
            x = getWidth() - (img.getWidth() / 2);
            g.drawImage(img, x, y, this);
        }        
    }    
}
like image 84
MadProgrammer Avatar answered Aug 28 '26 04:08

MadProgrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!