Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7684051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:56:13+00:00 2026-05-31T18:56:13+00:00

I’m trying to capture the screen and then paint the image to a JFrame

  • 0

I’m trying to capture the screen and then paint the image to a JFrame recursively while scaling the image (to create that effect you get when you look at a mirror in a mirror).

I’m having trouble with my code – it doesn’t paint any graphics. What am I doing wrong?

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;


public class ScreenCapture extends JFrame {

    BufferedImage screenCapture;
    Graphics screenCaptureGraphics;
    private static int recurseCount = 0;
    private static float $scale = 0.9f;
    private static float scale = 1.0f;
    private static int height;
    private static int width;

    ScreenCapture() {
        try {
            screenCapture = new Robot().createScreenCapture(
                       new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
            height = screenCapture.getHeight();
            width = screenCapture.getWidth();
            setSize(new Dimension(width, height));
            addWindowListener(new LocalWindowListener());
            Graphics g = recursiveDraw(screenCapture, getGraphics());
            paint(g);
        } catch (HeadlessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private Graphics recursiveDraw(BufferedImage img, Graphics imgG) {
        updateScale(++recurseCount);
        float newWidth = scale*width;
        float newHeight = scale*height;
        int w = (int) newWidth;
        int h = (int) newHeight;
        System.out.println("W: " + w + "; H: " + h);
        if (w >= 10 && h >= 10) {
            //scale image
            System.out.print("Creating scaled Image...");
            Image scaled = img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
            BufferedImage resized = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            imgG = resized.createGraphics();
            imgG.drawImage(scaled, 0, 0, null);
            System.out.println("...Image drawn to graphics");
            //return new graphics
            return recursiveDraw(resized, imgG);
        } else {
            //otherwise return old graphics
            System.out.println("Completed.");
            return imgG;
        }
    }


    private void updateScale(int count) {
        for (int i=0; i<count; i++) {
            scale *= $scale;
        }
        System.out.println("Updated scale: " + scale + "; Recurse count: " + recurseCount);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ScreenCapture().setVisible(true);
            }
        });
    }

    private class LocalWindowListener extends WindowAdapter {
        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0); return;
        }
    }

}

EDIT:
This is what I tried after @andrew-thompson ‘s answer:

ScreenCapture() {
    try {
        screenCapture = new Robot().createScreenCapture(
                   new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
        height = screenCapture.getHeight();
        width = screenCapture.getWidth();
        setSize(new Dimension(width, height));
        addWindowListener(new LocalWindowListener());
        setLayout(new GridLayout());
        add(new PaintPanel());
    } catch (HeadlessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private class PaintPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        g=recursiveDraw(screenCapture, g);
        //what to do with g?
    }
}

I still have the same problem where I don’t know how to make the BufferedImage paint to the graphics.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:56:14+00:00Added an answer on May 31, 2026 at 6:56 pm

    would separate out your Swing code from your recursive image creation code. In fact consider creating a static method that creates and returns the BufferedImage and that has no Swing code in it. Then have your GUI call the method when it wishes, and take the image and either write it to disk or display it in a JLabel’s ImageIcon.

    When I did this (today in fact), I created a recursive method with this signature

    private static void recursiveDraw(BufferedImage img, Graphics imgG, double scale) { 
    

    and with this method body (in pseudo-code)

    start recursiveDraw method 
       // most important: all recursions must have a good ending condition: 
       get img height and width. If either <= a min, return 
       create a BufferedImage, smlImg, for the smaller image using the height, 
            width and scale factor 
       Get the Graphics object, smlG, from the small image 
       Use smlG.drawImage(...) overload to draw the big image in shrunken 
            form onto the little image 
       recursively call recursiveDraw passing in smlImg, smlG, and scale. 
       dispose of smlG 
       draw smlImg (the small image) onto the bigger one using the bigger's 
            Graphics object (passed into this method) and a different 
            overload of the drawImage method. 
    end recursiveDraw method
    

    This algorithm resulted in images like: enter image description here

    For example:

    import java.awt.*;
    import java.awt.Dialog.ModalityType;
    import java.awt.event.ActionEvent;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class RecursiveDrawTest {
       private static final Color BACKGRND_1 = Color.green;
       private static final Color BACKGRND_2 = Color.MAGENTA;
       private static final Color FOREGRND_1 = Color.blue;
       private static final Color FOREGRND_2 = Color.RED;
    
       private static void createAndShowGui() {
          final JPanel mainPanel = new JPanel(new BorderLayout());
          final JSlider slider = new JSlider(50, 90, 65);
          slider.setMajorTickSpacing(10);
          slider.setMinorTickSpacing(5);
          slider.setPaintLabels(true);
          slider.setPaintTicks(true);
    
          JPanel southPanel = new JPanel();
          southPanel.add(new JLabel("Percent Size Reduction:"));
          southPanel.add(slider);
          southPanel.add(new JButton(new AbstractAction("Create Recursive Image") {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                try {
                   double scale = slider.getValue() / 100.0;
                   BufferedImage img = createRecursiveImg(scale);
                   ImageIcon icon = new ImageIcon(img);
                   JLabel label = new JLabel(icon);
    
                   Window win = SwingUtilities.getWindowAncestor(mainPanel);
                   JDialog dialog = new JDialog(win, "Image", ModalityType.MODELESS);
                   dialog.getContentPane().add(label);
                   dialog.pack();
                   dialog.setLocationRelativeTo(null);
                   dialog.setVisible(true);
                } catch (AWTException e) {
                   e.printStackTrace();
                }
             }
          }));
    
          mainPanel.add(new JScrollPane(new JLabel(new ImageIcon(createLabelImg()))), 
                BorderLayout.CENTER);
          mainPanel.add(southPanel, BorderLayout.PAGE_END);
    
          JFrame frame = new JFrame("RecursiveDrawTest");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       // create a background image to display in a JLabel so that the GUI
       // won't be boring.
       private static BufferedImage createLabelImg() {
          Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
          int width = (5 * d.width) / 6;
          int height = (5 * d.height) / 6;
          BufferedImage img = new BufferedImage(width, height, 
                BufferedImage.TYPE_INT_ARGB);
          Graphics2D g2 = img.createGraphics();
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
          g2.setPaint(new GradientPaint(0, 0, BACKGRND_1, 40, 40, BACKGRND_2, true));
          g2.fillRect(0, 0, width, height);
          g2.setPaint(new GradientPaint(0, height, FOREGRND_1, 40, height - 40, FOREGRND_2, true));
          g2.fillOval(0, 0, 2 * width, 2 * height);
          g2.dispose();
          return img;
       }
    
       // non-recursive image to get the initial image that will be drawn recursively
       public static BufferedImage createRecursiveImg(double scale) throws AWTException {
          Robot robot = new Robot();
          Dimension screenSz = Toolkit.getDefaultToolkit().getScreenSize();
          Rectangle screenRect = new Rectangle(screenSz);
          BufferedImage img = robot.createScreenCapture(screenRect);
          Graphics g = img.getGraphics();
          recursiveDraw(img, g, scale); // call recursive method
          g.dispose();
          return img;
       }
    
       // recursive method to draw image inside of image
       private static void recursiveDraw(BufferedImage img, Graphics g, double scale) {
          int w = img.getWidth();
          int h = img.getHeight();
    
          int smlW = (int)(w * scale);
          int smlH = (int)(h * scale);
          // criteria to end recursion
          if (smlW <= 1 || smlH <= 1) {
             return; 
          }
    
          BufferedImage smlImg = new BufferedImage(smlW, smlH, BufferedImage.TYPE_INT_ARGB);
          Graphics smlG = smlImg.getGraphics();
          // draw big image in little image, scaled to little image
          smlG.drawImage(img, 0, 0, smlW, smlH, null);
    
          // recursive call
          recursiveDraw(smlImg, smlG, scale);
          smlG.dispose(); // clear resources when done with them
    
          // these guys center the smaller img on the bigger
          int smlX = (w - smlW) / 2;
          int smlY = (h - smlH) / 2;
          // draw small img on big img
          g.drawImage(smlImg, smlX, smlY, smlW, smlH, null);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.