Hello! Please tell me why the image is not drawn in the right coordinates? In MainClass , an MainClass object is ImageResizer , and ImageResizer draws a picture (with arrows, but this is off topic) with the parameters specified from MainClass . In this case, the image is drawn in (120,6) (or (120,36) considering the window pane), but in fact it is drawn in (250,6) .

ImageResizer:

 public class ImageResizer extends JComponent { private int x, y, width, heigth; private Image img; ImageResizer(int x, int y, int width, int heigth, Image img){ super.setBounds(x,y,width,heigth); this.x = x; this.y = y; this.width = width; this.heigth = heigth; this.img = img; } @Override protected void paintComponent(Graphics g) { Graphics2D gr = (Graphics2D)g; gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); gr.drawImage(img, x, y, width, heigth, null); try { ((Graphics2D) g).drawImage(ImageIO.read(new File("arrowAboveLeft.png")),x,y,20,20,null); ((Graphics2D) g).drawImage(ImageIO.read(new File("arrowAboveRight.png")),width-20,y,20,20,null); ((Graphics2D) g).drawImage(ImageIO.read(new File("arrowBelowLeft.png")),x,heigth-20,20,20,null); ((Graphics2D) g).drawImage(ImageIO.read(new File("arrowBelowRight.png")),width-20,heigth-20,20,20,null); } catch (IOException e) { e.printStackTrace(); } } public int getX(){ return x; } public int getY() { return y; } public int getWidth() { return width; } public int getHeigth() { return heigth; } } 

MainClass:

 public class MainClass { public static void main(String[] args) throws IOException { BufferedImage img = ImageIO.read(new File("digital.jpg")); ImageResizer ir = new ImageResizer(120, 6, img.getWidth(), img.getHeight(), img); JFrame frame = new JFrame(); JPanel panel = new JPanel(null); panel.add(ir); frame.setBounds(200, 200, 400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.setVisible(true); frame.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseMoved(MouseEvent e) { int x = ir.getX(); int y = ir.getY(); int width = ir.getWidth(); int heigth = ir.getHeight(); int eX = e.getX(); int eY = e.getY(); if ((eX >= x))//&& (eX <=x+width) && e.getY()>=y+30 && e.getY()<=y+heigth+30) System.out.println("Here: " + eX + ", " + eY); } }); }} 
  • suppose that setBounds in ImageResizer sets the coordinates of the component in the container system (120 to the right of the left side of the frame), and paintComponent draws relative to the left side of the ImageResizer , i.e. you retreat twice to 120. - zRrr
  • Thank you very much! Yes, it was the case. Corrected. - Romeon0

0