Link to home
Start Free TrialLog in
Avatar of urobins
urobins

asked on

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);

      }
}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia 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
also set the preferred size
You need to override paint in DisplayMeter, not paintComponent
Avatar of urobins
urobins

ASKER

what is the diff between paint and paintcomponent?  I have always overridden paintcomponent?
> 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.
Avatar of urobins

ASKER

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?
Avatar of urobins

ASKER

Thanks for the link, looking at it now
>>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
try adding something in the CENTER, that way the space will get handled better.
> 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.
Avatar of urobins

ASKER

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!
Avatar of urobins

ASKER

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;
      }
      
}
>       public class DisplayCoins extends Canvas

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

never call paintComponent() directly

that should be:

                       meter.repaint();
Avatar of urobins

ASKER

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..
Thats because you are using Canvas, see my comment above. You should use JPanel instead
Avatar of urobins

ASKER

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
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
Avatar of urobins

ASKER

so I would just do JPanel.paintComponent(); ??
super.paintComponent(g);
Avatar of urobins

ASKER

and Im sorry where would I put that, in my displayCoins?
at start of paintComponent(), in fact u already have it there commented out
Avatar of urobins

ASKER

OH :)  Thanks.
Avatar of urobins

ASKER

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/
Avatar of urobins

ASKER

actually it leaves what ever the last drawn coin was.
Avatar of urobins

ASKER

Okay got that fixed :)
Avatar of urobins

ASKER

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.
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
Avatar of urobins

ASKER

Oh okay, I'll give that a shot... where do I do that in the main parkingmeter class where I declare the panel?
Avatar of urobins

ASKER

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));
What is the color set for buttonPanel/
Avatar of urobins

ASKER

default color set.  I am going to open a new question for this to be fair, as it deals with a different topic