Java:具有自定义形状的JButton:具有金属外观和渐变感 [英] Java: JButton with custom Shape: Fill with Metal Look and Feel Gradient

查看:226
本文介绍了Java:具有自定义形状的JButton:具有金属外观和渐变感的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从JButton派生的新类,它给了我Enter键的形状.

I have a new class derived from JButton which gives me the shape of a Enter-Button.

现在,我要用与默认JButton相同的渐变填充它. 但是我不知道.我该怎么做?

Now I want to have it filled with the same gradient as the default JButton. But I do not know. How I can do this?

此刻,它充满了纯黑色.

At the moment it is filled with plain black color.

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class EnterButton extends JButton {

    private Polygon shape;

    public EnterButton() {
        this.shape = new Polygon();
        // initialisiere Form
        this.initialize();
    }

    protected void initialize() {
        Point p1, p2, p3, p4, p5, p6;

        this.setSize(90, 120);

        p1 = new Point(0, 0);
        p2 = new Point(0, 60);
        p3 = new Point(30, 60);
        p4 = new Point(30, 120);
        p5 = new Point(90, 120);
        p6 = new Point(90, 0);

        this.shape.addPoint((int) Math.round(p1.getX()),
                (int) Math.round(p1.getY()));
        this.shape.addPoint((int) Math.round(p2.getX()),
                (int) Math.round(p2.getY()));
        this.shape.addPoint((int) Math.round(p3.getX()),
                (int) Math.round(p3.getY()));
        this.shape.addPoint((int) Math.round(p4.getX()),
                (int) Math.round(p4.getY()));
        this.shape.addPoint((int) Math.round(p5.getX()),
                (int) Math.round(p5.getY()));
        this.shape.addPoint((int) Math.round(p6.getX()),
                (int) Math.round(p6.getY()));
        this.setMinimumSize(this.getSize());
        this.setMaximumSize(this.getSize());
        this.setPreferredSize(this.getSize());
    }

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

    // Zeichne den Button
    protected void paintComponent(Graphics g) {
        Graphics2D gCopy = (Graphics2D) g.create();
        gCopy.fillPolygon(this.shape);

    }

    // zeichne die Border
    protected void paintBorder(Graphics g) {

    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        EnterButton button = new EnterButton();

        panel.add(button);
        frame.add(panel);

        frame.pack();
        frame.setVisible(true);

    }

}

谢谢!

推荐答案

请参见 java.awt.GradientPaint .

还要搜索类javax.swing.plaf.metal.MetalButtonUIupdate(Graphics, JComponent)方法的源代码.在您的JDK下.

Also search for the source code of the update(Graphics, JComponent) method of class javax.swing.plaf.metal.MetalButtonUI. That's somewhere under your JDK.

这篇关于Java:具有自定义形状的JButton:具有金属外观和渐变感的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆