Solved

how to draw a semi circle

Posted on 2007-03-21
14
1,990 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 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

21 Experts available now in Live!

Get 1:1 Help Now