Question

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();
    }   


}
Was it helpful?

Solution

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.

OTHER TIPS

I can't post any code right now

You wrote custom code that doesn't work and you expect us to guess what that code looks like? We don't have time to spend guessing what mistake you might have made. The point of the forums is to make it easy for us to answer the question which means you need to provide all the information we need to help solve the problem.

That is why you need to provide a SSCCE that demonstrates the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top