Hey so for my lab we were given this code and told to fix it so that each candidate's information is printed on the appropriately colored background. The voting space for sarah is supposed to be on a green background, and the voting space for joe is supposed to be on a red back ground. My new code which DOES NOT WORK is listed after the old, given code that we start with.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.EventQueue;
import javax.swing.JFrame;
/**
* @author rhyspj
* 11xi11
*
*/
public class Election1 extends JFrame implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int MARGIN = 20;
private int joeVotes, sarahVotes;
public Election1() {
setSize(600,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(this);
joeVotes = sarahVotes = 0;
repaint();
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.black);
g.drawString("Click on the left half to vote", MARGIN, 2*MARGIN);
g.drawString("for Joe \"The Plumber\" Palin", MARGIN, 3*MARGIN);
g.drawString("Click on the right half to vote", getWidth()/2 + MARGIN, 2*MARGIN);
g.drawString("for Kay Sarah Sera", getWidth()/2 + MARGIN, 3*MARGIN);
g.drawString("Joe has "+ joeVotes, MARGIN, 6*MARGIN);
g.drawString("Kay has "+ sarahVotes, getWidth()/2 + MARGIN, 6*MARGIN);
g.drawLine(getWidth()/2, 0, getWidth()/2, getHeight());
}
public void mouseClicked(MouseEvent clickEvent) {
if (clickEvent.getX() < getWidth()/2) {
joeVotes++;
repaint();
}
else {
sarahVotes++;
repaint();
}
}
public void mouseEntered(MouseEvent arg0) {
// Not needed, but must be defined to satisfy interface
}
public void mouseExited(MouseEvent arg0) {
// Not needed, but must be defined to satisfy interface
}
public void mousePressed(MouseEvent arg0) {
// Not needed, but must be defined to satisfy interface
}
public void mouseReleased(MouseEvent arg0) {
// Not needed, but must be defined to satisfy interface
}
public static void main(String[] args) {
// This is the approved form for running a JFrame
// Don't worry, Eclipse generates it for you
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Election1 frame = new Election1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Election2 extends JFrame implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int MARGIN = 20;
private Candidate joe, sarah;
public Election2() {
setSize(600,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(this);
joe = new Candidate(Color.red, "Joe \"the plumber\" Palin", MARGIN, MARGIN, getWidth()/2 - MARGIN, getHeight()-MARGIN);
sarah = new Candidate(Color.green, "Kay Sarah Sera", MARGIN+getWidth()/2, MARGIN, getWidth()/2 - MARGIN, getHeight()-MARGIN);
repaint();
}
public void paint(Graphics g) {
joe.paint(g);
sarah.paint(g);
}
public void mouseClicked(MouseEvent clickEvent) {
if (clickEvent.getX() < getWidth()/2) {
joe.registerVote();
repaint();
}
else {
sarah.registerVote();
repaint();
}
}
public void mouseEntered(MouseEvent arg0) {
// Not needed, but must be defined to satisfy interface
}
public void mouseExited(MouseEvent arg0) {
// Not needed, but must be defined to satisfy interface
}
public void mousePressed(MouseEvent arg0) {
// Not needed, but must be defined to satisfy interface
}
public void mouseReleased(MouseEvent arg0) {
// Not needed, but must be defined to satisfy interface
}
public static void main(String[] args) {
// This is the approved form for running a JFrame
// Don't worry, Eclipse generates it for you
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Election2 frame = new Election2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
ALSO this is the candidate class that these applets are referencing
import java.awt.Color;
import java.awt.Graphics;
public class Candidate {
private Color color;
protected String name;
protected int votes;
private int x, y, width, height;
public Candidate(Color c, String name, int x, int y, int width, int height) {
votes = 0;
this.name = name;
color = c;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void paint(Graphics g) {
// TODO Auto-generated method stub
}
public void registerVote() {
// TODO Auto-generated method stub
}
}