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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
and drawLine() to draw the line
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.setDefaultClose Operation( JFrame.EXI T_ON_CLOSE );
meterFrame.setSize(new Dimension(600, 400));
// Create the Meter panel
meterPanel = new JPanel(new GridLayout(1, 5));
addWidgets();
meterFrame.getContentPane( ).add(mete rPanel, BorderLayout.NORTH);
meterFrame.getContentPane( ).add(coin s, BorderLayout.SOUTH);
meterFrame.pack();
meterFrame.setVisible(true );
}
public void addWidgets()
{
JButton quarterButton,nickleButton ,dimeButto n,pennyBut ton,clearB utton;
//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(quarterButt on);
meterPanel.add(dimeButton) ;
meterPanel.add(nickleButto n);
meterPanel.add(pennyButton );
meterPanel.add(clearButton );
//add listeners and actions to the buttons
quarterButton.addActionLis tener(new ActionListener()
{
public void actionPerformed(ActionEven t event)
{
quarterButtonPressed();
total = total +25;
time = time +5;
System.out.println(total);
}
}
);
dimeButton.addActionListen er(new ActionListener()
{
public void actionPerformed(ActionEven t event)
{
dimeButtonPressed();
total = total +10;
time= time +2;
System.out.println(total);
}
}
);
nickleButton.addActionList ener(new ActionListener()
{
public void actionPerformed(ActionEven t event)
{
nickleButtonPressed();
total = total +5;
time = time+1;
System.out.println(total);
}
}
);
pennyButton.addActionListe ner(new ActionListener()
{
public void actionPerformed(ActionEven t event)
{
pennyButtonPressed();
total = total +1;
System.out.println(total);
}
}
);
clearButton.addActionListe ner(new ActionListener()
{
public void actionPerformed(ActionEven t event)
{
clearButtonPressed();
total=0;
}
}
);
}
public void quarterButtonPressed()
{
coins.SetCoinType(1);
coins.paintQuarters((Graph ics2D)coin s.getGraph ics());
//coins.repaint();
}
public void dimeButtonPressed()
{
coins.SetCoinType(2);
coins.paintDimes((Graphics 2D)coins.g etGraphics ());
//coins.repaint();
}
public void nickleButtonPressed()
{
coins.SetCoinType(3);
coins.paintNickels((Graphi cs2D)coins .getGraphi cs());
//coins.repaint();
}
public void pennyButtonPressed()
{
coins.SetCoinType(4);
coins.paintPennies((Graphi cs2D)coins .getGraphi cs());
//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(ActionEven t arg0) {
// TODO Auto-generated method stub
}
}//end parking meter
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.setDefaultClose
meterFrame.setSize(new Dimension(600, 400));
// Create the Meter panel
meterPanel = new JPanel(new GridLayout(1, 5));
addWidgets();
meterFrame.getContentPane(
meterFrame.getContentPane(
meterFrame.pack();
meterFrame.setVisible(true
}
public void addWidgets()
{
JButton quarterButton,nickleButton
//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(quarterButt
meterPanel.add(dimeButton)
meterPanel.add(nickleButto
meterPanel.add(pennyButton
meterPanel.add(clearButton
//add listeners and actions to the buttons
quarterButton.addActionLis
{
public void actionPerformed(ActionEven
{
quarterButtonPressed();
total = total +25;
time = time +5;
System.out.println(total);
}
}
);
dimeButton.addActionListen
{
public void actionPerformed(ActionEven
{
dimeButtonPressed();
total = total +10;
time= time +2;
System.out.println(total);
}
}
);
nickleButton.addActionList
{
public void actionPerformed(ActionEven
{
nickleButtonPressed();
total = total +5;
time = time+1;
System.out.println(total);
}
}
);
pennyButton.addActionListe
{
public void actionPerformed(ActionEven
{
pennyButtonPressed();
total = total +1;
System.out.println(total);
}
}
);
clearButton.addActionListe
{
public void actionPerformed(ActionEven
{
clearButtonPressed();
total=0;
}
}
);
}
public void quarterButtonPressed()
{
coins.SetCoinType(1);
coins.paintQuarters((Graph
//coins.repaint();
}
public void dimeButtonPressed()
{
coins.SetCoinType(2);
coins.paintDimes((Graphics
//coins.repaint();
}
public void nickleButtonPressed()
{
coins.SetCoinType(3);
coins.paintNickels((Graphi
//coins.repaint();
}
public void pennyButtonPressed()
{
coins.SetCoinType(4);
coins.paintPennies((Graphi
//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,
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(ActionEven
// 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 :)
you'll get a compile error if u can't, so it will be obvious :)
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.
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?
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
http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html#setClip(int,%20int,%20int,%20int)