Im trying to set the horizontal half of the JFrame to display the color that the cursor is on. This is what I got so far.
enter code here
package finalproject;
import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.PointerInfo;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.event.MouseMotionListener;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.*;
public class FinalProject {
public static void main(String[] args) throws AWTException {
JFrame frame = new JFrame();
JLabel rgbLabel = new JLabel();
rgbLabel.setVerticalAlignment(SwingConstants.NORTH);
rgbLabel.setHorizontalAlignment(SwingConstants.CENTER);
frame.setLocationRelativeTo(null);
frame.add(rgbLabel);
rgbLabel.setAlignmentX(50);
rgbLabel.setAlignmentY(10);
rgbLabel.setVisible(true);
frame.setLocation(650, 350);
frame.setVisible(true);
frame.pack();
frame.setTitle(display.TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(display.WIDTH, display.HEIGHT);
frame.setResizable(true);
frame.setVisible(true);
while (true){
PointerInfo cursorLocation = MouseInfo.getPointerInfo();
Point position = cursorLocation.getLocation();
int x = (int)position.getX();
int y = (int)position.getY();
Robot robot = new Robot();
Color pixelColor = robot.getPixelColor(x, y);
int colorRed = pixelColor.getRed();
int colorGreen = pixelColor.getGreen();
int colorBlue = pixelColor.getBlue();
rgbLabel.setText("Red: " + colorRed + " Green: " + colorGreen + " Blue: " + colorBlue + "\n" );
}
}
public static class display {
public static final int WIDTH = 250;
public static final int HEIGHT = 135;
public static final String TITLE = "Colorblind Assistant";
}
}
I know that putting
frame.getContantPane().setBackground(pixelColor);
under the while loop will change the whole frame to the color, but I only need the bottom half of the frame to be that color.
Some help would be appreciated. Thank you.
while (true)
that will tie up the Swing event thread.