2014-03-27 42 views
0

我正在开发用于处理图像的GUI,并且我无法显示图像。更新JPanel并保持JLabels可见

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Polygon; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.geom.Point2D; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import javax.swing.ImageIcon; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class BottomLeftPanel extends JPanel { 

    public static BottomLeftPanel BLP; 
    public static BufferedImage original; 
    public static ImageIcon icon; 
    public static Polygon poly; 
    public static JLabel label; 

    public BottomLeftPanel() throws IOException { 
     super(); 

     this.setBackground(new Color(255, 255, 255, 0)); 

     original = Methods2.loadImage("bowser jr.png"); 
     original = Methods2.toFourChannel(original); 
     poly = null; 
     icon = new ImageIcon(original); 
     label = new JLabel(icon); 
     this.add(new JLabel(icon)); 

     this.addMouseListener(new MouseListener() { 

      @Override 
      public void mouseClicked(MouseEvent me) { 
      } 

      @Override 
      public void mousePressed(MouseEvent me) { 
       Point2D P = me.getPoint(); 
       if(poly == null) { 
        poly = new Polygon(new int[]{(int) P.getX()}, new int[]{(int) P.getY()}, 1); 
        return; 
       } 
       int[] B = poly.xpoints; 
       int[] C = poly.ypoints; 
       int[] X = new int[poly.npoints + 1]; 
       int[] Y = new int[poly.npoints + 1]; 
       System.arraycopy(B, 0, X, 0, B.length); 
       System.arraycopy(C, 0, Y, 0, C.length); 
       X[B.length] = (int) P.getX(); 
       Y[C.length] = (int) P.getY(); 
       poly = new Polygon(X, Y, poly.npoints + 1); 
       System.out.println(poly.toString()); 
       BLP.getGraphics().clearRect(0, 0, BLP.getHeight(), BLP.getWidth()); 
       BLP.repaint(BLP.getGraphics()); 
      } 

      @Override 
      public void mouseReleased(MouseEvent me) { 
      } 

      @Override 
      public void mouseEntered(MouseEvent me) { 
      } 

      @Override 
      public void mouseExited(MouseEvent me) { 
      } 

     }); 

     BLP = this; 
    } 

    public void repaint(Graphics g) { 

     g.setColor(Color.black); 
     g.drawPolygon(poly); 
     icon = new ImageIcon(original); 
     label.setIcon(icon); 
    } 

} 

在mousePressed方法中,更新多边形多边形,并显示更新后的版本。但是,点击几下后,作为加载到屏幕上的JLabel的一部分的ImageIcon将不再可见。我如何解决这个问题,同时保持clearRect方法(我需要clearRect方法来删除已经绘制的多边形并绘制新的多边形)?

+0

永远不要使用'getGraphics()',这不是如何在Swing中完成绘画。请参阅[执行自定义绘画](http://docs.oracle.com/javase/tutorial/uiswing/painting/)了解如何完成自定义绘画。不要依赖'static'来提供跨类边界的变量访问,这只是设计不好的一个标志。如果您需要访问另一个类中的对象,请直接将它的引用传递给该类。 – MadProgrammer

回答

0

我能解决这个问题。我首先将BottomLeftPanel转换为BottomLeftLabel并将其放置在它自己的面板中。然后我在绘画(Graphics g)方法中做了绘画。在paint方法中,我使用了super.paint(Graphics g),但这并不重要,因为JLabel(BottomLeftLabel)不会清除它所持有的ImageIcon,而不管它是否被绘制。我不介意使用静态引用,因为如果我没有使用静态引用,我要么必须使该类实现MouseListener,要么创建一个实现MouseListener的独立类,并且由于我一次只运行1个GUI ,这样做没有意义(静态引用不会导致任何问题)。这里是代码:

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Polygon; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.geom.Point2D; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import javax.swing.ImageIcon; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class BottomLeftLabel extends JLabel { 

    public static BottomLeftLabel BLP; 
    public static BufferedImage original; 
    public static ImageIcon icon; 
    public static Polygon poly; 
// public static JLabel label; 

    public BottomLeftLabel() throws IOException { 
     super(); 

//  this.setBackground(new Color(255, 255, 255, 0)); 

     original = Methods2.loadImage("crash bandicoot picture.jpg"); 
//  original = Methods2.loadImage("bowser jr.png"); 
//  original = Methods2.loadImage("devil's tooth.jpg"); 
     original = Methods2.toFourChannel(original); 
//  int[][] p = Methods.toIntegerArray(original); 
//  p = Methods.adjustTransparency(p, (float) 1.0); 
//  original = Methods.toBufferedImage(p); 
//  this.setSize(new Dimension(original.getWidth(), original.getHeight())); 
//  Graphics g = this.getGraphics(); 
     poly = null; 
     icon = new ImageIcon(original); 
//  label = new JLabel(icon); 

     this.addMouseListener(new MouseListener() { 

      @Override 
      public void mouseClicked(MouseEvent me) { 
      } 

      @Override 
      public void mousePressed(MouseEvent me) { 
       Point2D P = me.getPoint(); 
       if(poly == null) { 
        poly = new Polygon(new int[]{(int) P.getX()}, new int[]{(int) P.getY()}, 1); 
        return; 
       } 
       int[] B = poly.xpoints; 
       int[] C = poly.ypoints; 
       int[] X = new int[poly.npoints + 1]; 
       int[] Y = new int[poly.npoints + 1]; 
       System.arraycopy(B, 0, X, 0, B.length); 
       System.arraycopy(C, 0, Y, 0, C.length); 
       X[B.length] = (int) P.getX(); 
       Y[C.length] = (int) P.getY(); 
       poly = new Polygon(X, Y, poly.npoints + 1); 
       System.out.println(poly.toString()); 
//    BLP.getGraphics().clearRect(0, 0, BLP.getHeight(), BLP.getWidth()); 
//    BLP.removeAll(); 
//    icon = new ImageIcon(original); 
//    BLP.add(new JLabel(icon)); 
       BLP.paint(BLP.getGraphics()); 
      } 

      @Override 
      public void mouseReleased(MouseEvent me) { 
      } 

      @Override 
      public void mouseEntered(MouseEvent me) { 
      } 

      @Override 
      public void mouseExited(MouseEvent me) { 
      } 

     }); 

     this.setIcon(icon); 
     BLP = this; 
//  repaint(this.getGraphics()); 
    } 

    @Override 
    public void paint(Graphics g) { 
     super.paint(g); 
     g.clearRect(0, 0, WIDTH, HEIGHT); 
     if(poly != null) { 
      g.drawPolygon(poly); 
     } 
    } 

// /** 
// * 
// * @param g 
// */ 
// public void repaint(Graphics g) { 
////  g.clearRect(0, 0, WIDTH, HEIGHT); 
// 
//  g.setColor(Color.black); 
//  g.drawPolygon(poly); 
//  this.removeAll(); 
//  icon = new ImageIcon(original); 
//  this.add(new JLabel(icon)); 
// } 

// public void repaint(Graphics g) { 
//  
// } 

}