Here is my code but for some reason my frame keeps coming up blank with no circl
ID: 3645185 • Letter: H
Question
Here is my code but for some reason my frame keeps coming up blank with no circle drawnCircle.java:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
*/
public class Circle {
double x;
double y;
double radius;
boolean filled;
Color c;
//constructor
public Circle(double x, double y, double radius, boolean filled, Color c) {
this.x = x;
this.y = y;
this.radius = radius;
this.filled = filled;
this.c = c;
}
public void draw(Graphics2D g){
Ellipse2D.Double circle = new Ellipse2D.Double(x, y, radius*2, radius*2);
g.draw(circle);
g.fill(circle);
g.setColor(Color.BLUE);
}
}
CircleComponent.java
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
*/
public class CircleComponent extends JComponent {
Circle c;
CircleComponent(double x, double y, double radius, boolean fill,Color cl){
c = new Circle(x,y,radius,fill,cl);
}
void paintComponent(Graphics2D g){
c.draw(g);
}
}
CircleViewer.java
import java.awt.Color;
import java.awt.Frame;
import java.util.Scanner;
import javax.swing.JFrame;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
*/
public class CircleViewer {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("X: ");
double x = s.nextDouble();
System.out.println("Y: ");
double y = s.nextDouble();
System.out.println("Radius: ");
double radius = s.nextDouble();
System.out.println("Filled (T/F): ");
String fill= s.next().toUpperCase();
boolean filll;
if (fill.equals("T")){
filll = true;
}
else {
filll = false;
}
System.out.println("Color: ");
String col = s.next().toLowerCase();
CircleComponent c = new CircleComponent(x, y, radius, filll,Color.BLUE);
// Create Frame
Frame frame = new Frame();
frame.setTitle("Flashcard Testing");
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.add(c);
frame.setVisible(true);
}
}
Instructions
Create a Circle class
Instance Variable
a double representing the x coordinate of the circle
a double representing the y coordinate of the circle
a double representing the radius of the circle
a boolean that determines if the circle is filled in.
a Color object representing the color of the circle
Parameterized constructor
Parameters representing the x, y, radius and fill in of the circle.
Sets the parameters to the appropriate instance field.
Methods
draw method
Takes in a Graphic2D object
Determines if the Circle is filled in or not.
Draws the circle to the Graphic2D object
setColor method
Set the color of the circle based on the size of the circle
Yellow for a circle 50 pixal or less
Green for a circle 100 pixal or less
Blue for a circle 200 pixal or less
Read for a cicle > 200.
NOTE: use constants to set the circle max sizes, do no use magic numbers.
Create a CircleComponent Class
Instance Variable
Explanation / Answer
This is a simple program of java awt. In this section, you will learn how to create Circle Diagram. The java circle is the most fundamental abstractions in a Java 2D in the supported java.awt.shapepackage. It describes a shape. But the Java 2D definition of a shape does not require the shape to enclose an area. The Java2D API also provides several classes that defines common Geometric Objects.
Program Description:
In this program, you will also show that how to create square drawing. Inside the program define a class name CircleDraw for the circle component. There is created two types of diagrams "Circle" and "square" for this program. Here, this program uses Ecllpise2D that is implemented to specify eclipse float and double. double and float components are represented for creating the square and circle in this program.
setPaint(): This class is used for filling the color.
Here is the code of this program:
Now, to do it in an applet is very easy;
import java.awt.event.*;
import java.awt.geom.*;
public class CircleDraw extends Frame {
Shape circle = new Ellipse2D.Float(100.0f, 100.0f, 100.0f, 100.0f);
Shape square = new Rectangle2D.Double(100, 100,100, 100);
public void paint(Graphics g) {
Graphics2D ga = (Graphics2D)g;
ga.draw(circle);
ga.setPaint(Color.green);
ga.fill(circle);
ga.setPaint(Color.red);
ga.draw(square);
}
public static void main(String args[]) {
Frame frame = new CircleDraw();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
frame.setSize(300, 250);
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.