Results 1 to 4 of 4

Thread: Making a mini ACA JFrame

  1. #1
    Join Date
    Feb 2013
    Location
    Narnia
    Posts
    615
    Mentioned
    8 Post(s)
    Quoted
    252 Post(s)

    Default Making a mini ACA JFrame

    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();
    	}
    
    }

    View my OSR Script Repository!


    Botted to max
    Guides: How to Report Bugs to the Scripter
    ~~~~ Moved to Java. Currently Lurking ~~~~

  2. #2
    Join Date
    Jun 2013
    Location
    Scranton
    Posts
    496
    Mentioned
    5 Post(s)
    Quoted
    220 Post(s)

    Default

    you aren't returning the panel that you are drawing the image to, so you are adding an empty jpanel to your frame, which is why nothing shows

  3. #3
    Join Date
    Feb 2013
    Location
    Narnia
    Posts
    615
    Mentioned
    8 Post(s)
    Quoted
    252 Post(s)

    Default

    Quote Originally Posted by jstemper View Post
    you aren't returning the panel that you are drawing the image to, so you are adding an empty jpanel to your frame, which is why nothing shows
    Right on man, thanks for catching that +1. Can you explain when to use override and super? I thought that's what I was doing wrong.

    View my OSR Script Repository!


    Botted to max
    Guides: How to Report Bugs to the Scripter
    ~~~~ Moved to Java. Currently Lurking ~~~~

  4. #4
    Join Date
    Jun 2013
    Location
    Scranton
    Posts
    496
    Mentioned
    5 Post(s)
    Quoted
    220 Post(s)

    Default

    Quote Originally Posted by Sk1nyNerd View Post
    Right on man, thanks for catching that +1. Can you explain when to use override and super? I thought that's what I was doing wrong.
    super anything will refer to the parent class. specifically for paintComponent, it's a call to the paintComponent method from the JComponent class. here's a good explanation of when to use it https://stackoverflow.com/questions/...tcomponentg-do

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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