将侦听器添加到图像

时间:2014-02-28 16:33:32

标签: java image jpanel listener

Helo亲爱的stackoverflow人:)

我的项目:我必须编写一个电路编辑器。 我的问题:我创建了一个镜像文本,我将其拖到一个Image上,然后将其放入我的程序(Jpanel)中的EditorPanel中。我希望能够使用鼠标移动这个镜像文本,并能够向其添加监听器。

我尝试创建另一个JPanel,将图像放入其中,然后将这个新的JPanel放到EditorPanel中,但我得到nullpointerException,我不知道为什么。请帮忙 谢谢

代码:

@Override 
      public void drawMirroredText(Graphics g3,ScrollPanel scrollpanel){ 


      Image OSC=null;

      try{
       OSC= ImageIO.read(new File("C:/Users/transparent.png"));//put transparent background to my image
      }catch(Exception e ){
          System.out.println("Cant load bufferedimages transparent background");
      }


      Graphics OSG = OSC.getGraphics();



      OSG.setColor(getForeground()); //color for text
      OSG.setFont(getFont());

      FontMetrics fm = OSG.getFontMetrics(getFont());
      int xImage = (100 - fm.stringWidth(this.text)) /2;
      int yImage = (100 + fm.getAscent() - fm.getDescent()) / 2;


        System.out.println("X is "+xImage);
         System.out.println("Y is "+yImage);
      OSG.drawString(text, xImage, yImage);
    OSG.dispose();




     // g3.drawImage(OSC, 100, 0, 0, 100, 0, 0, 100, 100, null);//EditorPanel graphics


    JPanel panel=new JPanel();

    Graphics panelG=panel.getGraphics();

    panel.setPreferredSize(new Dimension(100, 100));


    Graphics2D g2dpanel=(Graphics2D) panelG;

     g2dpanel.drawImage(OSC, 100, 0, 0, 100, 0, 0, 100, 100, panel);//draw image with mirrored text



    scrollpanel.add(panel);//Add new Jpanel to EditorPanel



  }

例外:

  

线程“AWT-EventQueue-0”中的异常    java.lang.NullPointerException at   Panels.ScrollPanel $ Text.drawMirroredText(ScrollPanel.java:2336)at at   Panels.ScrollPanel.drawallnodes(ScrollPanel.java:648)at   Panels.ScrollPanel.paintComponent(ScrollPanel.java:359)at   javax.swing.JComponent.paint(JComponent.java:1054)at   javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)at   javax.swing.RepaintManager中的$ PaintManager.paintDoubleBuffered(RepaintManager.java:1529)     在   javax.swing.RepaintManager中的$ PaintManager.paint(RepaintManager.java:1452)     在javax.swing.RepaintManager.paint(RepaintManager.java:1249)at   javax.swing.JComponent._paintImmediately(JComponent.java:5167)at at   javax.swing.JComponent.paintImmediately(JComponent.java:4978)at   javax.swing.RepaintManager $ 3.run(RepaintManager.java:808)at   javax.swing.RepaintManager $ 3.run(RepaintManager.java:796)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)     在   javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)     在   javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)     在   javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)     在javax.swing.RepaintManager.access $ 1100(RepaintManager.java:62)at   javax.swing.RepaintManager中的$ ProcessingRunnable.run(RepaintManager.java:1677)     at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)     在java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)at   java.awt.EventQueue.access $ 200(EventQueue.java:103)at   java.awt.EventQueue $ 3.run(EventQueue.java:694)at   java.awt.EventQueue $ 3.run(EventQueue.java:692)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)     在java.awt.EventQueue.dispatchEvent(EventQueue.java:703)at   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)     在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)     在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)     at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

0 个答案:

没有答案