Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

I am not getting an arc displayed

Posted on 2007-03-22
33
201 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
ID: 18775830
> public class DisplayMeter extends Canvas

change Canvas to JPanel
0
 
LVL 92

Expert Comment

by:objects
ID: 18775836
also set the preferred size
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18775938
You need to override paint in DisplayMeter, not paintComponent
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

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

Expert Comment

by:objects
ID: 18775994
> 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
ID: 18776000
0
 

Author Comment

by:urobins
ID: 18776007
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
ID: 18776009
Thanks for the link, looking at it now
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18776011
>>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
ID: 18776031
try adding something in the CENTER, that way the space will get handled better.
0
 
LVL 92

Expert Comment

by:objects
ID: 18776040
> 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
ID: 18776051
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
ID: 18776065
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
ID: 18776079
>       public class DisplayCoins extends Canvas

also change that to JPanel
0
 
LVL 92

Expert Comment

by:objects
ID: 18776094
>                        meter.paintComponent((Graphics2D)coins.getGraphics());

never call paintComponent() directly

that should be:

                       meter.repaint();
0
 

Author Comment

by:urobins
ID: 18776146
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
 
LVL 92

Expert Comment

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

Author Comment

by:urobins
ID: 18776174
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
ID: 18776213
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
ID: 18776231
so I would just do JPanel.paintComponent(); ??
0
 
LVL 92

Expert Comment

by:objects
ID: 18776252
super.paintComponent(g);
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:urobins
ID: 18776331
OH :)  Thanks.
0
 

Author Comment

by:urobins
ID: 18776346
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
ID: 18776353
actually it leaves what ever the last drawn coin was.
0
 

Author Comment

by:urobins
ID: 18776371
Okay got that fixed :)
0
 

Author Comment

by:urobins
ID: 18776375
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
ID: 18776434
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
ID: 18776452
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
ID: 18776462
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:Mayank S
ID: 18784108
What is the color set for buttonPanel/
0
 

Author Comment

by:urobins
ID: 18784116
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

840 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