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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:50:16+00:00 2026-05-20T04:50:16+00:00

I can’t post any code right now, since the computer I’m programming on has

  • 0

I can’t post any code right now, since the computer I’m programming on has no internet connection, and I absolutely refuse to write it out on this phone.

Basically, I have a JPanel (which implements mouseListener), which contains a Component in its contentPane. The JPanel is listening for mouse events on the Component.

When I draw to the panel, it works fine except that the area under the Component (which is visible but not painting anything) just shows the Panel’s background (a standard colour fill) and not the image I drew on top of it.

I get the feeling that I’m missing something fundamental to do with mouseListeners…

OK, here’s the whole class, now that my computer’s working as intended again:

(Also, it seems I was using Labels, not Components. Sorry about that.)

import java.awt.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.util.*;

public class PictureViewer extends Container implements MouseListener, ComponentListener
{
    java.util.List<Image> images;
    public Component leftSide, rightSide;
    int currentImage;
    boolean leftMoused, rightMoused;
    boolean mouseDown;
    Image leftTab, rightTab, noImage;

    public PictureViewer()
    {
        setVisible(true);
        setBackground(Color.BLUE);
        addComponentListener(this);

        images = new ArrayList<Image>();

        leftSide = new Label();
        leftSide.setLocation(0, 0);
        leftSide.setSize(getWidth() / 2, getHeight());
        leftSide.addMouseListener(this);
        add(leftSide);

        rightSide = new Label();
        rightSide.setLocation(getWidth() / 2, 0);
        rightSide.setSize(getWidth() / 2, getHeight());
        rightSide.addMouseListener(this);
        rightSide.setVisible(false);
        add(rightSide);

        noImage = Toolkit.getDefaultToolkit().getImage(getClass().getResource("Images/No Picture.png"));
        leftTab = Toolkit.getDefaultToolkit().getImage(getClass().getResource("Images/Left Tab.png"));
        rightTab = Toolkit.getDefaultToolkit().getImage(getClass().getResource("Images/Right Tab.png"));
    }


    public void addImage(Image image)
    {
        images.add(image);
    }
    public void clear()
    {
        images.clear();
    }

    public void paint(Graphics g)
    {       
        super.paint(g);

        Graphics2D g2d = (Graphics2D)g;

        Image imageToDraw;

        if (images.size() > 0)
        {
            imageToDraw = images.get(currentImage);
        }
        else
        {
            imageToDraw = noImage;
        }

        g2d.drawImage(imageToDraw, getX(), getY(), getWidth(), getHeight(), 0, 0, imageToDraw.getWidth(this), imageToDraw.getHeight(this), this);
        g2d.draw(new Rectangle(0, 0, 20, 20));

        if (leftMoused)
        {
            g2d.drawImage(leftTab, getX() + 8, getY() + (int)(getSize().getHeight() - leftTab.getHeight(this) / 2), this);
        }
    }

    public void componentHidden(ComponentEvent e){}
    public void componentShown(ComponentEvent e){}
    public void componentMoved(ComponentEvent e)
    {
        componentResized(e);
    }
    public void componentResized(ComponentEvent e)
    {
        leftSide.setLocation(getLocation());
        leftSide.setSize(getWidth() / 2, getHeight());

        rightSide.setLocation((int)(getLocation().getX() + (getWidth() / 2)), (int)getLocation().getY());
        rightSide.setSize(leftSide.getSize());

        System.out.println(getSize());

        repaint();
    }   

    public void mouseReleased(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseEntered(MouseEvent e)
    {   
    if (e.getComponent() == leftSide){
        leftMoused = true;
        System.out.println("Left");}
    else {
        rightMoused = true;
        System.out.println("Right");}

    repaint();
    }
    public void mouseExited(MouseEvent e)
    {   
    if (e.getComponent() == leftSide)
        leftMoused = false;     
    else
        rightMoused = false;

    repaint();
    }   


}
  • 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-20T04:50:16+00:00Added an answer on May 20, 2026 at 4:50 am

    The component will also have a paintComponent method which by default will paint the container’s background. You will need to override the method, setOpaque to false (depending on the component), or something else to keep painting from happening.

    However, it sounds like what you really want is to add mouse listener to the panel and have it listen to a defined boundary instead the panel rather than adding a component to the panel.

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

Sidebar

Related Questions

Can i get the source code for a WAMP stack installer somewhere? Any help
can any one tell me how can change this java code into objective c.is
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can a LINQ enabled app run on a machine that only has the .NET
I have a jquery bug and I've been looking for hours now, I can't
Can someone give me a clear, concise definition of the difference between a programming
Can I have a project that has some parts written in c and other
Can some one confirm me that only one UIWindow instance is possible in any
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can we close all known/unknown connections to database with the code? I'm using Access

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.