2013-09-24 121 views
2

我正在嘗試爲2D RPG製作地圖編輯器,而我目前正在嘗試做的事(將地面貼圖放置在地面上)是讓帶有JLabel的JScrollPane(其中包含圖像)和附加到JScrollPane的鼠標偵聽器,以確定圖像的X和Y位置。我碰到的問題是,它沒有得到Images X和Y的位置,而是JScrollPanes X和Y的位置。我如何獲得JScrollPane中圖像的X和Y位置

所以我有一個JScrollPane附加到512x4928的圖像上,我附加了一個鼠標監聽器。當我嘗試獲取Y位置時,問題仍然存在,因爲JScrollPane是一個單獨的對象,它獲取的JScrollPane的大小的X和Y JScrollPanes大小爲512x600 無論圖像在哪裏,它都不會返回值大於600.

任何方式,我可以使這項工作?

繼承人的代碼

public void loadMapTileImage(){ 
    try { 
     image = ImageIO.read(getClass().getResource("data/misc/tiledata.png")); 
     image = image.getSubimage(0, 0, 512, 4928); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    ImageIcon i = new ImageIcon(image); 
    MapEditorGlobalObjects.mapTileScroll = new JScrollPane(new JLabel(i)); 
} 

mapTileScroller.addMouseListener(new MouseListener() { 

     @Override 
     public void mouseReleased(MouseEvent arg0) { 
      MapEditorGlobalObjects.checkIfDebugging("Mouse Released Location X = "+arg0.getX()); 
      MapEditorGlobalObjects.checkIfDebugging("Mouse Released Location Y = "+arg0.getY()); 

     } 
     @Override 
     public void mousePressed(MouseEvent arg0) { 
      MapEditorGlobalObjects.checkIfDebugging("Mouse Clicked Location X = "+arg0.getX()); 
      MapEditorGlobalObjects.checkIfDebugging("Mouse Clicked Location Y = "+arg0.getY()); 

     } 

回答

4

代替添加MouseListener到滾動窗格的,嘗試將其添加到滾動窗格的視圖分量(即,事情,滾動窗格顯示的是)

在本例中的圖像是2560×1600

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.FontMetrics; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
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.JScrollPane; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestScrollPanePos { 

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

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

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new JScrollPane(new TestPane())); 
       frame.setSize(400, 400); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private BufferedImage bg; 
     private Point point; 

     public TestPane() { 
      try { 
       bg = ImageIO.read(new File("/path/to/image)); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      addMouseMotionListener(new MouseAdapter() { 

       @Override 
       public void mouseMoved(MouseEvent e) { 
        System.out.println(e.getPoint()); 
        point = e.getPoint(); 
        repaint(); 
       } 

      }); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return bg == null ? new Dimension(200, 200) : new Dimension(bg.getWidth(), bg.getHeight()); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (bg != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       int x = (getWidth() - bg.getWidth())/2; 
       int y = (getHeight()- bg.getHeight())/2; 
       g2d.drawImage(bg, x, y, this); 
       if (point != null) { 
        FontMetrics fm = g2d.getFontMetrics(); 
        g2d.setColor(Color.BLACK); 
        g2d.drawString(point.x + "x" + point.y, point.x, point.y - fm.getHeight() + fm.getAscent()); 
       } 
       g2d.dispose(); 
      } 
     } 
    } 

} 
+0

不錯!我喜歡那樣,更乾淨。謝謝你教我新的東西。 – Loligans

+0

我喜歡簡單;) – MadProgrammer

0

如果使用JScrollPane的這一點,得到了垂直滾動條的值,並將其從鼠標監聽器添加到Y值,你就會有正確的價值

相關問題