在 JApplet 中的填充矩形和轮廓矩形之间交替



我有以下简短易懂的类,允许用户使用正方形的大小并在两种颜色之间交替。我如何允许用户在填充矩形(当前矩形)和轮廓矩形(轮廓矩形 = 没有内部颜色,只有边框)之间交替。最短和最简单的修改会很棒。我只是在寻找最简单的解决方案。

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
public class Square extends JApplet{
    int size;
    Color color;
    JButton plus, minus, reset, changeColor;
    public void init(){
        color = Color.GREEN;
        size = 100;
        plus = new JButton("+");
        minus = new JButton("-");
        reset = new JButton("reset");
        changeColor = new JButton("color");
        setLayout(new FlowLayout());
        add(plus); add(minus); add(reset); add(changeColor);
        plus.addActionListener(new ButtonHandler(this));
        minus.addActionListener(new ButtonHandler(this));
        reset.addActionListener(new ButtonHandler(this));
        changeColor.addActionListener(new ButtonHandler(this));
    }
    public void paint(Graphics g){
        g.setColor(color);
        g.fillRect(20, 20, size, size);
    }
    class ButtonHandler implements ActionListener{
        Square myApplet;
        ButtonHandler(Square sq){
            this.myApplet = sq;
        }
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==myApplet.plus)
                myApplet.size+=10;
            if(e.getSource()==myApplet.minus)
                myApplet.size-=10;
            if(e.getSource()==myApplet.reset)
                myApplet.size=100;
            if(e.getSource()==myApplet.changeColor){
                if(myApplet.color==Color.GREEN)
                    myApplet.color=Color.RED;
                else
                    myApplet.color=Color.GREEN;
            }
            myApplet.repaint();
        }
    }
}

以为我会发布我自己问题的答案。当我正在寻找一个简单的解决方案时,以下内容可以做到这一点:

import java.applet.Applet;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Square extends Applet{
    int size;
    Color color;
    JButton plus, minus, reset, changeColor, alternate; int shapeDetector;
    public void init(){
        color = Color.GREEN;
        size = 100;
        shapeDetector = 1;
        plus = new JButton("+");
        minus = new JButton("-");
        reset = new JButton("reset");
        changeColor = new JButton("color");
        alternate = new JButton("alternate");
        setLayout(new FlowLayout());
        add(plus); add(minus); add(reset); add(changeColor); add(alternate);
        plus.addActionListener(new ButtonHandler(this));
        minus.addActionListener(new ButtonHandler(this));
        reset.addActionListener(new ButtonHandler(this));
        alternate.addActionListener(new ButtonHandler(this));
        changeColor.addActionListener(new ButtonHandler(this));
    }
    public void paint(Graphics g){
        super.paint(g);
        g.setColor(color);
        if(shapeDetector==1)
            g.fillRect(20, 20, size, size);
        else
            g.drawRect(20, 20, size, size);
    }
    class ButtonHandler implements ActionListener{
        Square myApplet;
        ButtonHandler(Square sq){
            this.myApplet = sq;
        }
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==myApplet.plus)
                myApplet.size+=10;
            if(e.getSource()==myApplet.minus)
                myApplet.size-=10;
            if(e.getSource()==myApplet.reset)
                myApplet.size=100;
            if(e.getSource()==myApplet.changeColor){
                if(myApplet.color==Color.GREEN)
                    myApplet.color=Color.RED;
                else
                    myApplet.color=Color.GREEN;
            }
            if(e.getSource()==myApplet.alternate){
                if(myApplet.shapeDetector==1)
                    myApplet.shapeDetector=2;
                else
                    myApplet.shapeDetector=1;
            }
            myApplet.repaint();
        }
    }
}

注意:有更好的方法可以做到这一点。

相关内容

  • 没有找到相关文章

最新更新