0

I have the following short and simple to understand classes that allows the user to play around with the size of a square and alternate between two colors. How could i allow the user to alternate between a filled rectangle (the current one) and an outline rectangle (outline rectangle = no inner color, just bordered). The shortest and simplest modification would be great. I'm just looking for the simplest solution.

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();
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
TheDerp
  • 131
  • 1
  • 10
  • 2
    The first thing you should be doing is calling super.paint...that's after you stop using JAppet (applets have officially been deprecated and are no longer supported). Next, you simply need a flag which when the applet is repainted, you would check and either call fillRect or drawRect – MadProgrammer May 21 '16 at 09:47
  • 1
    [RIP](http://openjdk.java.net/jeps/289). – trashgod May 21 '16 at 10:45

1 Answers1

0

Thought i'd post the answer to my own question. As i was looking for a simple solution, the following does this:

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

note: there are better ways of doing this.

TheDerp
  • 131
  • 1
  • 10