Hey, I am trying to loop the code below so that the JFrame fills with the color
ID: 3534836 • Letter: H
Question
Hey,
I am trying to loop the code below so that the JFrame fills with the color currently under the mouse. Currently, it only starts and works for the first color and doesn't change when the mouse is moved. Any ideas on how to loop this? I have a main class too that creates the JFrame but I think this is he part of the code that should be looped.
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class FinalProjectJPanels extends JPanel{
public FinalProjectJPanels() throws AWTException{
// 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();
int rgbValue = (colorRed + colorGreen + colorBlue);
// sets color of application background
JPanel color = new JPanel();
color.setBackground(pixelColor);
color.setPreferredSize(new Dimension(500,400));
JPanel full = new JPanel();
full.setBackground(Color.WHITE);
JPanel rgb = new JPanel();
rgb.setBackground(Color.WHITE);
rgb.setPreferredSize(new Dimension(150,25));
JLabel rgbPrint = new JLabel(" R: " + colorRed + ", G: " + colorGreen + ", B: " + colorBlue);
rgb.add(rgbPrint);
// Adding JPanels together
full.setLayout(new BoxLayout(full, BoxLayout.Y_AXIS));
full.setPreferredSize(new Dimension(500,500));
full.add(rgb);
full.add(color);
add(rgb, BorderLayout.SOUTH);
add(color, BorderLayout.NORTH);
}
}
Explanation / Answer
Please post the main class as well