简体   繁体   English

更新JPanels并保持JLabel可见

[英]Updating JPanels and keeping JLabels Visible

I am developing a GUI for processing images, and I have trouble with displaying the images. 我正在开发用于处理图像的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);
    }

}

In the method mousePressed, polygon poly is updated, and the updated version is shown. 在方法mousePressed中,将更新面多边形,并显示更新的版本。 However, after a few clicks, the ImageIcon which was part of the JLabel which was loaded onto the screen is no longer visible. 但是,单击几下后,将不再显示已加载到屏幕上的JLabel的ImageIcon。 How do I fix this while keeping the clearRect method in place (I need the clearRect method in order to remove the already drawn polygon and draw the new polygon)? 如何在保持clearRect方法不变的同时解决此问题(我需要使用clearRect方法才能删除已经绘制的多边形并绘制新的多边形)?

I was able to solve the problem. 我能够解决问题。 I first converted BottomLeftPanel to BottomLeftLabel and put it in a panel of its own. 我首先将BottomLeftPanel转换为BottomLeftLabel,并将其放在自己的面板中。 Then I did the painting in the paint(Graphics g) method. 然后我用paint(Graphics g)方法进行绘画。 In the paint method, I used super.paint(Graphics g), but this is not important because the JLabel (BottomLeftLabel) would not clear the ImageIcon it held no matter what was painted. 在绘制方法中,我使用了super.paint(Graphics g),但这并不重要,因为无论绘制什么,JLabel(BottomLeftLabel)都不会清除它所容纳的ImageIcon。 I do not mind using a static reference, since if I did not use a static reference, I would either have to make the class implement MouseListener, or create a seperate class which implements MouseListener, and since I am only running 1 GUI at a time, it does not make sense to do that (the static reference would not cause any problems here). 我不介意使用静态引用,因为如果我不使用静态引用,则要么必须使该类实现MouseListener,要么创建一个单独的类来实现MouseListener,并且由于我一次只运行1个GUI ,这样做是没有意义的(静态引用不会在此处引起任何问题)。 Here is the code: 这是代码:

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) {
//      
//  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM