【问题标题】:Java - JComponent in polygon shape with background imageJava - 具有背景图像的多边形形状的 JComponent
【发布时间】:2018-02-26 10:58:25
【问题描述】:

我想在我的 JFrame 上有一个 JComponent,它有一个多边形形式的自定义形状。现在我想添加一个背景图像,颜色相同,其余部分为空白。

有没有办法做到这一点?

我有这个测试类:

public class Test extends JButton {
private final Polygon shape;
private final int provinceId;
private ImageIcon img;

public Test(Polygon p, int x, int y, int w, int h, int id, ImageIcon img) {
    this.shape = p;
    this.provinceId = id;
    this.img = img;
    setSize(w, h);
    setLocation(x, y);
    setIcon(img);
    setContentAreaFilled(false);
    addMouseListener(animation());
}

private MouseListener animation() {
    return new MouseListener() {
        public void mouseEntered(MouseEvent e) {
            System.out.println("in");
        }

        public void mouseExited(MouseEvent e) {
            System.out.println("out");
        }

        public void mouseClicked(MouseEvent e) {

        }

        public void mousePressed(MouseEvent e) {

        }

        public void mouseReleased(MouseEvent e) {

        }
    };
}

protected void paintComponent(Graphics g) {
    g.drawPolygon(this.shape);
}

protected void paintBorder(Graphics g) {
    g.drawPolygon(this.shape);
}

public boolean contains(int x, int y) {
    return this.shape.contains(x, y);
}

public boolean isOpaque() {
    return false;
}

public int getId() {
    return this.provinceId;
}

public static void main(String[] args) {
    JFrame f = new JFrame();
    //Polygon p = new Polygon(new int[] {0, 400, 400, 0}, new int[] {0, 0, 300, 300}, 4);
    Polygon p = new Polygon(new int[] {50, 150, 250, 350, 200, 50}, new int[] {0, 0, 50, 200, 300, 200}, 6);
    ImageIcon ico = new ImageIcon("gfx/test.png");
    Test t = new Test(p, 20, 20, 400, 300, 101, ico);
    f.getContentPane().add(t);
    f.setSize(500, 400);
    f.setLayout(null);
    f.setVisible(true);
}

}

但我只得到这个输出:output

我的原图是:wanted output

【问题讨论】:

    标签: java jframe jcomponent


    【解决方案1】:

    您可以通过重写 contains(Point p) 方法来做到这一点,仅当 p 在您的自定义形状的范围内时才返回 true。

    那么你最好覆盖 isOpaque() 以返回 false。

    最后你覆盖paintComponent(Graphics g) 以你喜欢的任何方式来绘制你的组件(例如,背景图像的形状是颜色,其余部分是空白颜色)。

    示例代码:

    JPanel panel = new JPanel()
    {
        @Override
        public boolean isOpaque()
        {
            return false;
        }
    
        @Override
        public boolean contains(Point p)
        {
            // Use something that fits your shape here.
            return p.getX() % 2 == 0;
        }
    
        @Override
        public void paintComponent(Graphics g)
        {
            // do some painting
        }
    };
    

    【讨论】:

    • 我应该添加的一件事是,我想把这些组件中的一些放在一起。怎么回事,这些过度的角落没有互相阻挡。
    • 您必须将组件相互重叠放置,确保您的 contains 方法负责确定哪些组件被命中。
    • 这意味着使用允许您重叠组件的布局。
    • 我已经用我所做的尝试更新了我的问题。你能看一下,甚至可以说我做错了什么吗?
    • 它似乎有效?当我用鼠标进入多边形时,它会显示in,当我再次将鼠标移出时,它会显示out
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 2012-12-07
    • 2013-03-02
    • 1970-01-01
    相关资源
    最近更新 更多