Solved

I am not getting an arc displayed

Posted on 2007-03-22
33
195 Views
Last Modified: 2010-03-31
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);

      }
}
0
Comment
Question by:urobins
  • 17
  • 13
  • 2
  • +1
33 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
> public class DisplayMeter extends Canvas

change Canvas to JPanel
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
also set the preferred size
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to override paint in DisplayMeter, not paintComponent
0
 

Author Comment

by:urobins
Comment Utility
what is the diff between paint and paintcomponent?  I have always overridden paintcomponent?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 

Author Comment

by:urobins
Comment Utility
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
 

Author Comment

by:urobins
Comment Utility
Thanks for the link, looking at it now
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 
LVL 92

Expert Comment

by:objects
Comment Utility
try adding something in the CENTER, that way the space will get handled better.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 

Author Comment

by:urobins
Comment Utility
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
 

Author Comment

by:urobins
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
>       public class DisplayCoins extends Canvas

also change that to JPanel
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
>                        meter.paintComponent((Graphics2D)coins.getGraphics());

never call paintComponent() directly

that should be:

                       meter.repaint();
0
 

Author Comment

by:urobins
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 92

Expert Comment

by:objects
Comment Utility
Thats because you are using Canvas, see my comment above. You should use JPanel instead
0
 

Author Comment

by:urobins
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 

Author Comment

by:urobins
Comment Utility
so I would just do JPanel.paintComponent(); ??
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
super.paintComponent(g);
0
 

Author Comment

by:urobins
Comment Utility
and Im sorry where would I put that, in my displayCoins?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
at start of paintComponent(), in fact u already have it there commented out
0
 

Author Comment

by:urobins
Comment Utility
OH :)  Thanks.
0
 

Author Comment

by:urobins
Comment Utility
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
 

Author Comment

by:urobins
Comment Utility
actually it leaves what ever the last drawn coin was.
0
 

Author Comment

by:urobins
Comment Utility
Okay got that fixed :)
0
 

Author Comment

by:urobins
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 

Author Comment

by:urobins
Comment Utility
Oh okay, I'll give that a shot... where do I do that in the main parkingmeter class where I declare the panel?
0
 

Author Comment

by:urobins
Comment Utility
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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
What is the color set for buttonPanel/
0
 

Author Comment

by:urobins
Comment Utility
default color set.  I am going to open a new question for this to be fair, as it deals with a different topic
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating Java 9 87
pairs challenge 5 44
micro services vs rest web services 16 45
Java / Linux and Regular Expressions 11 43
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now