5 Replies - 549 Views - Last Post: 06 August 2013 - 09:05 PM Rate Topic: -----

#1 Warsaw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 04-August 13

Getting Image RGB and Alpha

Posted 04 August 2013 - 09:52 PM

Hi,

I am trying to get the RGB and alpha values of an image using the MouseMotionListener...but i keep getting an exception saying:

"Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!"

What does this mean? The getX() and getY() coordinates are working.

class clsDisplay extends JPanel implements MouseMotionListener {
		
		clsDisplay(Rectangle bounds) {
			this.setLayout(new FlowLayout());
			this.setBounds(bounds);
			this.setOpaque(false);
			this.setPreferredSize(new Dimension(bounds.width, bounds.height));
		}
		
		public void mouseMoved(MouseEvent e) {
			intX = e.getX();
			intY = e.getY();
			txtX.setText(Integer.toString(intX));
			txtY.setText(Integer.toString(intY));
			
			if(imgShow != null) {
				intRGB = imgShow.getRGB(intX,  intY);
				intA = (intRGB & 0xff000000) >> 32;
				intR = (intRGB & 0x00ff0000) >> 16;
				intG = (intRGB & 0x0000ff00) >> 8;
				intB = intRGB & 0x000000ff;
				txtA.setText(Integer.toString(intA));
				txtR.setText(Integer.toString(intR));
				txtG.setText(Integer.toString(intG));
				txtB.setText(Integer.toString(intB));
			}
		}
		
		public void mouseDragged(MouseEvent e) {
			// Empty
		}
		
		public void paintComponent(Graphics g) {
			Graphics2D g2d = (Graphics2D)g;
			g2d.drawImage(imgShow, 0, 0, null);
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Getting Image RGB and Alpha

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Getting Image RGB and Alpha

Posted 04 August 2013 - 10:27 PM

I don't know what in the world you're doing with that code, but getting what you want is quite easy: Your mouse is going out of bounds somewhere, it sounds like. I could replicate that in this code by setting setResizable(true), maximizing the window, and moving my mouse out of the colored image.

public class JavaApplication2 extends JFrame {
    
    public JavaApplication2() {
        setTitle("image rgb");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(new JPanel() {
            BufferedImage image;
            MouseMotionListener mml;
            Color color;
            {
                image = new BufferedImage(300,300, BufferedImage.TYPE_INT_ARGB);
                mml = new MouseMotionListener() {
                    @Override
                    public void mouseDragged(MouseEvent e) {
                    }
                    @Override
                    public void mouseMoved(MouseEvent e) {
                        color = new Color(image.getRGB(e.getX(), e.getY()));
                        int a = color.getAlpha();
                        int r = color.getRed();
                        int g = color.getGreen();
                        int b = color.getBlue();
                        changeImageColor();
                        repaint();
                        System.out.println(a + "a " + r + "r " + g + "g " + b + "b");
                    }
                    
                };
                setPreferredSize(new Dimension(300,300));
                setFocusable(true);
                addMouseMotionListener(mml);
            }
            
            void changeImageColor() {
                Graphics2D g = image.createGraphics();
                g.setColor(new Color(new Random().nextInt(256),new Random().nextInt(256),new Random().nextInt(256)));
                g.fillRect(0, 0, image.getWidth(), image.getHeight());
            }
            
            public void paintComponent(Graphics g) {
                g.drawImage(image, 0, 0, null);
            }
        });
        setResizable(false);
        pack();
        setVisible(true);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JavaApplication2();
            }
        });
    }
}


This post has been edited by farrell2k: 04 August 2013 - 10:31 PM

Was This Post Helpful? 1
  • +
  • -

#3 Warsaw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 04-August 13

Re: Getting Image RGB and Alpha

Posted 04 August 2013 - 11:51 PM

The code is a subclass to the picture viewer program i am working on for uni. didnt know it could be done like that, was doing bitwise computations. :(

Anyway, i got it working now. Used try-catch for the out of bounds exception.

Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#4 Warsaw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 04-August 13

Re: Getting Image RGB and Alpha

Posted 05 August 2013 - 02:14 AM

ok, after more testing, i am still getting the out of bounds exception near the edge of the frame...even with the try-catch statement. :(

Another question is how do you center an image in a JPanel, ive seen examples of people using JLabels and ImageIcons but is that also possible for BufferedImages?

Here is the full code...
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Assign1 extends JFrame implements ActionListener {
	
	private static final int frmHeight = 512;
	private static final int frmWidth = 512;
	
	private static JMenuItem itmOpen, itmQuit;
	private static JMenuItem itmAbout;
	
	private static JButton btnO, btnFlipX, btnFlipY, btnFlipXY, btnNeg;
	
	protected JTextArea txtX, txtY, txtR, txtG, txtB, txtA;
	
	private int intX = -1, intY = -1, intR = -1, intG = -1, intB = -1, intA = -1;
	
	private BufferedImage imgShow = null;
	private Color clrRGB = null;
	
	public Assign1() {
		// JFrame properties
		super("Picture Viewer");
		this.setSize(frmHeight, frmWidth);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(true);
		this.setLocationRelativeTo(null);
		
		// Menu
		JMenuBar menuBar = new JMenuBar();
		JMenu menu;
		
		menu = new JMenu("File");
		menuBar.add(menu);
		itmOpen = new JMenuItem("Open");
		itmOpen.addActionListener(this);
		menu.add(itmOpen);
		itmQuit = new JMenuItem("Quit");
		itmQuit.addActionListener(this);
		menu.add(itmQuit);
		
		menu = new JMenu("Help");
		menuBar.add(menu);
		itmAbout = new JMenuItem("About");
		itmAbout.addActionListener(this);
		menu.add(itmAbout);
		
		Container con = getContentPane();
		con.setLayout(new BoxLayout(con, BoxLayout.Y_AXIS));
		
		// Contents panel contains the buttons
		clsContent objContent = new clsContent(new Rectangle(0, 0, frmWidth, frmHeight / 10));
		
		// Info panel contains the text areas
		clsInfo objInfo = new clsInfo(new Rectangle(0, 0, frmWidth, frmHeight / 10));
		
		// Display panel is where the image will be loaded
		clsDisplay objDisplay = new clsDisplay(new Rectangle(0, 0, frmWidth, frmHeight));
		
		this.setJMenuBar(menuBar);
		con.add(objContent);
		con.add(objInfo);
		con.add(objDisplay);
		this.addMouseMotionListener(objDisplay);
		pack();
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		JComponent src = (JComponent)e.getSource();
		
		if(src == itmOpen) {
			fileOpen();
		} else if(src == itmQuit) {
			System.out.println("Exiting...");
			System.exit(0);
		} else if(src == itmAbout) {
			Assign1.msgBox("159.235 Assignment 1: Simple Interactive Image Viewing Tool", "About");
		} else if(src == btnO) {
			
		} else if(src == btnFlipX) {
			
		} else if(src == btnFlipY) {
			
		} else if(src == btnFlipXY) {
			
		} else if(src == btnNeg) {
			
		}
	}
	
	public static void main(String args[]) {
		Assign1 frmMain = new Assign1();
		frmMain.repaint();
	}
	
	public static void msgBox(String strMessage, String strTitle) {
		JOptionPane.showMessageDialog(null, strMessage, strTitle, JOptionPane.DEFAULT_OPTION);
	}
	
	public void fileOpen() {
		try {
			JFileChooser selectFile = new JFileChooser();
			int intOpt = selectFile.showOpenDialog(this);
			if(intOpt == JFileChooser.APPROVE_OPTION) {
				File imgFile = selectFile.getSelectedFile();
				imgShow = ImageIO.read(imgFile);
				System.out.println("Opening " + imgFile.getName());
			}
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	//============================================================================================//
	class clsContent extends JPanel {
		
		clsContent(Rectangle bounds) {
			this.setLayout(new FlowLayout());
			this.setBounds(bounds);
			this.setOpaque(false);
			this.setPreferredSize(new Dimension(bounds.width, bounds.height));
			
			// Buttons
			btnO = new JButton("O");
			btnFlipX = new JButton("Flip X");
			btnFlipY = new JButton("Flip Y");
			btnFlipXY = new JButton("Flip XY");
			btnNeg = new JButton("Negate");
			this.add(btnO);
			this.add(btnFlipX);
			this.add(btnFlipY);
			this.add(btnFlipXY);
			this.add(btnNeg);
		}
	}
	
	//============================================================================================//
	class clsInfo extends JPanel {
		
		clsInfo(Rectangle bounds) {
			this.setLayout(new FlowLayout());
			this.setBounds(bounds);
			this.setOpaque(false);
			this.setPreferredSize(new Dimension(bounds.width, bounds.height));
			
			// TextArea showing X and Y coordinates including RGB values and the Alpha
			txtX = new JTextArea("0", 1, 5);
			txtX.setEditable(false);
			this.add(new JLabel("X"));
			this.add(txtX);
			
			txtY = new JTextArea("0", 1, 5);
			txtX.setEditable(false);
			this.add(new JLabel("Y"));
			this.add(txtY);
			
			txtR = new JTextArea("0", 1, 5);
			txtX.setEditable(false);
			this.add(new JLabel("R"));
			this.add(txtR);
			
			txtG = new JTextArea("0", 1, 5);
			txtX.setEditable(false);
			this.add(new JLabel("G"));
			this.add(txtG);
			
			txtB = new JTextArea("0", 1, 5);
			txtX.setEditable(false);
			this.add(new JLabel("B"));
			this.add(txtB);
			
			txtA = new JTextArea("0", 1, 5);
			txtX.setEditable(false);
			this.add(new JLabel("A"));
			this.add(txtA);
		}
	}
	
	//============================================================================================//
	class clsDisplay extends JPanel implements MouseMotionListener {
		
		clsDisplay(Rectangle bounds) {
			this.setLayout(new FlowLayout());
			this.setBounds(bounds);
			this.setOpaque(false);
			this.setPreferredSize(new Dimension(bounds.width, bounds.height));
		}
		
		public void mouseMoved(MouseEvent e) {
			try {
				intX = e.getX();
				intY = e.getY();
				txtX.setText(Integer.toString(intX));
				txtY.setText(Integer.toString(intY));
				
				if(imgShow != null) {
					clrRGB = new Color(imgShow.getRGB(intX,  intY));
					intA = clrRGB.getAlpha();
					intR = clrRGB.getRed();
					intG = clrRGB.getGreen();
					intB = clrRGB.getBlue();
					txtA.setText(Integer.toString(intA));
					txtR.setText(Integer.toString(intR));
					txtG.setText(Integer.toString(intG));
					txtB.setText(Integer.toString(intB));
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		
		public void mouseDragged(MouseEvent e) {
			// Empty
		}
		
		public void paintComponent(Graphics g) {
			Graphics2D g2d = (Graphics2D)g;
			g2d.drawImage(imgShow, 0, 0, null);
		}
	}
}



finally, how do you edit your post?
Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Getting Image RGB and Alpha

Posted 05 August 2013 - 07:06 AM

It sounds like your mouse is just going out of the coordinates of the image. A simple if statement in the mousemotionlistener could prevent that.

Rectangle imageBounds = new Rectangle(image.x, image.y, image.width, image.height);
if(imageBounds.contains(e.getX(), e.getY()) {
  //print the rgb info.
}


Was This Post Helpful? 1
  • +
  • -

#6 Warsaw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 04-August 13

Re: Getting Image RGB and Alpha

Posted 06 August 2013 - 09:05 PM

ok, i got the rgb to work now thanks! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1