2013-04-21 99 views
0

我有以下代碼來顯示和繪製JPanel與緩衝的圖像。JPanel大小問題,當我調整

我將其大小設置爲緩衝圖像的大小,但實際上並未設置。

它顯示滾動條時我調整,但是當我調整比面板或BufferedImage的大小JFrame更大,我仍然得到鼠標事件,當我點擊面板的尺寸之外。

我省略了額外的代碼。

public class PaintFrame extends JFrame{  
     private JScrollPane paintScrollPane; 
     private Painter painter; 
     private JPanel paintPanel; 
     private BufferedImage paintImage; 
     private Color forgroundColor; 

     public PaintFrame(){ 
      super("Paint"); 
      setDefaultCloseOperation(EXIT_ON_CLOSE); 
      setPreferredSize(new Dimension(800, 600)); 
      setLocationByPlatform(true); 
      getContentPane().setBackground(Color.black); 

      paintImage = new BufferedImage(700, 500, BufferedImage.TYPE_3BYTE_BGR); 

      paintPanel = new JPanel(){ 
       @Override 
       public void paint(Graphics g){ 
        if(paintImage != null){ 
         g.drawImage(paintImage, 0, 0, paintImage.getWidth(), paintImage.getHeight(), null); 
        } 
       } 
      }; 
      paintPanel.setBackground(Color.white); 
      paintPanel.addMouseListener(new MouseAdapter() { 
       @Override 
       public void mouseClicked(MouseEvent me){ 
        mouseClickedOnPaint(me); 
       } 
      }); 
      paintPanel.setPreferredSize(new Dimension(paintImage.getWidth(), paintImage.getHeight())); 
      paintScrollPane = new JScrollPane(paintPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 

      getContentPane().add(paintScrollPane); 

      pack(); 
     } 
} 
+0

增加進口和註釋掉'Painter'後,我仍然得到一個'mouseClickedOnPaint(me);'錯誤。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-04-21 05:36:32

+0

@AndrewThompson你有沒有在粘貼? :P – Sinkingpoint 2013-04-21 05:37:55

+0

@Quirliom它的最後部分?當然。什麼,你覺得我很厚? – 2013-04-21 05:48:38

回答

2

由於Quirlom指出,LayoutManager S可根據有實現

的需求選擇忽略getPreferredSize值如果所有你擔心的是鼠標事件,你可以過濾出來通過確定鼠標事件是否在圖像內點擊。

否則,你需要找到更好滿足您需求的佈局管理器...

下面的例子只是確定是否鼠標事件屬於圖像的範圍內......

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.Rectangle; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class ClickImage { 

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

    public ClickImage() { 
     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 ImagePane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class ImagePane extends JPanel { 

     private BufferedImage background; 
     private List<Point> goodPoints; 
     private List<Point> badPoints; 

     public ImagePane() { 
      try { 
       background = ImageIO.read(new File("/Volumes/Disk02/Dropbox/MegaTokyo/url.jpg")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
      goodPoints = new ArrayList<>(5); 
      badPoints = new ArrayList<>(5); 
      addMouseListener(new MouseAdapter() { 
       @Override 
       public void mouseClicked(MouseEvent e) { 
        if (getImageBounds().contains(e.getPoint())) { 
         goodPoints.add(e.getPoint()); 
        } else { 
         badPoints.add(e.getPoint()); 
        } 
        repaint(); 
       } 
      }); 
     } 

     @Override 
     public void invalidate() { 
      goodPoints.clear(); 
      badPoints.clear(); 
      super.invalidate(); 
     } 

     public Rectangle getImageBounds() { 
      Rectangle bounds = new Rectangle(); 
      if (background != null) { 
       bounds.x = (getWidth() - background.getWidth())/2; 
       bounds.y = (getHeight() - background.getHeight())/2; 
       bounds.width = background.getWidth(); 
       bounds.height = background.getHeight(); 
      } 

      return bounds;    
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return background == null ? super.getPreferredSize() : getImageBounds().getSize(); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (background != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       Rectangle bounds = getImageBounds(); 
       g2d.drawImage(background, bounds.x, bounds.y, this); 

       g2d.setColor(Color.GREEN); 
       drawPoints(g2d, goodPoints); 
       g2d.setColor(Color.RED); 
       drawPoints(g2d, badPoints); 

       g2d.dispose(); 
      } 
     } 

     protected void drawPoints(Graphics2D g2d, List<Point> points) { 
      for (Point p : points) { 
       g2d.drawLine(p.x - 4, p.y - 4, p.x + 4, p.y + 4); 
       g2d.drawLine(p.x + 4, p.y - 4, p.x - 4, p.y + 4); 
      } 
     } 
    } 
} 

你也可能想看看A Visual Guide to Layout Managers

+0

+1形狀#包含()'。 – trashgod 2013-04-21 12:54:33