Question

I am developing a GUI for processing images, and I have trouble with displaying the images.

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. However, after a few clicks, the ImageIcon which was part of the JLabel which was loaded onto the screen is no longer visible. 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)?

Was it helpful?

Solution

I was able to solve the problem. I first converted BottomLeftPanel to BottomLeftLabel and put it in a panel of its own. Then I did the painting in the paint(Graphics g) method. 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. 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). 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) {
//      
//  }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top