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 8917059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:23:56+00:00 2026-06-15T05:23:56+00:00

My small Java program is trying to darken a png image. It’s working fine

  • 0

My small Java program is trying to darken a png image. It’s working fine on my Mac but when I try to run it on a Windows PC with java 1.7_07 installed but it doesn’t show anything at all except an empty JPanel, the image is completely disappeared.

Here is the code:

class MapCanvas extends JPanel {
    private Color color;
    RescaleOp op;
    BufferedImage sourceImage, bi;

    public MapCanvas() {
        try {
            sourceImage = ImageIO.read(new File(MAP_FILENAME));

            bi = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);

            op = new RescaleOp(.8f, 0, null);
            bi = op.filter(bi, null);

            Graphics2D g = bi.createGraphics();

            g.drawImage(sourceImage, 0, 0, 500, 382, null);
            g.dispose();

        } catch (Exception e) {
            e.printStackTrace();
        }

        // set size for the panel
        Dimension size = new Dimension(500, 382);
        this.setBackground(new Color(34, 102, 187));
        setPreferredSize(size);
        setSize(size);
        setLayout(null);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g2d = (Graphics2D) g;

        g2d.drawImage(bi, op, 0, 0);  
    }
}

Anyone know why I’m getting this? Many thanks.

  • 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-06-15T05:23:57+00:00Added an answer on June 15, 2026 at 5:23 am

    I don’t seem to have any issues, BUT, several things jump out at me about this example…

    Firstly, I don’t see why you’ve done this…

    try {
        sourceImage = ImageIO.read(new File("C:/Users/shane/Dropbox/issue453.jpg"));
    
        bi = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
    
        op = new RescaleOp(.1f, 0, null);
        bi = op.filter(bi, null);
    
        Graphics2D g = bi.createGraphics();
    
        g.drawImage(sourceImage, 0, 0, 500, 382, null);
        g.dispose();
    
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    Then done this…

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
    
        g2d = (Graphics2D) g;
        g2d.drawImage(bi, op, 0, 0);
    }
    

    You’re basically double apply the RescaleOp.

    It could simply just apply the RescaleOp directly to the sourceImage…

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
    
        g2d = (Graphics2D) g;
        g2d.drawImage(sourceImage, op, 0, 0);
    }
    

    Unless you’re concerned about performance, in which case you should simple draw the bi without any BufferedImageOp

    g2d.drawImage(bi, 0, 0, this);
    

    Secondly, your example won’t compile because you’ve not defined g2d in your paintComponent method. This is either an oversight (which is fine) OR you are caching the Graphics object, which is not fine.

    Graphics objects are stateless, they do not persist between repaints, you should NEVER cache them or rely on getGraphics.

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

Sidebar

Related Questions

I'm am currently working on a program in java which requires adding a small
I am building a small Java program for a Mac user who is an
I am trying to write a small java program that will accept a file
I'm trying to write a small java program that connects to a twitter search
I am trying to run my first Java program, an example script that interacts
I need a little help. I'm trying to put together a small Java program
I'm trying to write a really small program in Java that takes a string
I'm beginner in Java, I'm trying to compile some small program, can somebody explain
I'm trying to run eclipse for java in Windows 7 64bit edition, and when
I have a small Java program that reads list of IPs from text file

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.