I have a class which displays the pixel information of an image. Right now the class takes a buffered image in its constructor but I would like the class to allow an image to be passed from another class. How can I do this because right now if the class is run without an initial image, there will be an error. I hope someone can help.
Thank you

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package finalproject;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class PicturePanel extends JPanel implements MouseListener, MouseMotionListener {

    public PicturePanel(BufferedImage image) {
        this.setBorder(BorderFactory.createLoweredBevelBorder());
        this.setBorder(BorderFactory.createLineBorder(Color.BLUE, 5));
        this.image = image;
                add(this.getLabels());
        addMouseMotionListener(mml);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        setPreferredSize(new Dimension(400, 250));
        setVisible(true);

    }
    
    public void mouseClicked(MouseEvent e) {
    }


    public void mouseDragged(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }
    BufferedImage image;
    JLabel[] labels;

    private void showPixel(Point p) {
        int w = getWidth();
        int h = getHeight();
        int iw = image.getWidth();
        int ih = image.getHeight();
        Rectangle rect = new Rectangle(iw, ih);
        rect.x = (w - iw) / 2;
        rect.y = (h - ih) / 2;
        int x = 0,  y = 0, r = 0,  g = 0,  b = 0;
        if (rect.contains(p)) {
            x = p.x - rect.x;
            y = p.y - rect.y;
            int pixel = image.getRGB(x, y);
            r = (pixel >> 16) & 0xff;
            g = (pixel >> 8) & 0xff;
            b = (pixel >> 0) & 0xff;
        }
        setLabels(x, y, r, g, b);
    }

    private void setLabels(int x, int y, int r, int g, int b) {
        labels[0].setText(String.valueOf(x));
        labels[1].setText(String.valueOf(y));
        labels[2].setText(String.valueOf(r));
        labels[3].setText(String.valueOf(g));
        labels[4].setText(String.valueOf(b));
    }

    private JPanel getLabels() {
        labels = new JLabel[5];
        for (int i = 0; i < labels.length; i++) {
            labels[i] = new JLabel();
        }
        Dimension d = new Dimension(35, 25);
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 2, 2);
        addComponents("x =", labels[0], panel, gbc, d, 1.0, 0);
        addComponents("y =", labels[1], panel, gbc, d, 0, 1.0);
        addComponents("r =", labels[2], panel, gbc, d, 0, 0);
        addComponents("g =", labels[3], panel, gbc, d, 0, 0);
        addComponents("b =", labels[4], panel, gbc, d, 0, 1.0);
        return panel;
    }

    private void addComponents(String s, JComponent jc, Container c,
            GridBagConstraints gbc, Dimension d,
            double weightx1, double weightx2) {
        gbc.weightx = weightx1;
        gbc.anchor = GridBagConstraints.EAST;
        c.add(new JLabel(s), gbc);
        jc.setPreferredSize(d);
        gbc.weightx = weightx2;
        gbc.anchor = GridBagConstraints.WEST;
        c.add(jc, gbc);
    }
    
    private MouseMotionListener mml = new MouseMotionAdapter() {

        public void mouseMoved(MouseEvent e) {
            Point p = e.getPoint();
            showPixel(p);
        }
    };
}

Recommended Answers

All 4 Replies

How can I do this because right now if the class is run without an initial image, there will be an error.

Well, then make sure your code is not dependent on that image reference being set. A few null checks or whatever you need. It's your code, so "there will be an error" only comes into play if you let it.

After that is squared away, create a method setImage(Image) that let's you set the image on the panel.

the code has to be altered a bit, then u ll get output, there r some related code snippets are there in net, browse them.......

Thanks for the advice vincent.

At a quick look its only when mouseMoved calls showPixel that you have a problem with a null image. Since you can't do anything without an image I'd suggest modifying mouseMoved so that if image is null it just returns without doing anything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.