Hello old friends. I am getting back into the programming scene, however more so in Java. My current project is to try and expand on a little mini ColorAid like tool I found online (see video below) but I am having some JFrame issues. After taking a screen capture of a large portion of the screen I am interested in (not in pixel view, yet), I really have no idea on how to display it onto the JPanel within the JFrame, it's not like a label or button. My panel displays empty. Keep in mind, I will be adding other JPanels within this same JFrame. I found the source code for the video to be sort of helpful, not so much for this part (two lines and no comments), and I used this as a template to edit and prep for the future additions as a lot of the loading image/color stuff I stumbled upon in StackOverflow had similar approaches so maybe this is something silly I did or did not do. Any ideas or help?
Eclipse IDE
Code:
package Capture;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.border.LineBorder;
public class Capture extends JFrame {
public Capture() {
// Create JFrame
JFrame captureFrame = new JFrame("Pixel Capture Tool");
// Set JFrames settings
captureFrame.setDefaultCloseOperation(JFrame.EXIT_ ON_CLOSE);
captureFrame.setAlwaysOnTop(true);
// captureFrame.setResizable(false);
captureFrame.setLocation(10, 10);
// Add panels to JFrame
captureFrame.add(captureAreaPanel(), BorderLayout.CENTER);
// Finish constructing JFrame
captureFrame.pack();
captureFrame.setVisible(true);
}
private JPanel captureAreaPanel() {
// Create JPanel
JPanel panelComponents = new JPanel();
// Create objects for the JPanel
// final Point setCaptureArea = pointCaptureArea((int)MouseInfo.getPointerInfo().g etLocation().getX(),(int)MouseInfo.getPointerInfo( ).getLocation().getY());
final Point setCaptureArea = pointCaptureArea(50, 50);
final Dimension setScreenSize = sizeCaptureArea(500, 500);
panelComponents.setSize(setScreenSize);
final Rectangle captureArea = rectCaptureArea(setCaptureArea.x, setCaptureArea.y, setScreenSize.width, setScreenSize.height);
try {
Robot robot = new Robot();
final BufferedImage image = robot.createScreenCapture(captureArea);
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
//super.paintComponent(g);
// g.drawImage(image, 50, 50, setScreenSize.width, setScreenSize.height, this);
g.drawImage(image, 50, 50, this);
}
};
} catch (Exception e) {
e.printStackTrace();
}
// Set JPanels settings
panelComponents.setLayout(new BorderLayout());
// Add objects to the JPanel
return panelComponents;
}
// Screen location (top left) of Capture Area
public static Point pointCaptureArea(int x, int y) {
Point point = new Point(x, y);
return point;
}
// Screen dimension of Capture Area
public static Dimension sizeCaptureArea(int width, int height) {
Dimension dim = new Dimension(width, height);
return dim;
}
// Screen location and dimension of Capture Area
public static Rectangle rectCaptureArea(int x1, int y1, int width, int height) {
Rectangle rect = new Rectangle(x1, y1, width, height);
return rect;
}
public static void main(String[] args) {
new Capture();
}
}