Link to home
Start Free TrialLog in
Avatar of urobins
urobins

asked on

displaying frame in the right order.

I am still having problems getting my window/frame to display the way I want it to.  I would like the buttons on the top.  the coins to the left and the meter to the right but its lining up stacked one on top of the other

can anyone see what might be wrong

package project3;

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

import javax.swing.*;

public class ParkingMeter implements ActionListener
{
      JFrame meterFrame;
    JPanel meterPanel, buttonPanel;
    JLabel meterTime, coinTotal;
    DisplayCoins coins;
    DisplayMeter meter;
    int total=0;
    int time =0;
    int angle =0;
    String deposit,timeLeft, strTotal;
   
      public void ParkingMeter()
      {
                        
      }
      
      public void setWindow()
      {                     
          //Setting up the Frame
            JFrame.setDefaultLookAndFeelDecorated(true);
        meterFrame = new JFrame("Parking Meter: Press buttons to enter change");
        meterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        //meterFrame.setSize(new Dimension(600, 600));
     
        addWidgets();
       
        // adding panels to the frame
        meterFrame.getContentPane().add(buttonPanel, BorderLayout.PAGE_START);
        meterFrame.getContentPane().add(coins, BorderLayout.LINE_START);
        meterFrame.getContentPane().add(meterPanel,BorderLayout.LINE_END);
       
       
        meterFrame.pack();
        meterFrame.setVisible(true);
      }
      public void addWidgets()
      {
            meterPanel = new JPanel();        
        buttonPanel = new JPanel(new GridLayout(1,5));
            JButton quarterButton,nickleButton,dimeButton,pennyButton,clearButton;
          
            //create the buttons
          quarterButton = new JButton ("25");         
          dimeButton = new JButton("10");
          nickleButton = new JButton("5");
          pennyButton = new JButton("1");
          clearButton = new JButton("Clear");
          meterTime = new JLabel();
          coinTotal = new JLabel(deposit,SwingConstants.HORIZONTAL);
          //create display objects
          coins = new DisplayCoins();
          meter=new DisplayMeter();
          
          
          // add buttons and meter to the appropriate panels
          buttonPanel.add(quarterButton);
          buttonPanel.add(dimeButton);
          buttonPanel.add(nickleButton);
          buttonPanel.add(pennyButton);
          buttonPanel.add(clearButton);         
          meterPanel.add(meter);
          buttonPanel.add(coinTotal);
          
          //add listeners and actions to the buttons
          quarterButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       quarterButtonPressed();                      
                       //meter.repaint();                
                }                
          }
          );

          dimeButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       dimeButtonPressed();                      
                       //meter.repaint();                
                }                
          }
          );
          
          nickleButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       nickleButtonPressed();
                       //meter.repaint();                
                }                
          }
          );
          pennyButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       pennyButtonPressed();                                        
                }                
          }
          );
          
          clearButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       clearButtonPressed();                      
                }                
          }
          );
          
      }
      
      /*
       * coinString is passed a String amount and returns a String of words that
       * describe the amount
       */
      public String coinString(String amt) {
            // Create the Word Arrays for the Amounts
            String[] CENTS = { "", "One", "Two", "Three", "Four", "Five", "Six",
                        "Seven", "Eight", "Nine" };
            String[] TEEN = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
                        "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
            String[] TEN = { "", "", "Twenty", "Thirty", "Fourty", "Fifty",
                        "Sixty", "Seventy", "Eighty", "Ninety" };

            // Create a result to return
            String results = " Zero Cents";

            // And save the length of the amount
            int len = amt.length();

            // The maximm we handle is 3 characters
            if (len == 3) {
                  // Pull out each character as an integer
                  Integer centsVal = Integer.valueOf(amt.substring(2, 3));
                  Integer tensVal = Integer.valueOf(amt.substring(1, 2));
                  Integer dollarVal = Integer.valueOf(amt.substring(0, 1));
                  // use the integer to index into the words and concatenate them
                  results = CENTS[dollarVal] + " Dollar " + TEN[tensVal] + ""
                              + CENTS[centsVal] + " Cents";
            }
            // length of two has special cases where 10, 11, 12, and TEENS are
            // differant then
            // the rest of the numbers
            else if (len == 2) {
                  Integer tensVal = Integer.valueOf(amt.substring(0, 1));
                  Integer centsVal = Integer.valueOf(amt.substring(1, 2));
                  if (tensVal == 1) {
                        results = TEEN[centsVal] + " Cents";
                  } else {
                        results = TEN[tensVal] + " " + CENTS[centsVal] + " Cents";
                  }
            }
            // Single didgit numbers are simple lookups in
            // the CENTS array
            else if (len == 1) {
                  if (amt.equals("0")) {
                        results = " Zero Cents";
                  } else {
                        Integer centsVal = Integer.valueOf(amt.substring(0, 1));
                        results = CENTS[centsVal] + " Cents";
                  }
            }
            // Return the answer
            return results;
      }
      
      private void setCoinString(int amount)
      {
            
            String[] numbers = {"zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven",
                        "Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
                        
            if (amount > 1 && amount <20)
            {
                  deposit = numbers[amount];            
            }
            else if (amount >19 && amount <30 )
            {                                    
                  deposit = "Twenty "+numbers[amount-20]+" Cents";                  
            }
            else if (amount >29 && amount <40 )
            {                                    
                  deposit = "Thirty "+numbers[amount-30]+" Cents";                  
            }
            else if (amount >39 && amount <50 )
            {                                    
                  deposit = "Fourty "+numbers[amount-40]+" Cents";                  
            }
            else if (amount >49 && amount <60 )
            {                                    
                  deposit = "Fifty "+numbers[amount-50]+" Cents";                  
            }
            else if (amount >59 && amount <70 )
            {                                    
                  deposit = "Sixty "+numbers[amount-60]+" Cents";                  
            }
            else if (amount >69 && amount <80 )
            {                                    
                  deposit = "Seventy "+numbers[amount-70]+" Cents";                        
            }
            else if (amount >79 && amount <90 )
            {                                    
                  deposit = "Eighty "+numbers[amount-80]+" Cents";                  
            }
            else if (amount >89 && amount <100 )
            {                                    
                  deposit = "Ninety "+numbers[amount-90]+" Cents";                  
            }
            else if (amount >99 && amount <120 )
            {                                    
                  deposit = "One Dollar "+numbers[amount-100]+" Cents";                  
            }
            else if (amount >119 && amount <130 )
            {                                    
                  deposit = "One Dollar and twenty "+numbers[amount-120]+" Cents";                  
            }
            else if (amount >129 && amount <140 )
            {                                    
                  deposit = "One Dollar and thirty "+numbers[amount-130]+" Cents";                  
            }
            else if (amount >139 && amount <150 )
            {                                    
                  deposit = "One Dollar and fourty "+numbers[amount-140]+" Cents";                  
            }
            else if (amount >149 && amount <160 )
            {                                    
                  deposit = "One Dollar and fifty "+numbers[amount-150]+" Cents";                  
            }
            else if (amount >159 && amount <170 )
            {                                    
                  deposit = "One Dollar and sixty "+numbers[amount-160]+" Cents";                  
            }
            else if (amount >169 && amount <180 )
            {                                    
                  deposit = "One Dollar and seventy "+numbers[amount-170]+" Cents";                  
            }
            else if (amount >179 && amount <190 )
            {                                    
                  deposit = "One Dollar  and eighty"+numbers[amount-180]+" Cents";                  
            }
            else if (amount >189 && amount <200 )
            {                                    
                  deposit = "One Dollar and ninety "+numbers[amount-190]+" Cents";                  
            }
            else if (amount >199 && amount <220 )
            {                                    
                  deposit = "Two Dollars and "+numbers[amount-200]+" Cents";                  
            }
            else if (amount >219 && amount <230 )
            {                                    
                  deposit = "Two Dollars and twenty "+numbers[amount-220]+" Cents";                  
            }
            else if (amount >229 && amount <240 )
            {                                    
                  deposit = "OTwo Dollars and thirty "+numbers[amount-230]+" Cents";                  
            }
            else if (amount >239 && amount <250 )
            {                                    
                  deposit = "Two Dollars and fourty "+numbers[amount-240]+" Cents";                  
            }
            else if (amount >249 && amount <260 )
            {                                    
                  deposit = "Two Dollars and fifty "+numbers[amount-250]+" Cents";                  
            }
            else if (amount >259 && amount <270 )
            {                                    
                  deposit = "Two Dollars and sixty "+numbers[amount-260]+" Cents";                  
            }
            else if (amount >269 && amount <280 )
            {                                    
                  deposit = "Two Dollars and seventy "+numbers[amount-270]+" Cents";                  
            }
            else if (amount >279 && amount <290 )
            {                                    
                  deposit = "Two Dollars and eighty"+numbers[amount-280]+" Cents";                  
            }
            else if (amount >289 && amount <300 )
            {                                    
                  deposit = "Two Dollars and ninety "+numbers[amount-290]+" Cents";                  
            }
            else if (amount >299 && amount <320 )
            {                                    
                  deposit = "Three Dollars and "+numbers[amount-300]+" Cents";                  
            }
            else if (amount >319 && amount <330 )
            {                                    
                  deposit = "Three Dollars and twenty "+numbers[amount-320]+" Cents";                  
            }
            else if (amount >329 && amount <340 )
            {                                    
                  deposit = "Three Dollars and thirty "+numbers[amount-330]+" Cents";                  
            }
            else if (amount >339 && amount <350 )
            {                                    
                  deposit = "Three Dollars and fourty "+numbers[amount-340]+" Cents";                  
            }
            else if (amount >349 && amount <360 )
            {                                    
                  deposit = "Three Dollars and fifty "+numbers[amount-350]+" Cents";                  
            }
            else if (amount >359 && amount <370 )
            {                                    
                  deposit = "Three Dollars and sixty "+numbers[amount-360]+" Cents";                  
            }
            else if (amount >369 && amount <380 )
            {                                    
                  deposit = "Three Dollars and seventy "+numbers[amount-370]+" Cents";                  
            }
            else if (amount >379 && amount <390 )
            {                                    
                  deposit = "Three Dollars and eighty "+numbers[amount-380]+" Cents";                  
            }
            else if (amount >389 && amount <400 )
            {                                    
                  deposit = "Three Dollars and ninety "+numbers[amount-390]+" Cents";                  
            }
            else if (amount >399 && amount <420 )
            {                                    
                  deposit = "Four Dollars and "+numbers[amount-400]+" Cents";                  
            }
            else if (amount >419 && amount <430 )
            {                                    
                  deposit = "Four Dollars and twenty "+numbers[amount-420]+" Cents";                  
            }
            else if (amount >429 && amount <440 )
            {                                    
                  deposit = "Four Dollars and thirty "+numbers[amount-430]+" Cents";                  
            }
            else if (amount >439 && amount <450 )
            {                                    
                  deposit = "Four Dollars and fourty "+numbers[amount-440]+" Cents";                  
            }
            else if (amount >449 && amount <460 )
            {                                    
                  deposit = "Four Dollars and fifty "+numbers[amount-450]+" Cents";                  
            }
            else if (amount >459 && amount <470 )
            {                                    
                  deposit = "Four Dollars and sixty "+numbers[amount-460]+" Cents";                  
            }
            else if (amount >469 && amount <480 )
            {                                    
                  deposit = "Four Dollars and seventy "+numbers[amount-470]+" Cents";                  
            }
            else if (amount >479 && amount <490 )
            {                                    
                  deposit = "Four Dollars and eighty "+numbers[amount-480]+" Cents";                  
            }
            else if (amount >489 && amount <500 )
            {                                    
                  deposit = "Four Dollars and ninety "+numbers[amount-490]+" Cents";                  
            }
            else if (amount >499 && amount <520 )
            {                                    
                  deposit = "Five Dollars and "+numbers[amount-500]+" Cents";                  
            }
            else if (amount >519 && amount <530 )
            {                                    
                  deposit = "Five Dollars and twenty "+numbers[amount-520]+" Cents";                  
            }
            else if (amount >529 && amount <540 )
            {                                    
                  deposit = "Five Dollars and thirty "+numbers[amount-530]+" Cents";                  
            }
            else if (amount >539 && amount <550 )
            {                                    
                  deposit = "Five Dollars and fourty "+numbers[amount-540]+" Cents";                  
            }
            else if (amount >549 && amount <560 )
            {                                    
                  deposit = "Five Dollars and fifty "+numbers[amount-550]+" Cents";                  
            }
            else if (amount >559 && amount <570 )
            {                                    
                  deposit = "Five Dollars and sixty "+numbers[amount-560]+" Cents";                  
            }
            else if (amount >569 && amount <580 )
            {                                    
                  deposit = "Five Dollars and sevety "+numbers[amount-570]+" Cents";                  
            }
            else if (amount >579 && amount <590 )
            {                                    
                  deposit = "Five Dollars and eighty "+numbers[amount-580]+" Cents";                  
            }
            else if (amount >589 && amount <600 )
            {                                    
                  deposit = "Five Dollars and ninety "+numbers[amount-590]+" Cents";                  
            }
            else if (amount >599 && amount <601)
            {
                  deposit = "Six Dollars ";
            }
            else if (amount > 600)
            {
                  deposit = "You have reached your maximum time and deposit of 6 dollars";
            }
      }

      private void setTimeString(int amount)
      {
            
            String[] numbers = {"zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven",
                        "Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
                        
            if (amount > 1 && amount <20)
            {
                  timeLeft = numbers[amount];            
            }
            else if (amount >19 && amount <30 )
            {                                    
                  timeLeft = "Twenty "+numbers[amount-20]+" minutes";                  
            }
            else if (amount >29 && amount <40 )
            {                                    
                  timeLeft = "Thirty "+numbers[amount-30]+" minutes";                  
            }
            else if (amount >39 && amount <50 )
            {                                    
                  timeLeft = "Fourty "+numbers[amount-40]+" minutes";                  
            }
            else if (amount >49 && amount <60 )
            {                                    
                  timeLeft = "Fifty "+numbers[amount-50]+" minutes";                  
            }
            else if (amount >59 && amount <70 )
            {                                    
                  timeLeft = "one hour and "+numbers[amount-60]+" minutes";                  
            }
            else if (amount >69 && amount <90 )
            {                                    
                  timeLeft = "One hour and twenty "+numbers[amount-70]+" minutes";                        
            }
            else if (amount >89 && amount <100 )
            {                                    
                  timeLeft = "One hour and thirty "+numbers[amount-90]+" minutes";                  
            }
            else if (amount >99 && amount <110 )
            {                                    
                  timeLeft = "One hour and fourty "+numbers[amount-100]+" minutes";                  
            }
            else if (amount >109 && amount < 120)
            {
                  timeLeft = "One hour and fifty " +numbers[amount-110]+" minutes";
            }
            else if (amount >119 && amount < 121)
            {
                  timeLeft = "Two hours ";
            }
            else if (amount >120)
            {
                  timeLeft = "You have reached the maximum time of two hours";
            }
      }
      
      //Method to draw and display the meter
      public void drawMeter (int angle)
      {
            meter.setAngle(angle);
          meter.paintComponent((Graphics2D)coins.getGraphics());
      }
      
      // Sets the coin type and calls the appropriate paint method
      public void coinSetup(int x)
      {
            coins.SetCoinType(x);
            switch (x)
            {
            case 1:
                  coins.paintQuarters((Graphics2D)coins.getGraphics());
                  break;
        
            case 2:
                  coins.paintDimes((Graphics2D)coins.getGraphics());
                  break;
                 
            case 3:
                  coins.paintNickels((Graphics2D)coins.getGraphics());
                  break;
                 
            case 4:
                  coins.paintPennies((Graphics2D)coins.getGraphics());
                  break;
            }
      }
      
      // actions to be taken when the quarter button is pressed
      public void quarterButtonPressed()
      {      
            total = total +25;
          time = time +5;
          angle = (int) ((25 / 600.0) * 180);
          
          System.out.println(total);
          drawMeter(angle);
          coinSetup(1);
            setCoinString(total);
            strTotal= Integer.toString(total);
            deposit = coinString(strTotal);
            setTimeString(time);
            System.out.println("time= "+ timeLeft);
          //System.out.println("deposit= "+ deposit);
          System.out.println("String deposit= "+ deposit);

      }
      
//       actions to be taken when the dime button is pressed
      public void dimeButtonPressed()
      {
            total = total +10;
          time= time +2;
          angle = (int) ((10 / 600.0) * 180);
          
          System.out.println(total);
          drawMeter(angle);
          coinSetup(2);
            setCoinString(total);
          System.out.println("deposit= "+ deposit);
          setTimeString(time);
            System.out.println("time= "+ timeLeft);

      }
      
//       actions to be taken when the nickle button is pressed
      public void nickleButtonPressed()
      {
          total = total +5;
          time = time+1;
          angle = (int) ((5 / 600.0) * 180);
          
          System.out.println(total);
          drawMeter(angle);
          coinSetup(3);            
            setCoinString(total);
          System.out.println("deposit= "+ deposit);
          setTimeString(time);
            System.out.println("time= "+ timeLeft);
            //coins.repaint();
      }
      
//       actions to be taken when the penny button is pressed
      public void pennyButtonPressed()
      {
            total = total +1;
            
          System.out.println(total);
          coinSetup(4);            
            setCoinString(total);
          System.out.println("deposit= "+ deposit);
          setTimeString(time);
            System.out.println("time= "+ timeLeft);

      }
      
//       actions to be taken when the clear button is pressed
      public void clearButtonPressed()
      {
            coins.SetCoinType(0);
            coins.setY();
            coins.repaint();
            meter.zeroAngle();
            total=0;
            time=0;
            angle =0;
      }
      
      public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            
      }
      // Helper class to extend jpanel and fill windows
      public class DisplayCoins extends JPanel
        {
            int quarterY=30;
            int nickelY=30;
            int pennyY=30;
            int dimeY=30;
          private static final long serialVersionUID = 1L;
          
          /*
           * using coin type to determine which coin to paint
           * 1 = quarter
           * 2 = dime
           * 3 = nickle
           * 4 = penny
           */
          int coinType =0;
          
          public DisplayCoins()
          {
            super() ;
            this.setPreferredSize( new Dimension( 400,300) ) ;           
          }
          
          public void SetCoinType (int x)
          {
                coinType = x;                
          }
          
          public void setY()
          {
                quarterY=30;
                  nickelY=30;
                  pennyY=30;
                  dimeY=30;
          }
          
          public void paintComponent( Graphics graphics)
          {
            super.paintComponent( graphics ) ;
            Graphics2D g = (Graphics2D)graphics ;
            
            switch (coinType)
            {
            case 1:
                  paintQuarters(g) ;
                  break;
        
            case 2:
                  paintDimes( g) ;
                  break;
                 
            case 3:
                  paintNickels( g) ;
                  break;
                 
            case 4:
                  paintPennies( g) ;
                  break;      
                 
            }// end switch           
          } //end paint component
                   
          // methods to paint the coins to the screen using the coin Enumerated Type
          private void paintQuarters( Graphics2D graphics)
          {
                
                int x = 30;
            
              Coins c = Coins.QUARTER;
              c.paint(graphics,quarterY,x);
              quarterY=quarterY+10;                        
          }

        
          private void paintDimes( Graphics2D graphics)
          {
                
                int x=130 ;
            
              Coins c = Coins.DIME;
              c.paint( graphics, dimeY , x ) ;
              dimeY=dimeY+10;           
          }

          private void paintNickels( Graphics2D graphics)
          {
                
                int x=205;
            
              Coins c = Coins.NICKEL;
              c.paint(graphics,nickelY,x);
              nickelY=nickelY+10;           
          }

          private void paintPennies( Graphics2D graphics)
          {
                
                int x= 290;
            
              Coins c = Coins.PENNY;
              c.paint(graphics,pennyY,x);
              pennyY=pennyY+10;           
          }
                   
          
        }// end disply graphics

      


      

}//end parking meter
-----------------------------------------------------------------
package project3;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class DisplayMeter extends JPanel
{
      int startAngle ;
    int arcAngle ;
      public void DisplayMeter()
      {
            this.setPreferredSize( new Dimension( 400, 300 ) ) ;
            startAngle =0;
            arcAngle =0;
      }
      public void paintComponent(Graphics graphics)
      {
            Graphics2D g = (Graphics2D)graphics ;
            super.paintComponent(graphics);
            int startAngle = 180;
        //int arcAngle = -180;
        g.setStroke(new BasicStroke(8.0f));
        g.setColor(Color.black);
        g.drawArc(10, 200, 300, 200, startAngle, arcAngle);
        g.setColor(Color.red);
        g.fillArc(10, 200, 300, 200, startAngle, arcAngle);

      }
      public void setAngle(int amount)
      {
            if (amount ==0)
            {
                  arcAngle= 0;
            }
            else
                  arcAngle= arcAngle-amount;
            
      }
      public void zeroAngle()
      {
            arcAngle=0;
      }
      
}
----------------------------------------------------------------------
package project3;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;

public enum Coins {

      QUARTER("25",75,Color.lightGray),DIME("10",55,Color.lightGray),
      NICKEL("5",65,Color.lightGray.darker()),PENNY("1",60,Color.orange.darker());
      
      private Color color;
      private String value;
      private int size;
      
      private Coins (String value, int size,Color color)
      {
            this.color = color;
            this.value = value;
            this.size = size;
      }
      public int getSize()
      {
            return this.size;
      }
      
      public String getValue()
      {
            return this.value;
      }
      
      public Color getColor()
      {
            return this.color;
      }
      //paints the coins
      public void paint(Graphics2D g, int y, int x)
    {
            g.setStroke(new BasicStroke(6.0f));
        g.setColor(Color.black);
        g.drawOval(x, y, size, size);
        g.setColor(color);
        g.fillOval(x, y, size-2, size-2);
        g.setColor(Color.black);
        g.drawString(this.value, x+(this.getSize()/2)-8, y+(this.getSize()/2)+2);
   }
}
Avatar of Mayank S
Mayank S
Flag of India image

>> meterFrame.getContentPane().add(meterPanel,BorderLayout.LINE_END);

Try that as CENTER
Avatar of urobins
urobins

ASKER

I have... i have tried just about everything I can think of center north south in all combinatoins and it always displays the same way
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of urobins

ASKER

Thanks I'll give that a try
which one did you use
Avatar of urobins

ASKER

I ended up putting both coins and meter to the center and they displayed side by side
Cool :)