?
Solved

A Java applet using the Delegation Event Model(DEM)

Posted on 2005-04-18
17
Medium Priority
?
226 Views
Last Modified: 2013-11-23
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.
0
Comment
Question by:Gaute Rønningen
  • 9
  • 5
  • 2
  • +1
17 Comments
 
LVL 14

Expert Comment

by:StillUnAware
ID: 13808325
What have You done already?
Can You post Your code?
0
 

Author Comment

by:Gaute Rønningen
ID: 13808408
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 13808413
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 13808450
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13808478
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
 

Author Comment

by:Gaute Rønningen
ID: 13808715
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13808883
>> How do I use the paintComponent() method?

Just leave it as it is - it's fine
0
 
LVL 92

Expert Comment

by:objects
ID: 13810561
>    private Graphics kakediagram;

get rid of that variable
0
 
LVL 92

Expert Comment

by:objects
ID: 13810564
>           pack();

you can also get rid of that line
0
 

Author Comment

by:Gaute Rønningen
ID: 13814320
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13814365
>> When I call for the method paintComponent()

You don't call paintComponent. The system calls it. Follow the instructions i posted above
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13814384
The incrementing methods should be:

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

and similar for No
0
 

Author Comment

by:Gaute Rønningen
ID: 13814484
>> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13814521
>>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
 

Author Comment

by:Gaute Rønningen
ID: 13814866
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13814894
>>super.paint(g);

should be


super.paintComponent(g);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13815123
:-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question