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

  • Home
  • SEARCH
  • 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 6241435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:46:05+00:00 2026-05-24T11:46:05+00:00

I have an image of size w by h . In Java, I need

  • 0

I have an image of size w by h. In Java, I need to create an image that is size w by h+20 where the top w by 20 pixels is white, and the rest of the image is the same as the original image.

Essentially I want to know how I can add 20 pixels of white to the top of an existing buffered image.

So it would be something like:

public static void main (String[] args) {
  BufferedImage originalImage = [the original image with a specific file path];
    ...code to create a new image 20 pixels higher...
    ...code to paint originalImage 20 pixels down on the new image
    ...code to save the new image...
}
  • 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-24T11:46:06+00:00Added an answer on May 24, 2026 at 11:46 am

    Suggestion:

    1. Use GraphicsConfiguration.createCompatibleImage(int width, int height) to create a BufferedImage of the same width, but with a height that’s +20.
    2. Use BufferedImage.createGraphics() to obtain the Graphics2D object of this image.
    3. Use Graphics.setColor(Color c) and Graphics.fillRect(int x, int y, int width, int height) to draw the white top
    4. Use Graphics.drawImage(Image img, int x, int y, ImageObserver observer) to draw the original image to the specified coordinates of the new image.
    5. To save the new image, read the Writing/Saving an Image tutorial.

    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics2D;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsEnvironment;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    
    public class ImageManipulationDemo {
        private static BufferedImage ORIGINAL;
        private static BufferedImage ALTERED;
        private static final GraphicsConfiguration config = 
            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    
        public static void main(String[] args) {
            try {
                loadImages();
    
                SwingUtilities.invokeLater(new Runnable(){
                    @Override
                    public void run() {
                        createAndShowGUI();             
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private static void loadImages() throws IOException {
            ORIGINAL = ImageIO.read(
                    ImageManipulationDemo.class.getResource("../resources/whitefro1.jpg"));
            ALTERED = config.createCompatibleImage(
                    ORIGINAL.getWidth(), 
                    ORIGINAL.getHeight() + 20);
            Graphics2D g2 = ALTERED.createGraphics();
            g2.setColor(Color.WHITE);
            g2.fillRect(0, 0, ALTERED.getWidth(), 20);
            g2.drawImage(ORIGINAL, 0, 20, null);
            g2.dispose();
    
            // Save image
            ImageIO.write(ALTERED, "PNG", new File("alteredImage.png"));
        }
    
        private static void createAndShowGUI() {
            final JFrame frame = new JFrame("Image Manipulation Demo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setBackground(Color.BLUE.darker());
            frame.getContentPane().setLayout(new FlowLayout());
            frame.getContentPane().add(new JLabel(new ImageIcon(ORIGINAL)));
            frame.getContentPane().add(new JLabel(new ImageIcon(ALTERED)));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have image and txt file of same size say 200 KB. Now i
I Want to load Image size 2550X3300 (i.e 1.7 Mb size), i have loaded
I have an image of 4x4 in size and I want to use it
I have a string that is HTML encoded: '''<img class="size-medium wp-image-113"\ style="margin-left: 15px;" title="su1"\
I have image data and i want to get a sub image of that
I have: image 1:Many imageToTag Many:1 tag I want to issue a query that
I have some image processing Java code in Android that acts upon two large
Hi I have data of an image of size 640 * 480 pixels, the
I want an circular movement of an image in JAVA, i thought I have
I have an image I'm drawing in java 2d but I want it to

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.