Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I am writing a program in JAVA that identifies the color underneath the mous

ID: 3534191 • Letter: H

Question

Hi,


I am writing a program in JAVA that identifies the color underneath the mouse pointer and fills a JFrame with that color, and displays the RGB values of that color. This is what I have so far:


import java.awt.Component;

import java.awt.event.*;

import java.awt.AWTException;

import java.awt.Color;

import java.awt.MouseInfo;

import java.awt.Point;

import java.awt.PointerInfo;

import java.awt.Robot;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;


public class FinalProject {

  

  

  

public static void main(String[] args) throws AWTException {

  

  

// sets up application window

  

JFrame frame = new JFrame("Color Blind Assistant");

frame.setVisible(true);

frame.setSize(500,500);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// determines color underneath mouse

PointerInfo cursorLocation = MouseInfo.getPointerInfo();

Point position = cursorLocation.getLocation();

int x = (int)position.getX();

int y = (int)position.getY();


Robot colorfind = new Robot();   


Color pixelColor = colorfind.getPixelColor(x, y);

int colorRed = pixelColor.getRed();

int colorGreen = pixelColor.getGreen();

int colorBlue = pixelColor.getBlue();

// sets color of application background

frame.getContentPane().setBackground(pixelColor);

// prints RGB value of color under mouse

Explanation / Answer

public class WhatsMyColor { public static void main(String[] args) throws IOException { new WhatsMyColor(); } public WhatsMyColor() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } try { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new MouseColorPane()); frame.setSize(400, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); } catch (Exception exp) { exp.printStackTrace(); } } }); } public class MouseColorPane extends JPanel implements MouseMonitorListener { private Robot robot; private JLabel label; public MouseColorPane() throws AWTException { label = new JLabel(); setLayout(new GridBagLayout()); add(label); robot = new Robot(); PointerInfo pi = MouseInfo.getPointerInfo(); updateColor(pi.getLocation()); MouseMonitor monitor = new MouseMonitor(); monitor.setMouseMonitorListener(this); monitor.start(); } protected void updateColor(Point p) { Color pixelColor = robot.getPixelColor(p.x, p.y); setBackground(pixelColor); label.setText(p.x + "x" + p.y + " = " + pixelColor); } @Override public void mousePositionChanged(final Point p) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { updateColor(p); } }); } } public interface MouseMonitorListener { public void mousePositionChanged(Point p); } public static class MouseMonitor extends Thread { private Point lastPoint; private MouseMonitorListener listener; public MouseMonitor() { setDaemon(true); setPriority(MIN_PRIORITY); } public void setMouseMonitorListener(MouseMonitorListener listener) { this.listener = listener; } public MouseMonitorListener getMouseMonitorListener() { return listener; } protected Point getMouseCursorPoint() { PointerInfo pi = MouseInfo.getPointerInfo(); return pi.getLocation(); } @Override public void run() { lastPoint = getMouseCursorPoint(); while (true) { try { sleep(250); } catch (InterruptedException ex) { } Point currentPoint = getMouseCursorPoint(); if (!currentPoint.equals(lastPoint)) { lastPoint = currentPoint; MouseMonitorListener listener = getMouseMonitorListener(); if (listener != null) { listener.mousePositionChanged((Point) lastPoint.clone()); } } } } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote