2014-03-27 48 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) { 
//  
// } 

}