Results 1 to 3 of 3

Thread: Bar Graph

  1. #1
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default 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.
    Java 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

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    6

    Default

    Your code doesn't compile. There are two classes missing: Model and Coin
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  3. #3
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default

    Java 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();
            } 
        }
    }
    Java 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;
        }
    }
    Java Code:
    public class Demo
    {
    
        /**
         * @param args
         */
        public static void main(String[] args)
        {
           View myView = new View();        
        }
    
    }
    Java 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

Similar Threads

  1. Java chart & graph lib
    By saturdaybugs in forum Java 2D
    Replies: 1
    Last Post: 04-24-2008, 01:59 PM
  2. Big Faceless Graph Library 2.3.4
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-22-2008, 04:35 PM
  3. Need Help for Dot Plot Graph
    By BHCluster in forum Java 2D
    Replies: 5
    Last Post: 04-15-2008, 02:54 PM
  4. Graph DPS and BFS implementation
    By hey in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 09:19 PM
  5. Help with graph code example
    By silvia in forum Java 2D
    Replies: 1
    Last Post: 07-29-2007, 10:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •