【问题标题】:Alternate between filled and outline rectangle in JApplet在 JApplet 中的填充矩形和轮廓矩形之间交替
【发布时间】:2016-05-21 09:38:57
【问题描述】:

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

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

【问题讨论】:

  • 您应该做的第一件事是调用 super.paint...那是在您停止使用 JAppet 之后(小程序已正式被弃用且不再受支持)。接下来,您只需要一个标志,当重新绘制小程序时,您将检查并调用 fillRect 或 drawRect
  • RIP.

标签: java swing graphics japplet


【解决方案1】:

我想我会发布我自己问题的答案。当我在寻找一个简单的解决方案时,以下是这样做的:

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

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

【讨论】:

  • 也可以使用Action查看相关的example
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多