2013-02-10 70 views
0

时,我一直在努力,需要在图像上线的画时,通过鼠标拖动事件触发的程序,但不是被称为() paintComponent方法不会执行。我已阅读摆动图形教程和也有从这个其他程序员一些投入,但至今仍未能找到适合我的解决方案。的paintComponent()我当调用的paintComponent通过重绘由代码内重绘

我已经贴小SSCCE以帮助我的程序按预期在尝试简化此为我自己和任何人找过我的代码不充当内精确定位的区号。

预先感谢任何人,花时间去寻找这个在我。

下面是两个独立的类我使用。

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JLayeredPane; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 

public class TestGraphics { 
private JLayeredPane contentPane; 

public void newImage() { 
    try { 
     JFileChooser fileChooser = new JFileChooser("."); 
     int status = fileChooser.showOpenDialog(null); 

     if (status == JFileChooser.APPROVE_OPTION) { 
      File selectedFile = fileChooser.getSelectedFile(); 
      System.out.println("The selected file is from the: " + selectedFile.getParent() + " Drive"); 
      System.out.println("Name of file: " + selectedFile.getName()); 
      System.out.println("Opening file"); 

      BufferedImage buffImage = ImageIO.read(new File(selectedFile.getAbsolutePath())); 
      ImageIcon image = new ImageIcon(buffImage); 
      JLabel label = new JLabel(image); 
      label.setSize(label.getPreferredSize()); 

      label.setLocation(0, 0); 

      contentPane = new JLayeredPane(); 
      contentPane.setBackground(Color.WHITE); 
      contentPane.setOpaque(true); 
      //getTabbedPane().setComponentAt(tabNum, contentPane); 
      contentPane.add(label); 
      contentPane.setPreferredSize(new Dimension(label.getWidth(), label.getHeight())); 

      Segmentation segmentation = new Segmentation(); 
      segmentation.addListeners(label); //call to addListeners method 

      JFrame frame = new JFrame(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.getContentPane().add(contentPane); 
      frame.setResizable(false); 
      frame.pack(); 
      frame.setLocationByPlatform(true); 
      frame.setVisible(true); 

     } else if(status == JFileChooser.CANCEL_OPTION) { 
      JOptionPane.showMessageDialog(null, "Canceled"); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      TestGraphics tg = new TestGraphics(); 
      tg.newImage(); 
     } 
    }); 
} 

} 

和其他。

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.RenderingHints; 
import java.awt.event.MouseEvent; 
import java.awt.geom.Line2D; 
import java.util.ArrayList; 
import javax.swing.JLabel; 
import java.awt.event.MouseAdapter; 

public class Segmentation extends JLabel { 

private static final long serialVersionUID = -1481861667880271052L; // unique id 
private static final Color LINES_COLOR = Color.red; 
public static final Color CURRENT_LINE_COLOR = new Color(255, 200, 200); 
ArrayList<Line2D> lineList = new ArrayList<Line2D>(); 
Line2D currentLine = null; 
MyMouseAdapter mouse = new MyMouseAdapter(); 

public void addListeners(Component component) { 
    MyMouseAdapter myMouseAdapter = new MyMouseAdapter(); 
    component.addMouseListener(myMouseAdapter); 
    component.addMouseMotionListener(myMouseAdapter); 
} 

@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D) g; 
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

    System.out.println("repainting"); 

    g2.setColor(LINES_COLOR); 
    for (Line2D line : lineList) { 
     g2.draw(line); 
    } 
    if (currentLine != null) { 
     g2.setColor(CURRENT_LINE_COLOR); 
     g2.draw(currentLine); 
    } 
} 

private class MyMouseAdapter extends MouseAdapter { 
    Point p1 = null; 

    @Override 
    public void mousePressed(MouseEvent e) { 
     p1 = e.getPoint(); 
    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
     if (currentLine != null) { 
      currentLine = new Line2D.Double(p1, e.getPoint()); 
      lineList.add(currentLine); 
      currentLine = null; 
      p1 = null; 
      System.out.println("about to repaint"); 
      repaint(); 
     } 
    } 

    @Override 
    public void mouseDragged(MouseEvent e) { 
     if (p1 != null) { 
      currentLine = new Line2D.Double(p1, e.getPoint()); 
      repaint(); 
     } 
    } 
} 

} 

回答

4

您绝不会将Segmentation实例添加到组件层次结构中。所以你的paintComponent永远不会被调用。

你应该有这样的地方:

Segmentation segmentation = new Segmentation(); 
// ... 
component.add(segmentation); // assuming that component is part of a visible component hierarchy 

编辑:

你不注册相应的组件上的鼠标监听器。下面的代码似乎工作得很好:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.RenderingHints; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.geom.Line2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.util.ArrayList; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JLayeredPane; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 

public class TestGraphics { 
    private JLayeredPane contentPane; 

    public void newImage() { 
     try { 
      JFileChooser fileChooser = new JFileChooser("."); 
      int status = fileChooser.showOpenDialog(null); 

      if (status == JFileChooser.APPROVE_OPTION) { 
       File selectedFile = fileChooser.getSelectedFile(); 
       System.out.println("The selected file is from the: " + selectedFile.getParent() + " Drive"); 
       System.out.println("Name of file: " + selectedFile.getName()); 
       System.out.println("Opening file"); 

       BufferedImage buffImage = ImageIO.read(new File(selectedFile.getAbsolutePath())); 
       ImageIcon image = new ImageIcon(buffImage); 

       contentPane = new JLayeredPane(); 
       contentPane.setBackground(Color.WHITE); 
       contentPane.setOpaque(true); 
       // getTabbedPane().setComponentAt(tabNum, contentPane); 
       Dimension d = new Dimension(image.getIconWidth(), image.getIconHeight()); 
       Segmentation segmentation = new Segmentation(); 
       segmentation.setIcon(image); 
       segmentation.setSize(d); 
       contentPane.setPreferredSize(d); 
       contentPane.add(segmentation); 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setContentPane(contentPane); 
       frame.setResizable(false); 
       frame.pack(); 
       frame.setLocationByPlatform(true); 
       frame.setVisible(true); 

      } else if (status == JFileChooser.CANCEL_OPTION) { 
       JOptionPane.showMessageDialog(null, "Canceled"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static class Segmentation extends JLabel { 

     private static final long serialVersionUID = -1481861667880271052L; // unique id 
     private static final Color LINES_COLOR = Color.red; 
     public static final Color CURRENT_LINE_COLOR = new Color(255, 200, 200); 
     ArrayList<Line2D> lineList = new ArrayList<Line2D>(); 
     Line2D currentLine = null; 
     MyMouseAdapter mouse = new MyMouseAdapter(); 

     public Segmentation() { 
      addMouseListener(mouse); 
      addMouseMotionListener(mouse); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2 = (Graphics2D) g; 
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

      System.out.println("repainting"); 

      g2.setColor(LINES_COLOR); 
      for (Line2D line : lineList) { 
       g2.draw(line); 
      } 
      if (currentLine != null) { 
       g2.setColor(CURRENT_LINE_COLOR); 
       g2.draw(currentLine); 
      } 
     } 

     private class MyMouseAdapter extends MouseAdapter { 
      Point p1 = null; 

      @Override 
      public void mousePressed(MouseEvent e) { 
       p1 = e.getPoint(); 
      } 

      @Override 
      public void mouseReleased(MouseEvent e) { 
       if (currentLine != null) { 
        currentLine = new Line2D.Double(p1, e.getPoint()); 
        lineList.add(currentLine); 
        currentLine = null; 
        p1 = null; 
        System.out.println("about to repaint"); 
        repaint(); 
       } 
      } 

      @Override 
      public void mouseDragged(MouseEvent e) { 
       if (p1 != null) { 
        currentLine = new Line2D.Double(p1, e.getPoint()); 
        repaint(); 
       } 
      } 
     } 

    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       TestGraphics tg = new TestGraphics(); 
       tg.newImage(); 
      } 
     }); 
    } 

} 
+0

非常感谢,真的很有帮助,它现在完美调用paintComponent方法!但是它没有显示要涂在图像上的线条。你有什么想法,为什么这可能是? – Mochi 2013-02-10 22:07:30

+1

@Mochi看到我更新的答案。您没有在正确的组件上注册鼠标监听器。 – 2013-02-11 00:11:06

+0

啊你是对的。感谢你的帮助。 – Mochi 2013-02-11 17:42:56

相关问题