NOTE: The following code must pass in the following compiler. Please make absolu
ID: 3774267 • Letter: N
Question
NOTE: The following code must pass in the following compiler. Please make absolutely sure it does before posting: http://codecheck.it/codecheck/files?repo=bj4fp&problem=ch03/c03_exp_3_15
Draw a "bull's eye" - a set of concentric rings in alternating black and white colors. Hint: Fill a black circle, then fill a smaller white circle on top, and so on. Your program should be composed of classes BullsEye, BullsEyeComponent, and BullsEyeViewer. Here is a sample program output: (example image found on above link)
Use the following class as your main class:
Complete the following class in your solution:
Use the following class in your solution:
Explanation / Answer
*******************************BullsEyeViewer.java*********************************************
package bullseye;
import javax.swing.JFrame;
/*
Displays a "bull's eye".
*/
public class BullsEyeViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(220, 240);
frame.setTitle("BullsEye");
BullsEyeComponent component = new BullsEyeComponent();
frame.add(component);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
*****************************************BullsEye.java***********************************************
package bullseye;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
public class BullsEye
{
/**
Creates a new instance of BullsEye.
@param r the radius
@param x the center x-coordinate
@param y the center y-coordinate
*/
int rad;
int x_co;
int y_co;
public BullsEye(int r, int x,int y)
{
rad=r;
x_co=x;
y_co=y;
}
/*
Draws the bull's eye.
@param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
while (rad >= 10) {
g2.setStroke(new BasicStroke(8F));
if(rad<=10){
Ellipse2D.Double circle = new Ellipse2D.Double(x_co-rad,y_co-rad,2*rad,2*rad);
g2.fill(circle);
}
else{
g2.drawOval(x_co-rad,y_co-rad,2*rad,2*rad);
}
rad = rad-20;
}
}
}
***************************************BullsEyeComponent.java****************************************
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bullseye;
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
/*
Displays a bull's eye.
*/
public class BullsEyeComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
int radius = 50;
int xCenter = 100;
int yCenter = 100;
BullsEye be = new BullsEye(radius, xCenter, yCenter);
be.draw(g2);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.