Created
March 18, 2012 21:48
-
-
Save anonymous/2082150 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Graphics; | |
import java.awt.Color; | |
import java.lang.Math; | |
import javax.swing.JPanel; | |
import javax.swing.JFrame; | |
import javax.swing.JOptionPane; | |
public class Circles | |
{ | |
public static void main(String[] args) | |
{ | |
JFrame frame = new JFrame("Circles Demo"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setSize(800, 600); | |
boolean loop = false; | |
int rad, xc, yc; | |
do | |
{ | |
String tryRad = JOptionPane.showInputDialog( | |
"Please enter a radius. (0-300)"); | |
rad = isValidNumber(tryRad); | |
if (rad < 0) loop = true; | |
}while(!loop); | |
loop = false; | |
do | |
{ | |
String tryCoord = JOptionPane.showInputDialog( | |
"Please enter an x-coordinate. (" + rad + "-" + (800-rad) + ")"); | |
xc = isValidNumber(tryCoord); | |
if (xc < rad || xc > (800-rad)) loop = true; | |
}while(!loop); | |
loop = false; | |
do | |
{ | |
String tryCoord = JOptionPane.showInputDialog( | |
"Please enter a y-coordinate. (" + rad + "-" + (600-rad) + ")"); | |
yc = isValidNumber(tryCoord); | |
if (yc < rad || yc > (600-rad)) loop = true; | |
}while(!loop); | |
CirclesPanel cp = new CirclesPanel(rad, xc, yc); | |
frame.add(cp); | |
frame.setVisible(true); | |
} | |
public static int isValidNumber(String s) | |
{ | |
try | |
{ | |
int result = Integer.parseInt(s); | |
return result; | |
} | |
catch (NumberFormatException ex) | |
{ | |
return -1; | |
} | |
} | |
} | |
class CirclesPanel extends JPanel | |
{ | |
int rad, xc, yc; | |
public CirclesPanel(int r, int x, int y) | |
{ | |
rad = r; | |
xc = x; | |
yc = y; | |
} | |
public void paintComponent(Graphics g) | |
{ | |
super.paintComponent(g); | |
this.setBackground(Color.WHITE); | |
g.drawOval(xc, yc, 2*rad, 2*rad); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment