Opinon Poll Assignment
Can someone please help!. I am doing an assginment in college. I am required to do a world cup opinion poll where fans can vote which team will win the world cup in 2006. There 4 radio buttons represanting 4 countries, Brazil, England, Germany and Italy.A fan selects a button and clicks the Vote button to vote. A the vote button must be kept disabled until a button is selected. Each vote cast should increase the bar chart for that partcular team. I am doing something terribly wrong somewhere in my code. I think it is in the ActionPerformed method. I am not good in java so your help is important. I hope my explantion is sensible. Here is my code below.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class TaskA extends JFrame implements ActionListener{
JLabel question = new JLabel(" Who will win the World Cup?");//Question Label
double x, y, height, width;
int country [] = new int [4];
String teams [] = {"Brazil", "England", "Germany", "Italy"};
Font italicFont = new Font("Arial",Font.ITALIC,12);
JButton btnVote ;
public TaskA(){// Constructor
super("Assign06b");
setSize(550, 450);// size of thye frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font currentFont = new Font("Arial", Font.BOLD,20);//creating a font object for the banner/question
question.setFont(currentFont);
Font westFont = new Font("Arial", Font.ITALIC,12);// creating a font object for bar names
JRadioButton brazil = new JRadioButton("Brazil");// RadioButton1
brazil.setActionCommand("Brazil");
JRadioButton england = new JRadioButton("England");// RadioButton2
england.setActionCommand("England");
JRadioButton germany = new JRadioButton("Germany");// RadioButton3
germany.setActionCommand("Germany");
JRadioButton italy = new JRadioButton("Italy");// RadioButton4
italy.setActionCommand("Italy");btnVote.setEnabled(true);// how can I make this to appear false then change to true when radio button is selected?
btnVote.addActionListener(this);
ButtonGroup group = new ButtonGroup();//Group to hold buttons
group.add(brazil);
group.add(england);
group.add(germany);
group.add(italy);
JPanel rButtons = new JPanel();//Input Panel
BoxLayout vertical = new BoxLayout(rButtons, BoxLayout.Y_AXIS);
rButtons.setLayout(vertical);
rButtons.add(question);
rButtons.add(brazil);
rButtons.add(england);
rButtons.add(germany);
rButtons.add(italy);
rButtons.add(btnVote);
DrawFillRectPanel dr = new DrawFillRectPanel();
JPanel content = new JPanel();
BorderLayout bord = new BorderLayout();
setLayout(bord);
add(rButtons, BorderLayout.NORTH);
add(dr, BorderLayout.CENTER);
setVisible(true);
height = 40;
country[0] = 80; // I am putting here some figures so you can see what we are suppossed to do but I shoud not do it this way!!
country[1] = 0;
country[2] = 0;
country[3] = 0;
}
public void actionPerformed (ActionEvent event){
Object source = event.getSource();
// teams = "Brazil", "England", "Germany", "Italy";
if(source == btnVote){
btnVote.getActionCommand("Brazil");
for (int country [0] = 0; country [0]<= 100, country[0]++){// I don't think this is right but I am really stuck here!!
System.out.println(country[0] + "votes" + country[0] + "%");
}
else
{
btnVote.getActionCommand("England");
} if (source == btnVote)
{
btnVote.getActionCommand("Germany");
}else if (source == btnVote)
{
btnVote.getActionCommand("Italy");
}
}
//Overriding getInsets() to place custom borders round the frame.
public Insets getInsets() {
return new Insets(40, 15, 15, 15);
}
class DrawFillRectPanel extends JPanel {
public void paintComponent (Graphics comp) {
super.paintComponent (comp);
Graphics2D comp2D = (Graphics2D)comp;
comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
setBackground(Color.cyan);
comp2D.setColor(Color.black);
x =100;
y =40;
comp2D.drawString(" ",(int) (x - 25), (int) (y-25));
Rectangle2D.Double rect = new Rectangle2D.Double();
for (int team = 0; team <4; team++) {
comp2D.drawString(teams[team],
(int) (x-70),(int) (y + (height/2)));
width = country[team]*4;
rect.setRect(x,y,width,height);
comp2D.setColor(Color.red);
comp2D.fill(rect);
comp2D.setColor(Color.black);
comp2D.setFont(italicFont);
comp2D.draw(rect);
y = y +height;
}
}
}
public static void main(String[] args){
TaskA frame = new TaskA();
}
}

