从按钮网格绘制特定按钮 [英] painting a particular button from a grid of buttons

查看:99
本文介绍了从按钮网格绘制特定按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下按钮网格定义为:

JButton button_x = new RoundButton();

其中 RoundButton 定义为:

public class RoundButton extends JButton {

    public RoundButton(String label) {
        super(label);
        this.setContentAreaFilled(false);
        Dimension size = this.getPreferredSize();
        size.height = size.width = Math.max(size.height, size.width);
        this.setPreferredSize(size);
    }

    @Override
    protected void paintComponent(Graphics g) {
        if(!GameState.getIfComplete()) { // If the game is not complete or has just started
            this.setBorder(null);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, this.getSize().width, this.getSize().height);
            if(this.getModel().isArmed()) {
               g.setColor(Color.RED);
            }else {
                g.setColor(Color.GREEN);
            }
            g.fillOval(0,0,this.getSize().width-1,this.getSize().height-1);
            super.paintComponent(g);
        }else {
            this.setBorder(null);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, this.getSize().width, this.getSize().height);
            g.setColor(Color.WHITE);
            g.fillOval(0,0,this.getSize().width-1,this.getSize().height-1);
            super.paintComponent(g); 
        }
    }

}

目前所有的按钮都是绿色的,但在某种情况下,我想用白色绘制特定的按钮(这是else部分中的代码)。例如当!GameState时。 getIfComplete()返回 false 我想在第一列中绘制白色的按钮。所以我打电话给重绘

Currently all the buttons are painted in green, but on a certain condition I want to paint particular buttons in white (which is the code in the else part).For instance when !GameState.getIfComplete() returns false I want to paint the buttons in the first column in white. So I call repaint as :

buttons[0].repaint();
buttons[3].repaint();
buttons[6].repaint();

但这不起作用!在第一列中,其他一些按钮也涂成白色。这是为什么 ?

But this doesn't work ! With the first column some other buttons are also painted in white. Why is that ?

电话有什么问题?如何绘制特定按钮?

What is wrong with the call ? How do I paint a particular button ?

推荐答案

问题在于依赖 GameState ,所有圆形按钮都使用相同的逻辑来绘制自己,也就是说,当游戏完成时,它们都将被绘制 WHITE

The problem is the reliance on the GameState, ALL your round buttons use the same logic to paint themselves, that is, when the game is completed, they will all be painted WHITE

相反,您应该依赖按钮的属性。设置它以使颜色实际上来自按钮本身。

Instead, you should rely on the properties of the button. Set it up so that the colors are actually derived from the button itself.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BadButton01 {

    public static void main(String[] args) {
        new BadButton01();
    }

    public BadButton01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public static class GameState {

        private static boolean isComplete;

        public static boolean getIfComplete() {
            return isComplete;
        }

        public static void setComplete(boolean value) {
            isComplete = value;
        }

    }

    public class TestPane extends JPanel {

        private RoundButton[] btns = new RoundButton[]
        {
            new RoundButton("1"),
            new RoundButton("2"),
            new RoundButton("3"),
            new RoundButton("4"),
            new RoundButton("5"),
            new RoundButton("6"),
            new RoundButton("7"),
            new RoundButton("8"),
            new RoundButton("9")
        };

        public TestPane() {
            setLayout(new GridLayout(3, 3));
            for (RoundButton btn : btns) {
                add(btn);
            }
            btns[0].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    GameState.setComplete(true);
                    btns[0].setBackground(Color.WHITE);
                    btns[1].setBackground(Color.WHITE);
                    btns[2].setBackground(Color.WHITE);
                    repaint();
                }
            });
        }

    }

    public class RoundButton extends JButton {

        public RoundButton(String label) {
            super(label);
            this.setContentAreaFilled(false);
            setBorderPainted(false);
            setFocusPainted(false);
            setOpaque(false);
            Dimension size = this.getPreferredSize();
            size.height = size.width = Math.max(size.height, size.width);
            this.setPreferredSize(size);
            setBackground(Color.GREEN);
        }

        @Override
        protected void paintComponent(Graphics g) {
//            if (!GameState.getIfComplete()) { // If the game is not complete or has just started
//                this.setBorder(null);
//                g.setColor(Color.BLACK);
//                g.fillRect(0, 0, this.getSize().width, this.getSize().height);
                if (this.getModel().isArmed()) {
                    g.setColor(Color.RED);
                } else {
//                    g.setColor(Color.GREEN);
                    g.setColor(getBackground());
                }
//            } else {
//                this.setBorder(null);
//                g.setColor(Color.BLACK);
//                g.fillRect(0, 0, this.getSize().width, this.getSize().height);
//                g.setColor(Color.WHITE);
//                g.fillOval(0, 0, this.getSize().width - 1, this.getSize().height - 1);
//                g.setColor(getBackground());
//            }
            g.fillOval(0, 0, this.getSize().width - 1, this.getSize().height - 1);
            super.paintComponent(g);
        }

    }

}

这篇关于从按钮网格绘制特定按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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