Link to home
Start Free TrialLog in
Avatar of urobins
urobins

asked on

how to draw a semi circle

Is there an easy way to draw a semi circle?  I know how to draw a full cirlce using drawoval, but I don't know how to chop that in half
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER

I am using this to create a meter and I will have to move the line accordingly will this be possible using an arc?  I am trying to implement this solution now.
use drawArc() (or fillArc()) to draw the arc
and drawLine() to draw the line
Avatar of urobins

ASKER

thanks I'll try that!

I have another question if you don't mind

I currently have a classwithin a class.  can I seperate it out so it is not in the body of my main class?

package project3;

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

import javax.swing.*;

public class ParkingMeter implements ActionListener
{
      JFrame meterFrame;
    JPanel meterPanel;
    DisplayCoins coins;
    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, 5));    
        addWidgets();
       
        meterFrame.getContentPane().add(meterPanel, BorderLayout.NORTH);
        meterFrame.getContentPane().add(coins, BorderLayout.SOUTH);
       
        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();
          
          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
you can as long as it doesn't reference any vars from the outer class
you'll get a compile error if u can't, so it will be obvious :)

Avatar of urobins

ASKER

Thanks!  Im gonna get hammering on this.. YOu guys are great as always!
and do *not* use setClip() thats not what its for. You'll just get into a mess using it.
Avatar of urobins

ASKER

Thanks.  I am playing with the arc now... Im sure I'll have more questions later, I always do!

:)
actually can u explain why u accepted a comment that is incorrect?
Avatar of urobins

ASKER

I had thought either one would work, so I accepted both after only testing yours.  I don't know how to change that now...
Reopen the question if you want urobins