Results 1 to 3 of 3
Thread: Help with a Polling Program.
- 09-12-2012, 04:43 PM #1
Member
- Join Date
- Apr 2012
- Location
- St. Louis, MO
- Posts
- 32
- Rep Power
- 0
Help with a Polling Program.
I'm attempting to make a program that will count votes, and then formulate the number of votes into a pie chart. I have three parts to the program: The Control Panel, which manages the window, and vote buttons; The Display Panel, which contains my methods; and my driver, which I have named "Poll". All 3 parts of the program compile and run, however when I "Vote" for a subject, the pie chart disappears. I believe that the problem lies within my formulas in the display panel part of the program, but I haven't been able to figure it out. Any help is appreciated.
Here is the Display Panel part of the program:
Java Code:import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; public class PollDisplayPanel extends JPanel { private String name1, name2, name3; // Declare the int fields count1, count2, count3: //int count 1, count 2, count 3 int count1 = 0; // optional int count2 = 0; // optional int count3 = 0; // optional; //________________________________________________________ // Constructor public PollDisplayPanel(String nm1, String nm2, String nm3) { setBackground(Color.WHITE); name1 = nm1; name2 = nm2; name3 = nm3; //int count1 = 0; // optional // int count2 = 0; // optional // int count3 = 0; // optional } // Increments count1 public void vote1() { count1++; } // Increments count2 public void vote2() { count2++; } // Increments count3 public void vote3() { count3++; } // Returns a string representation of this object public String toString() { return "Candidate 1: "+count1+" Candidate 2: "+count2+" Candidate 3: "+count3; } // Redefines JPanel's paintComponent to draw this pie chart public void paintComponent(Graphics g) { super.paintComponent(g); int w = getWidth(); int h = getHeight(); int x = w/2; int y = h/2; int r = Math.min(w, h) / 4; drawPieChart(g, x, y, r); drawLegend(g, x, y, r); } // Draws the pie chart. private void drawPieChart(Graphics g, int x, int y, int r) { int total = count1 + count2 + count3; int fromDegree = 0; if (total > 0) { int degrees; g.setColor(Color.RED); degrees = countToDegrees;//(count1, total); drawSector(g, x, y, r, fromDegree, degrees); } else { g.setColor(Color.LIGHT_GRAY); drawSector(g, x, y, r, fromDegree, 360); } } // Draws the vote counts and the corresponding color squares. private void drawLegend(Graphics g, int x, int y, int r) { // Display the counts: y += (r + 20); g.setColor(Color.BLACK); g.drawString( (""+count1), x - r, y); g.drawString(""+count2, x, y); g.drawString( ""+count3, x + r, y); // Display the color squares: y += 5; x -= 2; g.setColor(Color.RED); g.fillRect(x - r, y, 10, 10); g.setColor(Color.GREEN); g.fillRect(x, y, 10, 10); g.setColor(Color.BLUE); g.fillRect(x + r, y, 10, 10); } // Returns the number of degrees in a pie slice that // corresponds to count / total, rounded to the nearest integer. private int countToDegrees() { int first= count/total; int num1= first*360; return num1; } // Draws a sector, centered at x, y, of radius r, // of angle measure degrees, starting at fromDegree. private void drawSector(Graphics g, int x, int y, int r, int fromDegree, int degrees) { if (degrees > 359) g.fillOval(x - r, y - r, 2 * r, 2 * r); else g.fillArc(x - r, y - r, 2 * r, 2 * r, fromDegree, degrees); } }
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PollControlPanel extends JPanel implements ActionListener { private JButton button1, button2, button3; private PollDisplayPanel chartPanel; public PollControlPanel(PollDisplayPanel chart) { chartPanel = chart; button1 = new JButton("Tami"); button1.setPreferredSize(new Dimension(80, 30)); button1.setToolTipText("Vote for Tami"); button1.addActionListener(this); button2 = new JButton("Brian"); button2.setPreferredSize(new Dimension(80, 30)); button2.setToolTipText("Vote for Brian"); button2.addActionListener(this); button3 = new JButton("Liz"); button3.setPreferredSize(new Dimension(80, 30)); button3.setToolTipText("Vote for Liz"); button3.addActionListener(this); add(button1); add(button2); add(button3); } // Processes button events public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); if (button == button1) chartPanel.vote1(); else if (button == button2) chartPanel.vote2(); else if (button == button3) chartPanel.vote3(); chartPanel.repaint(); } }
Java Code:import java.awt.*; import javax.swing.*; public class Poll extends JFrame { public Poll() { super("Vote for Tami, Brian, or Liz"); Container c = getContentPane(); c.setBackground(Color.WHITE); PollDisplayPanel chart = new PollDisplayPanel("Tami", "Brian", "Liz"); PollControlPanel controls = new PollControlPanel(chart); c.add(chart, BorderLayout.CENTER); c.add(controls, BorderLayout.SOUTH); } public static void main(String[] args) { Poll w = new Poll(); w.setBounds(300, 300, 400, 400); w.setDefaultCloseOperation(EXIT_ON_CLOSE); w.setVisible(true); } }
- 09-13-2012, 04:47 AM #2
Member
- Join Date
- Apr 2012
- Location
- St. Louis, MO
- Posts
- 32
- Rep Power
- 0
Re: Help with a Polling Program.
Bump for help.
- 09-13-2012, 08:32 AM #3
Re: Help with a Polling Program.
You are? How, by the two-keystroke method? (Ctrl-C / Ctrl-V)
http://www.chegg.com/homework-help/q...corre-q1804941
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
Synchronization of a variable during polling
By Fubarable in forum Threads and SynchronizationReplies: 3Last Post: 05-22-2012, 07:24 AM -
Polling mouse status directly without MouseEvent?
By Toll in forum AWT / SwingReplies: 4Last Post: 06-05-2011, 02:16 PM -
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
Tail without polling
By taotree in forum Advanced JavaReplies: 2Last Post: 11-12-2008, 08:57 AM -
FrameWork polling
By nanaji in forum Advanced JavaReplies: 0Last Post: 10-29-2008, 10:16 AM
Bookmarks