
Andy C. answered 07/19/17
Tutor
4.9
(27)
Math/Physics Tutor
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
public class RectangleCircle extends JPanel {
private static final int RECT_X = 100;
private static final int RECT_Y = RECT_X;
private static final int RECT_WIDTH = 400;
private static final int RECT_HEIGHT = RECT_WIDTH;
private static int centerX;
private static int centerY;
private static int radius;
private static boolean rectCircleFlag; // true if the circle lies entirely inside the rectangle
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// draw the rectangle here
g.drawRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_HEIGHT);
int r = radius;
int x = centerX-(r/2);
int y = centerY-(r/2);
int maxX = RECT_X + RECT_WIDTH;
int maxY = RECT_Y + RECT_HEIGHT;
rectCircleFlag = ( ((x-r)<RECT_X) || ((x+r)>maxX) || ((y-r)<RECT_Y) || ((y+r)>maxY) );
//Color myColor = new Color();
if (rectCircleFlag)
{
g.setColor(Color.YELLOW);
}
else
{
g.setColor(Color.GRAY);
}
g.fillOval(x,y,r,r);
}
@Override
public Dimension getPreferredSize() {
// so that our GUI is big enough
return new Dimension(RECT_WIDTH + 2 * RECT_X, RECT_HEIGHT + 2 * RECT_Y);
}
// create the GUI explicitly on the Swing event thread
private static void createAndShowGui() {
RectangleCircle mainPanel = new RectangleCircle();
String inbuff = JOptionPane.showInputDialog(" Please Input the X-coordinate of the center of the circle");
centerX = Integer.parseInt(inbuff);
inbuff = JOptionPane.showInputDialog(" Please Input the Y-coordinate of the center of the circle");
centerY = Integer.parseInt(inbuff);
inbuff = JOptionPane.showInputDialog(" Please Input the Radius of the circle");
radius = Integer.parseInt(inbuff);
System.out.println(" Center X = " + centerX + " : Center Y = " + centerY + " : radius = " +radius);
JFrame frame = new JFrame("DrawRect");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
public class RectangleCircle extends JPanel {
private static final int RECT_X = 100;
private static final int RECT_Y = RECT_X;
private static final int RECT_WIDTH = 400;
private static final int RECT_HEIGHT = RECT_WIDTH;
private static int centerX;
private static int centerY;
private static int radius;
private static boolean rectCircleFlag; // true if the circle lies entirely inside the rectangle
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// draw the rectangle here
g.drawRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_HEIGHT);
int r = radius;
int x = centerX-(r/2);
int y = centerY-(r/2);
int maxX = RECT_X + RECT_WIDTH;
int maxY = RECT_Y + RECT_HEIGHT;
rectCircleFlag = ( ((x-r)<RECT_X) || ((x+r)>maxX) || ((y-r)<RECT_Y) || ((y+r)>maxY) );
//Color myColor = new Color();
if (rectCircleFlag)
{
g.setColor(Color.YELLOW);
}
else
{
g.setColor(Color.GRAY);
}
g.fillOval(x,y,r,r);
}
@Override
public Dimension getPreferredSize() {
// so that our GUI is big enough
return new Dimension(RECT_WIDTH + 2 * RECT_X, RECT_HEIGHT + 2 * RECT_Y);
}
// create the GUI explicitly on the Swing event thread
private static void createAndShowGui() {
RectangleCircle mainPanel = new RectangleCircle();
String inbuff = JOptionPane.showInputDialog(" Please Input the X-coordinate of the center of the circle");
centerX = Integer.parseInt(inbuff);
inbuff = JOptionPane.showInputDialog(" Please Input the Y-coordinate of the center of the circle");
centerY = Integer.parseInt(inbuff);
inbuff = JOptionPane.showInputDialog(" Please Input the Radius of the circle");
radius = Integer.parseInt(inbuff);
System.out.println(" Center X = " + centerX + " : Center Y = " + centerY + " : radius = " +radius);
JFrame frame = new JFrame("DrawRect");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Andy C.
07/19/17