我无法在鼠标监听器组件上绘图

发布于 2024-10-18 13:11:56 字数 3923 浏览 2 评论 0原文

我现在无法发布任何代码,因为我正在编程的计算机没有互联网连接,我绝对拒绝在这部手机上写出来。

基本上,我有一个 JPanel (它实现 mouseListener),它在其 contentPane 中包含一个 ComponentJPanel 正在侦听 Component 上的鼠标事件。

当我绘制到面板时,它工作正常,除了 Component 下的区域(可见但不绘制任何内容)仅显示面板的背景(标准颜色填充)而不是我绘制的图像在它上面。

我感觉我错过了与 mouseListeners 相关的一些基本内容...

好吧,这是整个课程,现在我的计算机再次按预期工作:(

另外,似乎我使用的是标签,而不是组件。对此感到抱歉.)

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


}

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


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无远思近则忧 2024-10-25 13:11:56

该组件还将有一个 paintComponent 方法,默认情况下该方法将绘制容器的背景。您需要将方法 setOpaque 重写为 false(取决于组件)或其他方法以防止绘制发生。

但是,听起来您真正想要的是向面板添加鼠标侦听器,并让它侦听定义的边界而不是面板,而不是向面板添加组件。

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.

一杯敬自由 2024-10-25 13:11:56

我现在无法发布任何代码

您编写的自定义代码不起作用,并且您希望我们猜测该代码是什么样的?我们没有时间去猜测您可能犯了什么错误。论坛的目的是让我们轻松回答问题,这意味着您需要提供我们需要的所有信息来帮助解决问题。

这就是为什么您需要提供一个 SSCCE 来演示该问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文