Solved

how to draw a semi circle

Posted on 2007-03-21
14
2,003 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:urobins
  • 6
  • 5
  • 2
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 18768167
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 18768184
0
 
LVL 92

Expert Comment

by:objects
ID: 18768198
0
 

Author Comment

by:urobins
ID: 18768275
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 18768356
use drawArc() (or fillArc()) to draw the arc
and drawLine() to draw the line
0
 

Author Comment

by:urobins
ID: 18768373
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 18768383
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 :)

0
 

Author Comment

by:urobins
ID: 18768398
Thanks!  Im gonna get hammering on this.. YOu guys are great as always!
0
 
LVL 92

Expert Comment

by:objects
ID: 18768412
and do *not* use setClip() thats not what its for. You'll just get into a mess using it.
0
 

Author Comment

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

:)
0
 
LVL 92

Expert Comment

by:objects
ID: 18768419
actually can u explain why u accepted a comment that is incorrect?
0
 

Author Comment

by:urobins
ID: 18768433
I had thought either one would work, so I accepted both after only testing yours.  I don't know how to change that now...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18769624
Reopen the question if you want urobins
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
where is session ID cache stored 1 46
starter POM and spring-boot-starter,  spring-boot-web 2 40
simple java question 3 44
egit plugin on eclipse 8 33
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 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

16 Experts available now in Live!

Get 1:1 Help Now