A Java applet using the Delegation Event Model(DEM)

I've been working on this for 10 hours straight now, and it's not working... So would anyone please help me with this?

I'm supposed to create an application which is going to count Yes- and No-votes. The app. should have two JButtons (controller) to register the votes, and the results should be update continuesly in two separate views. The first view should show the results in a pie-chart. The second view shall show the actual numbers of the votes so far.

http://www.ex-co.net/images/Clipboard01.bmp
(Ja=Yes, Nei=No)

I've been told to use the following code for the pie-chart:
      public void paintComponent(java.awt.Graphics g) {
            super.paintComponent(g);
            int totalVotes = ...
            int yesVotes = ...
            if (totalVotes == 0) {
                  g.setColor(java.awt.Color.green);
                  g.fillOval(40,20,70,70);
            }
            else {
                  int vinkel = (int) Math.round(360.0 * yesVotes/totalVotes);
                  g.setColor(java.awt.Color.blue);
                  g.fillArc(40,20,70,70,0,vinkel);
                  g.setColor(java.awt.Color.red);
                  g.fillArc(40,20,70,70,vinkel, 360-vinkel);
            }
            g.setColor(java.awt.Color.black);
            g.drawOval(40,20,70,70);
      }  

Yes, I know you wont provide a complete solution to this, but could you at least help me get started? Cause all my work so far has been a waste... (Deadline in two days...) *head hurts*

Any help extremly appreciated,
eX.
Gaute RønningenOwner, DeveloperAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
You should just

1. add listeners to your buttons
2. increment a count in the handlers for each and call repaint
3. print that count to the text fields (uneditable)
4. call methods that return the count in paint
0
 
StillUnAwareCommented:
What have You done already?
Can You post Your code?
0
 
Gaute RønningenOwner, DeveloperAuthor Commented:
Well, I found out that I've been working on the wrong assignment, and the code I've been working on was based on Model-View-Controller/Observable so I tried to re-write the whole thing using DEM, without fully knowing what it meant... Been working on the DEM for about 2 hours now... So it's pretty much just GUI (which isn't working...), but here you go:

/*
 * Controller.java
 *
*/
package pclab15;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Controller extends JFrame {
      Data data;

      public static JTextField tf_ja, tf_nei;
      private JPanel topp, v1, v2, bunn;
      private JLabel l_ja, l_nei;
      private JButton b_ja, b_nei;
      private String v_ja = "Ja", v_nei = "Nei";
      private Graphics kakediagram;

      public Controller(Data d) {
            super("Model-View-Controller Demo");
            Lytter lytter = new Lytter();
            data = d;
          topp = new JPanel();
          v1 = new JPanel();
          v2 = new JPanel();          
          bunn = new JPanel();
          b_ja = new JButton(v_ja);
          b_nei = new JButton(v_nei);
          l_ja = new JLabel("JA-stemmer");          
          l_nei = new JLabel("NEI-stemmer");
            tf_ja = new JTextField(5);
            tf_nei = new JTextField(5);
            tf_ja.setEnabled(false);
            tf_nei.setEnabled(false);
            tf_ja.setText("0");
            tf_nei.setText("0");
            l_ja.setEnabled(false);
            l_nei.setEnabled(false);
            
            topp.setBorder(BorderFactory.createTitledBorder("Kontroller"));
            v1.setBorder(BorderFactory.createTitledBorder("View #1"));
            v2.setBorder(BorderFactory.createTitledBorder("View #2"));
            
            b_ja.addActionListener(lytter);
            b_nei.addActionListener(lytter);
            
            v1.setLayout(new GridLayout(1,1,5,5));
            kakediagram.drawOval(40,20,70,70);
            paintComponent(kakediagram,0,0);
            
            v2.setLayout(new GridLayout(2,2,5,5));
            v2.add(l_ja);
            v2.add(tf_ja);
            v2.add(l_nei);
            v2.add(tf_nei);
            
            topp.setLayout(new GridLayout(1,2,5,5));
            topp.add(b_ja);
            topp.add(b_nei);
            
            bunn.setLayout(new GridLayout(1,2,5,5));
            bunn.add(v1);
            bunn.add(v2);
            
          Container GUI = getContentPane();
            GUI.setLayout(new BorderLayout());
            
            GUI.add(topp, BorderLayout.NORTH);
            GUI.add(bunn, BorderLayout.CENTER);
            
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
            setVisible(true);
            setSize(400,250);
      } // end constructor

      private class Lytter implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                  String cmd = e.getActionCommand();
                  if (cmd.equals(v_ja)) {

                  } else if (cmd.equals(v_nei)) {

                  } // end if
            } // end method actionPerformed
      } // end class Lytter
      
     public void paintComponent(java.awt.Graphics g) {
          super.paintComponent(g);
          int totalVotes = 0;
          int yesVotes = 0;
          if (totalVotes == 0) {
               g.setColor(java.awt.Color.green);
               g.fillOval(40,20,70,70);
          }
          else {
               int vinkel = (int) Math.round(360.0 * yesVotes/totalVotes);
               g.setColor(java.awt.Color.blue);
               g.fillArc(40,20,70,70,0,vinkel);
               g.setColor(java.awt.Color.red);
               g.fillArc(40,20,70,70,vinkel, 360-vinkel);
          }
          g.setColor(java.awt.Color.black);
          g.drawOval(40,20,70,70);
     }  
} // end class Controller

/*
 * Klient.java
 *
 */
package pclab15;

/**
 * @author Gaute Rønningen
 * 13.apr.2005
 *
 */
public class Klient {
      public static void main (String args[]) {
            Data data = new Data(0);
            //Visning visning = new Visning();
            Controller kontroller = new Controller(data);
      } // end method main
} // end class Klient

/*
 * Data.java
 *
 */
package pclab15;

public class Data {

      /**
       *
       */
      public Data(int i) {
            super();
            // TODO Auto-generated constructor stub
      }

}
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
CEHJCommented:
0
 
CEHJCommented:
Having seen your code, i'll add to that:

1. Get rid of everything except Controller
2. Rename that class and make it extend JPanel
3. Give it a main method, create a JFrame in there and add your new class to it
0
 
Gaute RønningenOwner, DeveloperAuthor Commented:
How do I use the paintComponent() method? Just type paintComponent(graphics); ?
And the graphics, how do I create it. It says that it's a nullpointerexception in there and when I try to add a private Graphics kakediagram = new Graphics(); it says that it cannot be instantiated...
0
 
CEHJCommented:
>> How do I use the paintComponent() method?

Just leave it as it is - it's fine
0
 
objectsCommented:
>    private Graphics kakediagram;

get rid of that variable
0
 
objectsCommented:
>           pack();

you can also get rid of that line
0
 
Gaute RønningenOwner, DeveloperAuthor Commented:
When I call for the method paintComponent() what shall I use as source? When I get rid of the 'private Graphics kakediagram;' I don't have any source... or should I use repaint()?

Anyways this is my new PieChart component:

/*
 * Created on 19.apr.2005
 *
 */
package pclab15;

import java.awt.*;

public class PieChart extends Component {
      private int tot = 0, ja = 0;
      public PieChart() {
      }
      public void plussJa() {
            tot += tot;
            ja += ja;
            repaint();
      }
      public void plussNei() {
            tot += tot;
            repaint();
      }
      /**
       * @param g            java.awt.Graphics
       */
      public void paintComponent(java.awt.Graphics g) {
            super.paint(g);
            int totalt = tot;
            int jaStemmer = ja;
            if (totalt == 0) {
                  g.setColor(java.awt.Color.green);
                  g.fillOval(40,20,70,70);
            }
            else {
                  int vinkel = (int) Math.round(360.0 *
                                                      jaStemmer/totalt);
                  g.setColor(java.awt.Color.blue);
                  g.fillArc(40,20,70,70,0,vinkel);
                  g.setColor(java.awt.Color.red);
                  g.fillArc(40,20,70,70,vinkel, 360-vinkel);
            }
            g.setColor(java.awt.Color.black);
            g.drawOval(40,20,70,70);
      }  

}
0
 
CEHJCommented:
>> When I call for the method paintComponent()

You don't call paintComponent. The system calls it. Follow the instructions i posted above
0
 
CEHJCommented:
The incrementing methods should be:

public void plussJa() {
      tot++;
      ja++;
}

and similar for No
0
 
Gaute RønningenOwner, DeveloperAuthor Commented:
>> You don't call paintComponent. The system calls it.

But what do I add to my GUI, as in where I'd want it to be on my frame/panel?
0
 
CEHJCommented:
>>2. Rename that class and make it extend JPanel

OK, you've done the first part ('PieChart') of my above suggestion, but not the second. When you've made it into a JPanel subclass, you can add it wherever you like in your main gui frame
0
 
Gaute RønningenOwner, DeveloperAuthor Commented:
Ok, well... Debugging time. I get an error:
java.lang.StackOverflowError
      at javax.swing.SwingUtilities.appContextGet(SwingUtilities.java:1646)
      at javax.swing.DebugGraphics.info(DebugGraphics.java:1437)
      at javax.swing.DebugGraphics.debugComponentCount(DebugGraphics.java:1389)
      at javax.swing.JComponent.getComponentGraphics(JComponent.java:493)
      at javax.swing.JComponent.paint(JComponent.java:760)
      at pclab15.PieChart.paintComponent(PieChart.java:43)
      at javax.swing.JComponent.paint(JComponent.java:808)
      at pclab15.PieChart.paintComponent(PieChart.java:43)
      at javax.swing.JComponent.paint(JComponent.java:808)
...
That's at line 43 of the PieChart() class which is the "super.paint(g);"
Any idea what to do?
0
 
CEHJCommented:
>>super.paint(g);

should be


super.paintComponent(g);
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.