Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-25-2008, 05:52 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
Bar Graph
Hey I have a discrete Mathematics project due next week, and we have to do a probability graph. I have it all set up just I can't figure out what the x and y values to plot correctly. If someone could help me that would be much appreciated.
Code:
import java.awt.Color; import java.awt.Graphics; import javax.swing.JComponent; public class BarGraph extends JComponent implements Runnable { private Thread myThread; private Model myModel; private Coin[] myCoinArray; public static final double[] nValues = { 5, 5, 5, 5, 5, 5, 5, 15, 30, 50, 100, 150, 300, 500, 1000, 3000, 400, 700, 1500, 40000, 50000}; private int myN; private boolean isSimulated; private int myNumberOfRuns; private boolean isSimulating; private int[] myNumberOfTriesArray; private int[] myNumberOfRepeatedTries; private long mySpeed; public BarGraph() { myN = 0; this.start(); myModel = new Model(); isSimulated = false; isSimulating = false; } private void start() { myThread = new Thread(this); myThread.start(); } @Override public void run() { while(true) { repaint(); if(isSimulating) { for(int i = 0; i < myNumberOfRuns; i++) { myNumberOfTriesArray[i] = myModel.getNumberOfTries(); myNumberOfRepeatedTries[i]++; for(int k = 0; k < i - 1; k++) { if(myNumberOfTriesArray[i] == myNumberOfTriesArray[k]) { myNumberOfRepeatedTries[i]++; } } repaint(); this.runSimulation(); try{ Thread.sleep(mySpeed); } catch(Exception e) {} } isSimulating = false; } try{ Thread.sleep(0); } catch(Exception e) {} } } public void paintComponent(Graphics g) { super.paintComponents(g); g.setColor(Color.white); g.drawLine(350, 10, 350, 530); g.drawLine(350, 530, 1090, 530); g.drawString("0", 310, 570); for(int i = 1; i <= 10; i++) { g.drawLine(360 + (i * ((990 - 350) / 10)) - 10, 530, 360 + (i * ((990 - 350) / 10)) - 10, 550); g.drawString(((double)(nValues[myN] / 10) * i) + "" , 360 + (i * ((990 - 350) / 10)) - 20, 570); g.drawLine(330, (i * ((530 - 10) / 10)) - 20, 350, (i * ((530 - 10) / 10)) - 20); g.drawString(((double)(nValues[myN] / 10) * (11 - i)) / nValues[myN] + "" , 310, (i * ((530 - 10) / 10)) - 25); } int count = 0; for(int k = 0; k <= myN / 5; k++) { for(int i = 0; i < 5; i++) { if(isSimulated && count < myN) { if(myCoinArray[count].getState() == Coin.HEADS) { g.setColor(Color.green); g.fillOval(i * 58 + 10, 10 + (k * 58), 48, 48); g.setColor(Color.BLACK); g.drawString("H", i * 58 + 30, (k * 58) + 35); } else { g.setColor(Color.red); g.fillOval(i * 58 + 10, 10 + (k * 58), 48, 48); g.setColor(Color.BLACK); g.drawString("T", i * 58 + 30, (k * 58) + 35); } } count++; } } if(isSimulated) { for(int i = 0; i < myNumberOfRuns; i++) { g.setColor(Color.white); g.drawRect(350 + (myNumberOfTriesArray[i] * 10), 520 - (myNumberOfTriesArray[i] * ( 10 * myNumberOfRepeatedTries[i]) / myNumberOfRuns) * 10, 10, 530 - (520 - (myNumberOfTriesArray[i] * (myNumberOfRepeatedTries[i] / 20) / myNumberOfRuns) * 10)); } } } public void setN(int value) { myN = value; myModel.setNumberOfCoins(myN); } public void setHeadsProbabilty(int value) { myModel.setHeadsProbabilty(value); } public void runSimulation() { myModel.runSimulation(); myCoinArray = myModel.getCoinArray(); isSimulated = true; isSimulating = true; } public void setNumberOfRepitions(int value) { myNumberOfRuns = value; myNumberOfTriesArray = new int[value]; myNumberOfRepeatedTries = new int[value]; } public void setSpeed(long value) { mySpeed = value; } }
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-25-2008, 06:21 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
Your code doesn't compile. There are two classes missing: Model and Coin
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-28-2008, 08:52 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
Code:
import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSlider; public class View extends JFrame implements ActionListener, MouseListener { private JSlider nSlider; private BarGraph myBarGraph; private JLabel nLabel; private JButton enterButton; private JSlider probabiltySlider; private JLabel probabiltyLabel; private JLabel nNameLabel; private JLabel probabiltyNameLabel; private JLabel numberRepitionsNameLabel; private JSlider numberRepitionsSlider; private JLabel numberRepitionsLabel; private JSlider speedSlider; private JLabel speedLabel; private JLabel speedNameLabel; public View() { Container contentPane = getContentPane(); this.setTitle("Odd Man Out"); this.setSize(1050, 700); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setAlwaysOnTop(true); this.setLocationRelativeTo(null); this.setResizable(false); contentPane.setBackground(Color.black); numberRepitionsNameLabel = new JLabel("Number of Repitions"); numberRepitionsNameLabel.setForeground(Color.white); numberRepitionsNameLabel.setBounds(115, 575, 115, 50); contentPane.add( numberRepitionsNameLabel); numberRepitionsSlider = new JSlider(JSlider.HORIZONTAL, 0, 1000, 500); numberRepitionsSlider.setAutoscrolls(true); numberRepitionsSlider.setMajorTickSpacing(1); numberRepitionsSlider.setSnapToTicks(true); numberRepitionsSlider.setBackground(Color.black); numberRepitionsSlider.setForeground(Color.white); numberRepitionsSlider.setBounds(250, 590, 200, 20); numberRepitionsSlider.setToolTipText("NNumber Of Repitions"); numberRepitionsSlider.addMouseListener(this); contentPane.add(numberRepitionsSlider); numberRepitionsLabel = new JLabel("R = " + numberRepitionsSlider.getValue()); numberRepitionsLabel.setForeground(Color.white); numberRepitionsLabel.setBounds(460, 575, 50, 50); contentPane.add(numberRepitionsLabel); speedNameLabel = new JLabel("Speed in MilliSeconds"); speedNameLabel.setForeground(Color.white); speedNameLabel.setBounds(115, 615, 125, 50); contentPane.add(speedNameLabel); speedSlider = new JSlider(JSlider.HORIZONTAL, 0, 1000, 500); speedSlider.setAutoscrolls(true); speedSlider.setMajorTickSpacing(1); speedSlider.setSnapToTicks(true); speedSlider.setBackground(Color.black); speedSlider.setForeground(Color.white); speedSlider.setBounds(250, 630, 200, 20); speedSlider.setToolTipText("Speed"); speedSlider.addMouseListener(this); contentPane.add(speedSlider); speedLabel = new JLabel("S = " + speedSlider.getValue()); speedLabel.setForeground(Color.white); speedLabel.setBounds(460, 615, 50, 50); contentPane.add(speedLabel); nNameLabel = new JLabel("Number of Coins"); nNameLabel.setForeground(Color.white); nNameLabel.setBounds(515, 575, 95, 50); contentPane.add(nNameLabel); nSlider = new JSlider(JSlider.HORIZONTAL, 3, 10, 6); nSlider.setAutoscrolls(true); nSlider.setMajorTickSpacing(1); nSlider.setSnapToTicks(true); nSlider.setBackground(Color.black); nSlider.setForeground(Color.white); nSlider.setBounds(630, 590, 200, 20); nSlider.setToolTipText("Number Of Coins"); nSlider.addMouseListener(this); contentPane.add(nSlider); nLabel = new JLabel("N = " + nSlider.getValue()); nLabel.setForeground(Color.white); nLabel.setBounds(840, 575, 50, 50); contentPane.add(nLabel); probabiltyNameLabel = new JLabel("Probability of Heads"); probabiltyNameLabel.setForeground(Color.white); probabiltyNameLabel.setBounds(515, 615, 115, 50); contentPane.add(probabiltyNameLabel); probabiltySlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50); probabiltySlider.setAutoscrolls(true); probabiltySlider.setMajorTickSpacing(1); probabiltySlider.setSnapToTicks(true); probabiltySlider.setBackground(Color.black); probabiltySlider.setForeground(Color.white); probabiltySlider.setBounds(630, 630, 200, 20); probabiltySlider.setToolTipText("Probabilty Of Heads"); probabiltySlider.addMouseListener(this); contentPane.add(probabiltySlider); probabiltyLabel = new JLabel("p = " + probabiltySlider.getValue()); probabiltyLabel.setForeground(Color.white); probabiltyLabel.setBounds(840, 615, 50, 50); contentPane.add(probabiltyLabel); enterButton = new JButton("Enter"); enterButton.setBounds(900, 590, 90, 70); enterButton.addActionListener(this); contentPane.add(enterButton); myBarGraph = new BarGraph(); contentPane.add(myBarGraph); this.setVisible(true); } @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { if(e.getSource() == nSlider) { nLabel.setText("n = " + nSlider.getValue()); } if(e.getSource() == probabiltySlider) { probabiltyLabel.setText("p = " + probabiltySlider.getValue()); } if(e.getSource() == numberRepitionsSlider) { numberRepitionsLabel.setText("R = " + numberRepitionsSlider.getValue()); } if(e.getSource() == speedSlider) { speedLabel.setText("S = " + speedSlider.getValue()); } } @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == enterButton); { myBarGraph.setN(nSlider.getValue()); myBarGraph.setHeadsProbabilty(probabiltySlider.getValue()); myBarGraph.setNumberOfRepitions(numberRepitionsSlider.getValue()); myBarGraph.setSpeed((long) speedSlider.getValue()); myBarGraph.runSimulation(); } } }
Code:
import java.util.Random; public class Model { private Coin myCoinArray[]; private int myNumberOfCoins; private int myHeadsProbabilty; private int myNumberOfHeads; private int myNumberOfTails; private int myNumberOfTries; private Random rand; public Model() { myNumberOfTries = 1; rand = new Random(); } private void initiateArray() { for(int i = 0; i < myNumberOfCoins; i++) { myCoinArray[i] = new Coin(); } } public void runSimulation() { this.flipCoins(); myNumberOfTries = 1; while(myNumberOfHeads != 1 && myNumberOfTails != 1 ||(myNumberOfHeads == 0 || myNumberOfTails == 0)) { myNumberOfHeads = 0; myNumberOfTails = 0; myNumberOfTries++; this.flipCoins(); } } public void flipCoins() { int temp; for(int i = 0; i < myNumberOfCoins; i++) { temp = rand.nextInt(100); if(temp <= myHeadsProbabilty) { temp = 1; } else { temp = 2; } switch(temp) { case 1: myCoinArray[i].setState(Coin.HEADS); myNumberOfHeads++; break; case 2: myCoinArray[i].setState(Coin.TAILS); myNumberOfTails++; break; default : myCoinArray[i].setState(Coin.TAILS); myNumberOfTails++; break; } } } public String toString() { String temp = ""; for(int i = 0; i < myNumberOfCoins; i++) { temp += myCoinArray[i].getState() + "\n"; } temp += myNumberOfHeads + "\n"; temp += myNumberOfTails + "\n"; temp += myNumberOfTries + "\n"; return temp; } public Coin[] getCoinArray() { return myCoinArray; } public int getNumberOfTries() { return myNumberOfTries; } public void setNumberOfCoins(int n) { myNumberOfCoins = n; myCoinArray = new Coin[myNumberOfCoins]; this.initiateArray(); } public void setHeadsProbabilty(int n) { myHeadsProbabilty = n; } }
Code:
public class Demo { /** * @param args */ public static void main(String[] args) { View myView = new View(); } }
Code:
public class Coin { public static final int HEADS = 0; public static final int TAILS = 1; public static final int NO_STATE = -1; private int myState; public Coin() { myState = NO_STATE; } public int getState() { return myState; } public void setState(int aState) { myState = aState; } }
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java chart & graph lib saturdaybugs Java 2D 1 04-24-2008 03:59 PM
Big Faceless Graph Library 2.3.4 Java Tip Java Announcements 0 04-22-2008 06:35 PM
Need Help for Dot Plot Graph BHCluster Java 2D 5 04-15-2008 04:54 PM
Graph DPS and BFS implementation hey New To Java 1 01-09-2008 11:19 PM
Help with graph code example silvia Java 2D 1 07-30-2007 12:16 AM


All times are GMT +3. The time now is 09:12 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org