Results 1 to 8 of 8
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Intermitently getting Color for getX, getY

    I have a little application I am making for myself, and ran into a snag. I recently added the screenPanel MouseListener and was trying to use it to get the color of a pixel from a BufferedImage. I am able to get the image (screenShot) that I want and load it where I want. And it appears that when you click on the image, you are *sometimes* getting the controlPanel to update its background color. I am confused as to why it seems to only work sometimes. Is there anyone that can see something wrong who could point me in the right direction.

    Java Code:
    package com.cb.ryan;
    
    import java.awt.AWTException;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Program extends Container{
    	
    	private BufferedImage screenShot;
    	private Image i;
    	private ImageIcon screenCap;
    	private JPanel screenPanel, controlPanel;
    	private JLabel screenLabel;
    	private int factor = 4, posX, posY;
    	
    	public Program(){
    		capture();
    	}
    	
    	public void capture(){
    		try {
    			screenShot = captureScreen("screen Shot");
    		} catch (AWTException | IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		screenCap = new ImageIcon();
    		screenCap.setImage(i);
    		
    		screenPanel = new JPanel();
    		screenPanel.setSize(450, 350);
    		screenPanel.setLocation(0, 0);
    		screenPanel.setBackground(Color.BLACK);
    		screenPanel.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		c.gridx = 0;
    		c.gridy = 0;
    		screenLabel = new JLabel(screenCap);
    		screenPanel.add(screenLabel,c);
    		screenPanel.addMouseListener(new MouseListener(){
    
    			@Override
    			public void mouseClicked(MouseEvent e) {
    				
    				posX = e.getX();
    				posY = e.getY();
    				controlPanel.setBackground(new Color(screenShot.getRGB(posX,posY)));
    				
    			}
    
    			@Override
    			public void mouseEntered(MouseEvent e) {
    				
    				
    			}
    
    			@Override
    			public void mouseExited(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			public void mousePressed(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			public void mouseReleased(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    		});
    		add(screenPanel);
    		
    		controlPanel = new JPanel();
    		controlPanel.setSize(300,350);
    		controlPanel.setLocation(450, 0);
    		controlPanel.setBackground(Color.DARK_GRAY);
    		controlPanel.setLayout(new GridBagLayout());
    		GridBagConstraints c2 = new GridBagConstraints();
    		c2.gridx = 0;
    		c2.gridy = 0;
    		add(controlPanel);
    	}	
    	
    	public BufferedImage captureScreen(String fileName) throws AWTException, IOException{
    		   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    		   Rectangle screenRectangle = new Rectangle(screenSize);
    		   Robot robot = new Robot();
    		   BufferedImage image = robot.createScreenCapture(screenRectangle);
    		   i = image.getScaledInstance(image.getWidth()/factor, image.getHeight()/factor, Image.SCALE_SMOOTH);
    		   return image;
    		}
    }
    EDIT: I am getting no error codes of any kind, I simply am not getting the performance as expected.
    Last edited by rru96; 09-19-2013 at 04:48 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,016
    Rep Power
    6

    Default Re: Intermitently getting Color for getX, getY

    Please provide an SSCCE to illustrate the problem. The above program does not contain a main entry point.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Intermitently getting Color for getX, getY

    Java Code:
    package com.ct.ryan;
    
    import java.awt.AWTException;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class SSCCE extends Container{
    	
    	private BufferedImage screenShot;
    	private Image i;
    	private ImageIcon screenCap;
    	private JPanel screenPanel, controlPanel;
    	private JLabel screenLabel;
    	private int factor = 1, posX, posY;
    		
    	public SSCCE(){
    		capture();
    	}
    	
    	public void capture(){
    		try {
    			screenShot = captureScreen("screen Shot");
    		} catch (AWTException | IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		screenCap = new ImageIcon();
    		screenCap.setImage(i);
    		
    		screenPanel = new JPanel();
    		screenPanel.setSize(450, 350);
    		screenPanel.setLocation(0, 0);
    		screenPanel.setBackground(Color.BLACK);
    		screenPanel.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		c.gridx = 0;
    		c.gridy = 0;
    		screenLabel = new JLabel(screenCap);
    		screenPanel.add(screenLabel,c);
    		screenPanel.addMouseListener(new MouseListener(){
    
    			@Override
    			public void mouseClicked(MouseEvent e) {
    				
    				posX = e.getX();
    				posY = e.getY();
    				controlPanel.setBackground(new Color(screenShot.getRGB(posX,posY)));
    				
    			}
    
    			@Override
    			public void mouseEntered(MouseEvent e) {
    				
    				
    			}
    
    			@Override
    			public void mouseExited(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			public void mousePressed(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			public void mouseReleased(MouseEvent arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    		});
    		add(screenPanel);
    		
    		controlPanel = new JPanel();
    		controlPanel.setSize(300,350);
    		controlPanel.setLocation(450, 0);
    		controlPanel.setBackground(Color.DARK_GRAY);
    		controlPanel.setLayout(new GridBagLayout());
    		GridBagConstraints c2 = new GridBagConstraints();
    		c2.gridx = 0;
    		c2.gridy = 0;
    		add(controlPanel);
    	}	
    	
    	public BufferedImage captureScreen(String fileName) throws AWTException, IOException{
    		   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    		   Rectangle screenRectangle = new Rectangle(screenSize);
    		   Robot robot = new Robot();
    		   BufferedImage image = robot.createScreenCapture(screenRectangle);
    		   i = image.getScaledInstance(image.getWidth()/factor, image.getHeight()/factor, Image.SCALE_SMOOTH);
    		   return image;
    		}
    	
    	public static void main(String[] args){
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setPreferredSize(new Dimension(600,300));
    		frame.setMinimumSize(new Dimension(600,300));
    		
    		SSCCE x = new SSCCE();
    		frame.add(x);
    		frame.pack();
    		
    		frame.setVisible(true);
    		
    	}
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Intermitently getting Color for getX, getY

    Time for you to go through the tutorial for GridBagLayout, and the use of layouts in general. You have two GridBagConstraints that aren't used, and a GridBagLayout set to a container to which no components are added. While you're there. also go through the section on Concurrency in Swing and learn about invoking Swing constructors and methods on the EDT.

    Also, learn about MouseAdapter. It could make your code about 20 lines shorter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Intermitently getting Color for getX, getY

    Oh, and your problem may stem from using two images, one of which is a scaled instance of the other, and one of which is displayed and the other queried for the RGB value.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Intermitently getting Color for getX, getY

    Concurrency was set up on a separate class, that I had my main.. I guess I tried to make an SSCCE and thought I was trying to get rid of everything I could but what was needed to replicate the problem.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,016
    Rep Power
    6

    Default Re: Intermitently getting Color for getX, getY

    When you display your image, look at the top left corner. Does that look like the top left corner of your screen? I suggest you replace your screenRectangle with something like new Rectangle(0,0,450,350) to see it work properly. You need to ensure your displayed image and your snapshot coincide.

    Also, you can use MouseAdapter instead of MouseListener. Then you don't have to provide the dummy methods.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Intermitently getting Color for getX, getY

    Thank you guys. Was actually in the process of reading and restructuring my code to use a MouseAdapter.

Similar Threads

  1. Confisuion about sprite.getX() and sprite.getY()
    By Basit781 in forum Java Gaming
    Replies: 0
    Last Post: 01-10-2011, 07:16 AM
  2. getx and gety mouseClick stuff
    By Chozo Knight in forum New To Java
    Replies: 5
    Last Post: 11-22-2010, 04:48 AM
  3. [COLOR="Navy"]execute .bat file in mysql [/COLOR]
    By msankar.ravi in forum Networking
    Replies: 0
    Last Post: 02-24-2010, 05:27 AM
  4. getX() and getY() return same value
    By chielt in forum Java Applets
    Replies: 14
    Last Post: 12-21-2009, 08:09 AM
  5. A bit of color!
    By tim in forum Java 2D
    Replies: 8
    Last Post: 02-12-2008, 12:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •