Saturday, March 12, 2011

partisan voting applet

A fun applet which moves some of the buttons around when you try to click them so that they can't be depressed :)


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
/**
** Vote2006.java by Douglas A. Bauman
**/
public class Vote2006 extends Applet implements MouseMotionListener, MouseListener {
String title1a, title1b, title1c, title2a, title2b, title2c;
Button b1a, b1b, b1c, b2a, b2b, b2c;
int xpos, ypos, hits;
int rect1xco,rect1yco,rect1width,rect1height;
boolean rect1Active;
boolean rdB1;
Random r;

public void init() {
title1a = getParameter("title1a");
title1b = getParameter("title1b");
title1c = getParameter("title1c");
title2a = getParameter("title2a");
title2b = getParameter("title2b");
title2c = getParameter("title2c");
r = new Random();
b1a = new Button(title1a);
b1b = new Button(title1b);
b1c = new Button(title1c);
b2a = new Button(title2a);
b2b = new Button(title2b);
b2c = new Button(title2c);
setLayout(null);
add(b1a);
add(b1b);
add(b1c);
add(b2a);
add(b2b);
add(b2c);
hits = 0;
rect1xco = 100;
rect1yco = 50;
rect1width = 40;
rect1height= 26;
b1a.setBounds(rect1xco, rect1yco, rect1width, rect1height);
b1b.setBounds(rect1xco+rect1width+10, rect1yco, rect1width, rect1height);
b1c.setBounds(rect1xco+2*rect1width+20, rect1yco, rect1width, rect1height);
b2a.setBounds(rect1xco+10, rect1yco+rect1height, rect1width, rect1height);
b2b.setBounds(rect1xco+rect1width+10, rect1yco+rect1height, rect1width, rect1height);
b2c.setBounds(rect1xco+2*rect1width+10, rect1yco+rect1height, rect1width, rect1height);
//addMouseListener(this);
b1a.addMouseMotionListener(this);
b1b.addMouseMotionListener(this);
b1c.addMouseMotionListener(this);
}
public void destroy() {
//removeMouseListener(this);
//removeMouseMotionListener(this);
}
public void start() {
}
public void stop() {
}

public boolean handleEvent(Event e) {
if (title2a.equals(e.arg) ||
title2b.equals(e.arg) ||
title2c.equals(e.arg)) {
b1a.setBounds(rect1xco + r.nextInt()%rect1xco,
rect1yco + r.nextInt()%rect1yco,
rect1width, rect1height);
b1b.setBounds(rect1xco + r.nextInt()%rect1xco,
rect1yco + r.nextInt()%rect1yco,
rect1width, rect1height);
b1c.setBounds(rect1xco + r.nextInt()%rect1xco,
rect1yco + r.nextInt()%rect1yco,
rect1width, rect1height);
rect1Active = true;
showStatus("Thanks for your vote!");
repaint();
}
return true;
}
public void mouseReleased(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseDragged(MouseEvent e) {
}

public void mouseMoved(MouseEvent e) {
xpos = e.getX();
ypos = e.getY();
b1a.setBounds(rect1xco + r.nextInt()%rect1xco,
rect1yco + r.nextInt()%rect1yco,
rect1width, rect1height);
b1b.setBounds(rect1xco + r.nextInt()%rect1xco,
rect1yco + r.nextInt()%rect1yco,
rect1width, rect1height);
b1c.setBounds(rect1xco + r.nextInt()%rect1xco,
rect1yco + r.nextInt()%rect1yco,
rect1width, rect1height);
rdB1 = true;
rect1Active = false;
b1a.repaint();
b1b.repaint();
b1c.repaint();
b1a.getParent().repaint();
b1b.getParent().repaint();
b1c.getParent().repaint();
repaint();
e.consume();
}
public void paint(Graphics g) {
if (rect1Active) g.setColor(Color.red);
else g.setColor(Color.blue);
g.fillRect(0,0, 600-1, 200-1);
g.setColor(Color.blue);
if (rdB1) {
rdB1 = false;
showStatus("Please place your vote!");
}
}
}


<html> <head> <title>U.S. election of 1800</title> </head>
<body>
<a href="http://americanhistory.about.com/od/elections/p/election1800.htm">
Alexander Hamilton backed Charles Pinckney and saw Thomas Jefferson
as a bitter rival because of his stance on states’ rights. However, when the election came down to
Aaron Burr versus Thomas Jefferson, Hamilton put his weight behind
Jefferson because he couldn’t stand Burr. They would eventually meet in
a duel in 1804 in which Hamilton would be killed.</a>
<hr>
<applet code="Vote2006.class" width=600 height=200>
<param name=title1a value="Jefferson">
<param name=title1b value="Jay">
<param name=title1c value="Burr">
<param name=title2a value="Adams">
<param name=title2b value="Pinckney">
<param name=title2c value="Hamilton">
Sorry, You can't vote, since you are registered Republican.
</applet>
<br>
<a href="Vote2006.java"> The source</a>
</body> </html>


No comments:

Post a Comment