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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:02:03+00:00 2026-06-16T23:02:03+00:00

Hello Java Developers, I’ve never encountered this scenario so far. This scenario is: (To

  • 0

Hello Java Developers,

I’ve never encountered this scenario so far. This scenario is:

(To make the scenario general for readers, lets have this illustration.)

We have this Box.png and Circle.png declared:

private final URL IMG1_DIRECTORY = Main.class.getResource("/res/Box.png");
private final URL IMG2_DIRECTORY = Main.class.getResource("/res/Circle.png");

Under our constructor:

try {
    box = ImageIO.read(IMG1_DIRECTORY);
} catch (Exception e) {
    // Our catchblock here
}

try {
    circle= ImageIO.read(IMG2_DIRECTORY);
} catch (Exception e) {
    // Our catchblock here
}

currentImg = box;

With the paint method, the box is drawn to our JPanel as shown in our Illustration 1.

@Override
public void paint(Graphics g) {
    g.drawImage(currentImg, DEFAULT_LOCATION, DEFAULT_LOCATION, null);
}

Through a certain event, mousePressed in this example the Image will be changed.

@Override
public void mousePressed( MouseEvent e ) {
        currentImg = circle;
        repaint();
}

The desired output is shows in our Illustration 2. Unfortunately, the result happens to be the Illustration 3.
The question was:
– Why is the result happens to be both image overlaying each other?
– Another thing, if I have a code that will repaint the image to circle ( From Illustration 3 ) the box will just overlay the circle image.

enter image description here

  • 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-16T23:02:05+00:00Added an answer on June 16, 2026 at 11:02 pm
    1. You’ve failed to call super.paint, which, apart from a whole bunch of other important stuff, clears the graphics context
    2. You should rarly need to override paint, it’s normally preferred to use paintComponent, but make sure you call super.paintComponent

    The graphic context is a shared resource and tends to be re-used between repaints, that means, because you didn’t clear the graphics context when you painted, you’ve got the previous “state”, which then paint over

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

Sidebar

Related Questions

hello fellow java developers. I'm having a bit of an issue here. I have
Hello I am a Java web app developer I have a problem, I would
Possible Duplicate: Appending files to a zip file with Java Hello Java Developers, Here's
hello fellow java developers. I'm having a very strange issue. I'm trying to read
Hello Java Developers, Here's a straight question. How would I be able to set
hello i'm a beginner java developer and this is one question in a list
Hello android developers, I was struggling to get this work but failed and now
Hello android/Java developers, When a function call a function and that function call another
I have an application project with source files: androidsrc/MainActivity.java androidsrc/ClientSession.java I get this error
Hello Python developers! I'm a Java one and I know that there is 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.