A java problem Question 1: Draw a Circle (50 points) Before starting this questi
ID: 3792572 • Letter: A
Question
A java problem Question 1: Draw a Circle (50 points) Before starting this question, we strongly recommend that you complete the last two warm- up exercises if you have not already done so. These warm-up questions will be the best way to start the assignment. For this question, you will write a program called circle that displays a circle in the upper right quadrant of the Cartesian plane. To do so, write (at a minimum) the following three methods: A. A method called onCircle that takes as input five ints. The first three parameters represent (in order) the radius and x,y coordinates of the centre of the circle. The last two parameters represent an x,y position on the grid. This method determines whether or not the circle should be drawn at the grid coordinates given by the method's parameters. To get the appropriate thickness for the circle, return whether the following formula holds or not: radius S (z a)2+ (w b2 s radius 1 In the above formula, a and b are the coordinates of the centre of the circle, and x and y are the grid coordinates. B. A method called verifyInput that takes as input three ints representing the radius and centre coor- dinates of the circle, and returns nothing. This method should throw an IllegalArgumentException and display a helpful error message if either the circle does not fit in the upper right quadrant, or if the radius is non-positive. Otherwise, the method will not print anything. As a hint, the circle does not fit in the upper-right quadrant if it is too close to the axes. C. A method called drawcircle that takes as input an in rernesenting the radius of the circle, another int representing the x coordinate of the centre of the circle, and a third int representing the
Explanation / Answer
package com.circle;
import java.awt.*;
import javax.swing.*;
public class CirclePanel1 extends JPanel{
int r,x,y,a,b;
Graphics g;
public CirclePanel1() {
setPreferredSize(new Dimension(9,9));
setBackground(Color.white);
}//end constructor
public CirclePanel1(int r2, int x2, int y2, int a2, int b2) {
// TODO Auto-generated constructor stub
this();
this.r=r2;
this.x=x2;
this.y=y2;
this.a=a2;
this.b=b2;
}
//=========================================== paintComponent
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawCircle(g, x, y, r); // center (1,1) r=20
g.drawLine(x,y,a,b);
g.drawLine(y,x,b,x);
onCircle(r,x,y,a,b);
}
public void verifyInput(int r,int x,int y,int a,int b){
try{
this.r=r;
this.x=x;
this.y=y;
this.a=a;
this.b=b;
}catch(IllegalArgumentException e){
System.out.println("Circle does not fit in upper quadrant");
}
}
public int onCircle(int r,int x,int y,int a,int b){
//drawCircle(g, x, y, r);
//paintComponent(g);
int val=0;
val=(x-a)^2+(y-b)^2;
System.out.println("thickness of circle: "+val);
return val;
}
// Convenience method to draw from center with radius
public void drawCircle(Graphics cg, int xCenter, int yCenter, int r) {
cg.drawOval(xCenter-r, yCenter-r, 2*r, 2*r);
}//end drawCircle
}
//This is the test class run this file you will be get output
package com.circle;
import java.awt.*;
import javax.swing.*;
public class Circle1 extends JFrame {
//pass the radios,x,y,a,b value here
CirclePanel1 drawing = new CirclePanel1(20,30,30,30,50);
Circle1() {
//--- Get content pane, set layout, add components
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(drawing, BorderLayout.CENTER); // Note 2
this.setTitle("Circles");
this.pack(); // finalize the layout
}//
public static void main(String[] args) {
JFrame myWindow = new Circle1();
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.