I am not getting an arc displayed

Can someone tell me why my meter is not displaying?  I am getting the buttons and the coins but the arc is not displaying....

package project3;

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

import javax.swing.*;

public class ParkingMeter implements ActionListener
{
      JFrame meterFrame;
    JPanel meterPanel;
    DisplayCoins coins;
    DisplayMeter meter;
    int total=0;
    int time =0;
   
      public void ParkingMeter()
      {
                        
      }
      
      public void setWindow()
      {                     
          //Setting up the Frame
        meterFrame = new JFrame("Parking Meter: Press buttons to enter change");
        meterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        meterFrame.setSize(new Dimension(600, 400));
       
        // Create the Meter panel
        meterPanel = new JPanel(new GridLayout(1, 6));
       
        addWidgets();
       
        meterFrame.getContentPane().add(meterPanel, BorderLayout.NORTH);
        meterFrame.getContentPane().add(coins, BorderLayout.SOUTH);
        meterFrame.getContentPane().add(meter,BorderLayout.EAST);
       
       
        meterFrame.pack();
        meterFrame.setVisible(true);
      }
      public void addWidgets()
      {
            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");
          
          //create display graphics object
          coins = new DisplayCoins();
          meter=new DisplayMeter();
          
          
          meterPanel.add(quarterButton);
          meterPanel.add(dimeButton);
          meterPanel.add(nickleButton);
          meterPanel.add(pennyButton);
          meterPanel.add(clearButton);
          
          //add listeners and actions to the buttons
          quarterButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       quarterButtonPressed();
                       total = total +25;
                       time = time +5;
                       System.out.println(total);
                 
                }                
          }
          );

          dimeButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       dimeButtonPressed();
                       total = total +10;
                       time= time +2;
                       System.out.println(total);
                 
                }                
          }
          );
          
          nickleButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       nickleButtonPressed();
                       total = total +5;
                       time = time+1;
                       System.out.println(total);
                 
                }                
          }
          );
          pennyButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       pennyButtonPressed();
                       total = total +1;
                       System.out.println(total);
                 
                }                
          }
          );
          
          clearButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       clearButtonPressed();
                       total=0;
                }                
          }
          );
      }
      
      public void quarterButtonPressed()
      {            
            coins.SetCoinType(1);
            coins.paintQuarters((Graphics2D)coins.getGraphics());
            //coins.repaint();
      }
      
      public void dimeButtonPressed()
      {
            coins.SetCoinType(2);
            coins.paintDimes((Graphics2D)coins.getGraphics());
            //coins.repaint();
      }
      
      public void nickleButtonPressed()
      {
            coins.SetCoinType(3);
            coins.paintNickels((Graphics2D)coins.getGraphics());
            //coins.repaint();
      }
      
      public void pennyButtonPressed()
      {
            coins.SetCoinType(4);
            coins.paintPennies((Graphics2D)coins.getGraphics());
            //coins.repaint();
      }
      
      public void clearButtonPressed()
      {
            coins.setY();
            coins.repaint();
      }
      
      // Helper class to extend jpanel and fill windows
      public class DisplayCoins extends Canvas
        {
            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
          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

      public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            
      }


      

}//end parking meter
--------------------------------------------

package project3;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class DisplayMeter extends Canvas
{

      public void DisplayMeter()
      {
            int startAngle = 0;
        int arcAngle = -60;
      }
      public void paintComponent(Graphics graphics)
      {
            Graphics2D g = (Graphics2D)graphics ;
            
            int startAngle = 0;
        int arcAngle = -60;
        g.setStroke(new BasicStroke(6.0f));
        g.setColor(Color.black);
        g.drawArc(50, 50, 300, 200, startAngle, arcAngle);
        g.fillArc(50, 50, 300, 200, startAngle, arcAngle);

      }
}
urobinsAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
> public class DisplayMeter extends Canvas

change Canvas to JPanel
0
 
objectsCommented:
also set the preferred size
0
 
CEHJCommented:
You need to override paint in DisplayMeter, not paintComponent
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
urobinsAuthor Commented:
what is the diff between paint and paintcomponent?  I have always overridden paintcomponent?
0
 
objectsCommented:
> what is the diff between paint and paintcomponent?  I have always overridden paintcomponent?

you should rarely need to override paint. paintComponent() is what u should be overriding.
Your problem is you're extending an AWT class instead of Swing.
0
 
objectsCommented:
0
 
urobinsAuthor Commented:
Objects I got it to display but it is coming up in the bottom of the frame I need it to come out on the left.  I tried using BordeLayout.EAST and it still comes up on the bottom... any ideas?
0
 
urobinsAuthor Commented:
Thanks for the link, looking at it now
0
 
CEHJCommented:
>>what is the diff between paint and paintcomponent?  I have always overridden paintcomponent?

You override paint in AWT and in some cases in Swing. Using a Canvas is fine. Using a JPanel is unnecessarily weighty for what you're doing with it
0
 
objectsCommented:
try adding something in the CENTER, that way the space will get handled better.
0
 
objectsCommented:
> Using a JPanel is unnecessarily weighty for what you're doing with it

Ignore that statement, its complete rubbish.
JPanel is about the lightest component avaialbel to you and a lot lighter than a Canvas.
0
 
urobinsAuthor Commented:
for my displaycoins I had to use a canvas otherwise the buttons didn't work out right so I just assumed the same thing with displaymeter but I set that to jpanel and it seems to work fine.  I will try adding it to the center thanks!
0
 
urobinsAuthor Commented:
I tried putting the coins center and iM getting the same thing.

here is my code as its been altered

package project3;

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

import javax.swing.*;

public class ParkingMeter implements ActionListener
{
      JFrame meterFrame;
    JPanel meterPanel;
    DisplayCoins coins;
    DisplayMeter meter;
    int total=0;
    int time =0;
   
      public void ParkingMeter()
      {
                        
      }
      
      public void setWindow()
      {                     
          //Setting up the Frame
        meterFrame = new JFrame("Parking Meter: Press buttons to enter change");
        meterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //meterFrame.setLayout(new BorderLayout(5,5));
        meterFrame.setSize(new Dimension(600, 400));
       
        // Create the Meter panel
        meterPanel = new JPanel(new GridLayout(1, 6));
       
        addWidgets();
       
        meterFrame.getContentPane().add(meterPanel, BorderLayout.NORTH);
        meterFrame.getContentPane().add(coins, BorderLayout.CENTER);
        meterFrame.getContentPane().add(meter,BorderLayout.EAST);
       
       
        meterFrame.pack();
        meterFrame.setVisible(true);
      }
      public void addWidgets()
      {
            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");
          
          //create display graphics object
          coins = new DisplayCoins();
          meter=new DisplayMeter();
          
          
          meterPanel.add(quarterButton);
          meterPanel.add(dimeButton);
          meterPanel.add(nickleButton);
          meterPanel.add(pennyButton);
          meterPanel.add(clearButton);         
          //meterPanel.add(meter,BorderLayout.EAST);
          
          //add listeners and actions to the buttons
          quarterButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       quarterButtonPressed();
                       total = total +25;
                       time = time +5;
                       System.out.println(total);
                       meter.setAngle(time);
                       meter.paintComponent((Graphics2D)coins.getGraphics());
                 
                }                
          }
          );

          dimeButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       dimeButtonPressed();
                       total = total +10;
                       time= time +2;
                       System.out.println(total);
                       meter.setAngle(time);
                       meter.paintComponent((Graphics2D)coins.getGraphics());
                 
                }                
          }
          );
          
          nickleButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       nickleButtonPressed();
                       total = total +5;
                       time = time+1;
                       System.out.println(total);
                       meter.setAngle(time);
                       meter.paintComponent((Graphics2D)coins.getGraphics());
                 
                }                
          }
          );
          pennyButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       pennyButtonPressed();
                       total = total +1;
                       System.out.println(total);
                 
                }                
          }
          );
          
          clearButton.addActionListener(new ActionListener()
          {
                public void actionPerformed(ActionEvent event)
                {
                       clearButtonPressed();
                       total=0;
                }                
          }
          );
      }
      
      public void quarterButtonPressed()
      {            
            coins.SetCoinType(1);
            coins.paintQuarters((Graphics2D)coins.getGraphics());
            //coins.repaint();
      }
      
      public void dimeButtonPressed()
      {
            coins.SetCoinType(2);
            coins.paintDimes((Graphics2D)coins.getGraphics());
            //coins.repaint();
      }
      
      public void nickleButtonPressed()
      {
            coins.SetCoinType(3);
            coins.paintNickels((Graphics2D)coins.getGraphics());
            //coins.repaint();
      }
      
      public void pennyButtonPressed()
      {
            coins.SetCoinType(4);
            coins.paintPennies((Graphics2D)coins.getGraphics());
            //coins.repaint();
      }
      
      public void clearButtonPressed()
      {
            coins.setY();
            coins.repaint();
            meter.zeroAngle();
            total=0;
            time=0;
      }
      
      // Helper class to extend jpanel and fill windows
      public class DisplayCoins extends Canvas
        {
            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
          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

      public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            
      }


      

}//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 ;
            
            int startAngle = 180;
        //int arcAngle = -180;
        g.setStroke(new BasicStroke(6.0f));
        g.setColor(Color.black);
        g.drawArc(10, 200, 300, 200, startAngle, arcAngle);
        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;
      }
      
}
0
 
objectsCommented:
>       public class DisplayCoins extends Canvas

also change that to JPanel
0
 
objectsCommented:
>                        meter.paintComponent((Graphics2D)coins.getGraphics());

never call paintComponent() directly

that should be:

                       meter.repaint();
0
 
urobinsAuthor Commented:
If I use repaint it doesn't draw the mete in increments in fact it doesn't show up at all.... if I call paint components it does what it should?? is there some othe way I shoul be setting this up?

The idea is that for every coin you deposit the meter fills a certain percentage..
0
 
objectsCommented:
Thats because you are using Canvas, see my comment above. You should use JPanel instead
0
 
urobinsAuthor Commented:
yeah when I used JPanel it would repeat the button row when I hit clear for some reason???  When I went to Canvas it fixed that problem.  I am prob'ly creating new problems everytime I think I fixed one
0
 
objectsCommented:
thats because you don't clear your background
and clear calls setY()

either paint the panel background (fillRect()) or call JPanel's paintCompoent to handle it
0
 
urobinsAuthor Commented:
so I would just do JPanel.paintComponent(); ??
0
 
objectsCommented:
super.paintComponent(g);
0
 
urobinsAuthor Commented:
and Im sorry where would I put that, in my displayCoins?
0
 
objectsCommented:
at start of paintComponent(), in fact u already have it there commented out
0
 
urobinsAuthor Commented:
OH :)  Thanks.
0
 
urobinsAuthor Commented:
I changed it back to JPanel and uncommented that line now when I hit clear it clears everything but 1 dime?  what could be causing that/
0
 
urobinsAuthor Commented:
actually it leaves what ever the last drawn coin was.
0
 
urobinsAuthor Commented:
Okay got that fixed :)
0
 
urobinsAuthor Commented:
but I still can't get the meter to go to the right of the frame.  I tried creating a new panel and that didn't help either.
0
 
objectsCommented:
where exactly is it showing it.
It may actuall be to the right, but showing the meter  down the bottom
set the background colour of the panel to get an idea exactlky where the panel is
0
 
urobinsAuthor Commented:
Oh okay, I'll give that a shot... where do I do that in the main parkingmeter class where I declare the panel?
0
 
urobinsAuthor Commented:
I put it here and it still shows gray

 meterPanel = new JPanel(new GridLayout(1, 1));
        meterPanel.setBackground(Color.blue);
        buttonPanel = new JPanel(new GridLayout(1,5));
0
 
Mayank SAssociate Director - Product EngineeringCommented:
What is the color set for buttonPanel/
0
 
urobinsAuthor Commented:
default color set.  I am going to open a new question for this to be fair, as it deals with a different topic
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.