import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
//Cole Williams
//Making smiles and colors with buttons
public class williamsclab10 extends JApplet implements ActionListener
{
Image fishimg;
String imgFile = "../images/fish.gif";
TextField text = new TextField(10);
int number = -1;
private static Random background = new Random();
private JButton color;
private JButton print;
private JButton change;
private JButton surprise;
int count = 0;
int A = 1;
int B = 1;
int C = 1;
int D = 1;
boolean vis = false;
boolean showmypicture = false;
public void init() {
setSize(600, 400);
Container content = getContentPane();
int red = Math.abs(background.nextInt()) % 256;
int green = Math.abs(background.nextInt()) % 256;
int blue = Math.abs(background.nextInt()) % 256;
getContentPane().setBackground(new Color(red, green, blue));
fishimg = getImage(getCodeBase(), imgFile);
setLayout(new FlowLayout());
text.addActionListener(this);
add(text);
color = new JButton("Change the color");
add(color);
color.addActionListener(this);
print = new JButton("Print my picture!");
add(print);
print.addActionListener(this);
change = new JButton("Change the picture!");
add(change);
change.addActionListener(this);
surprise = new JButton("Click here for a surpise!");
add(surprise);
surprise.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
Graphics g = this.getContentPane().getGraphics();
// button 1 background change
if (a.getSource() == color)
{
int red = Math.abs(background.nextInt()) % 256;
int green = Math.abs(background.nextInt()) % 256;
int blue = Math.abs(background.nextInt()) % 256;
getContentPane().setBackground(new Color(red, green, blue));
}
//button 2 prints picture centered
else if (a.getSource() == print)
{
drawpicture(g,400,50);
g.drawImage(fishimg, 50,100,200,200,this);
}
//button 3 picture toggling
else if (a.getSource() == change)
{
vis = true;
g.drawImage(fishimg, 50,100,200,200,this);
showmypicture =! showmypicture;
}
//button 4 surprise
else if (a.getSource() == surprise)
{
if (A == 1);
{
A = 1;
}
}
count++;
repaint();
}
private void drawpicture(Graphics g, int x, int y)
{
// tail
g.setColor(Color.blue);
g.drawLine(x, y, x + 30, y);
g.drawLine(x + 30, y, x + 30, y + 30);
g.drawLine(x + 30, y + 30, x, y);
// body
g.setColor(Color.blue);
g.drawOval(x + 20, y + -33, 40, 40);
g.setColor(Color.blue);
g.drawOval(x + 36, y + -32, 10, 10);
}
public void paint(Graphics g) {
super.paint(g);
Dimension d = getSize();
text.setLocation(d.width / 2 - 30, 30);
int centerX = d.width / 2;
int centerY = d.height / 2;
int widthOfPicture = 600;
int heightOfPicture = 600;
int x = centerX - widthOfPicture / 2;
int y = centerY - heightOfPicture / 2;
change.setSize(150, 50);
change.setLocation(d.width - 600, d.height - 50);
color.setSize(150, 50);
color.setLocation(d.width - 150, d.height - 50);
print.setSize(150, 50);
print.setLocation(d.width - 600, d.height - 400);
surprise.setSize(160, 50);
surprise.setLocation(d.width - 160, d.height - 400);
if (A == 1);
{
drawpicture(g,400,100);
g.drawImage(fishimg, 50,100,200,200,this);
}
}
}