更新JPanel并保持JLabel可见

时间:2014-03-27 22:09:22

标签: java swing user-interface graphics jlabel

我正在开发一个用于处理图像的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 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方法来删除已经绘制的多边形并绘制新的多边形)?

1 个答案:

答案 0 :(得分:0)

我能够解决问题。我首先将BottomLeftPanel转换为BottomLeftLabel并将其放在自己的面板中。然后我在paint(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) {
//      
//  }

}