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.setDefaultLookAndFe elDecorate d(true);
    meterFrame = new JFrame("Parking Meter: Press buttons to enter change");
    meterFrame.setDefaultClose Operation( JFrame.EXI T_ON_CLOSE );
   Â
    //meterFrame.setSize(new Dimension(600, 600));
  Â
    addWidgets();
   Â
    // adding panels to the frame
    meterFrame.getContentPane( ).add(butt onPanel, BorderLayout.PAGE_START);
    meterFrame.getContentPane( ).add(coin s, BorderLayout.LINE_START);
    meterFrame.getContentPane( ).add(mete rPanel,Bor derLayout. LINE_END);
   Â
   Â
    meterFrame.pack();
    meterFrame.setVisible(true );
      }
      public void addWidgets()
      {
           meterPanel = new JPanel();    Â
    buttonPanel = new JPanel(new GridLayout(1,5));
           JButton quarterButton,nickleButton ,dimeButto n,pennyBut ton,clearB utton;
       Â
           //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,SwingConsta nts.HORIZO NTAL);
        //create display objects
        coins = new DisplayCoins();
        meter=new DisplayMeter();
       Â
       Â
        // add buttons and meter to the appropriate panels
        buttonPanel.add(quarterBut ton);
        buttonPanel.add(dimeButton );
        buttonPanel.add(nickleButt on);
        buttonPanel.add(pennyButto n);
        buttonPanel.add(clearButto n);       Â
        meterPanel.add(meter);
        buttonPanel.add(coinTotal) ;
       Â
        //add listeners and actions to the buttons
        quarterButton.addActionLis tener(new ActionListener()
        {
             public void actionPerformed(ActionEven t event)
             {
                 quarterButtonPressed();                Â
                 //meter.repaint();             Â
             }             Â
        }
        );
        dimeButton.addActionListen er(new ActionListener()
        {
             public void actionPerformed(ActionEven t event)
             {
                 dimeButtonPressed();                Â
                 //meter.repaint();             Â
             }             Â
        }
        );
       Â
        nickleButton.addActionList ener(new ActionListener()
        {
             public void actionPerformed(ActionEven t event)
             {
                 nickleButtonPressed();
                 //meter.repaint();             Â
             }             Â
        }
        );
        pennyButton.addActionListe ner(new ActionListener()
        {
             public void actionPerformed(ActionEven t event)
             {
                 pennyButtonPressed();                              Â
             }             Â
        }
        );
       Â
        clearButton.addActionListe ner(new ActionListener()
        {
             public void actionPerformed(ActionEven t 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.substr ing(2, 3));
                 Integer tensVal = Integer.valueOf(amt.substr ing(1, 2));
                 Integer dollarVal = Integer.valueOf(amt.substr ing(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.substr ing(0, 1));
                 Integer centsVal = Integer.valueOf(amt.substr ing(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.substr ing(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","N ine","Ten" ,"Eleven",
                      "Twelve","Thirteen","Fourt een","Fift een","Sixt een","Seve nteen","Ei ghteen","N ineteen"};
                     Â
           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","N ine","Ten" ,"Eleven",
                      "Twelve","Thirteen","Fourt een","Fift een","Sixt een","Seve nteen","Ei ghteen","N ineteen"};
                     Â
           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((Grap hics2D)coi ns.getGrap hics());
      }
     Â
      // Sets the coin type and calls the appropriate paint method
      public void coinSetup(int x)
      {
           coins.SetCoinType(x);
           switch (x)
         {
         case 1:
              coins.paintQuarters((Graph ics2D)coin s.getGraph ics());
              break;
      Â
         case 2:
              coins.paintDimes((Graphics 2D)coins.g etGraphics ());
              break;
             Â
         case 3:
              coins.paintNickels((Graphi cs2D)coins .getGraphi cs());
              break;
             Â
         case 4:
              coins.paintPennies((Graphi cs2D)coins .getGraphi cs());
              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("depo sit= "+ 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("deposi t= "+ 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("deposi t= "+ 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("deposi t= "+ 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(ActionEven t 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(graph ics);
           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.ligh tGray),DIM E("10",55, Color.ligh tGray),
      NICKEL("5",65,Color.lightG ray.darker ()),PENNY( "1",60,Col or.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);
  }
}
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.setDefaultLookAndFe
    meterFrame = new JFrame("Parking Meter: Press buttons to enter change");
    meterFrame.setDefaultClose
   Â
    //meterFrame.setSize(new Dimension(600, 600));
  Â
    addWidgets();
   Â
    // adding panels to the frame
    meterFrame.getContentPane(
    meterFrame.getContentPane(
    meterFrame.getContentPane(
   Â
   Â
    meterFrame.pack();
    meterFrame.setVisible(true
      }
      public void addWidgets()
      {
           meterPanel = new JPanel();    Â
    buttonPanel = new JPanel(new GridLayout(1,5));
           JButton quarterButton,nickleButton
       Â
           //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,SwingConsta
        //create display objects
        coins = new DisplayCoins();
        meter=new DisplayMeter();
       Â
       Â
        // add buttons and meter to the appropriate panels
        buttonPanel.add(quarterBut
        buttonPanel.add(dimeButton
        buttonPanel.add(nickleButt
        buttonPanel.add(pennyButto
        buttonPanel.add(clearButto
        meterPanel.add(meter);
        buttonPanel.add(coinTotal)
       Â
        //add listeners and actions to the buttons
        quarterButton.addActionLis
        {
             public void actionPerformed(ActionEven
             {
                 quarterButtonPressed();                Â
                 //meter.repaint();             Â
             }             Â
        }
        );
        dimeButton.addActionListen
        {
             public void actionPerformed(ActionEven
             {
                 dimeButtonPressed();                Â
                 //meter.repaint();             Â
             }             Â
        }
        );
       Â
        nickleButton.addActionList
        {
             public void actionPerformed(ActionEven
             {
                 nickleButtonPressed();
                 //meter.repaint();             Â
             }             Â
        }
        );
        pennyButton.addActionListe
        {
             public void actionPerformed(ActionEven
             {
                 pennyButtonPressed();                              Â
             }             Â
        }
        );
       Â
        clearButton.addActionListe
        {
             public void actionPerformed(ActionEven
             {
                 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.substr
                 Integer tensVal = Integer.valueOf(amt.substr
                 Integer dollarVal = Integer.valueOf(amt.substr
                 // 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.substr
                 Integer centsVal = Integer.valueOf(amt.substr
                 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.substr
                      results = CENTS[centsVal] + " Cents";
                 }
           }
           // Return the answer
           return results;
      }
     Â
      private void setCoinString(int amount)
      {
          Â
           String[] numbers = {"zero","One","Two","Three
                      "Twelve","Thirteen","Fourt
                     Â
           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
           }
           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
           }
           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
                      "Twelve","Thirteen","Fourt
                     Â
           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((Grap
      }
     Â
      // Sets the coin type and calls the appropriate paint method
      public void coinSetup(int x)
      {
           coins.SetCoinType(x);
           switch (x)
         {
         case 1:
              coins.paintQuarters((Graph
              break;
      Â
         case 2:
              coins.paintDimes((Graphics
              break;
             Â
         case 3:
              coins.paintNickels((Graphi
              break;
             Â
         case 4:
              coins.paintPennies((Graphi
              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("depo
        System.out.println("String
      }
     Â
// Â Â Â Â Â Â 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("deposi
        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("deposi
        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("deposi
        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(ActionEven
           // 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,
          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(graph
           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.ligh
      NICKEL("5",65,Color.lightG
     Â
      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);
  }
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks I'll give that a try
which one did you use
ASKER
I ended up putting both coins and meter to the center and they displayed side by side
Cool :)
Try that as CENTER