Please im having troubles getting how to make a linked list of circle shapes. That is creat a multiple shape circle and adding them to linked list. Please how do i go about this? Thanks any help, would be nice.
Circle circle;
List linked = new LinkedList();
linked.add(circle);
is this not what you're trying to do?? This is as close as i can get w/o more information... If you were to ask a doctor how to operate, you wouldn't ask "How do i fix blood?", you would ask something like "How do i insert an angioplasty balloon?"
make the LinkedList class? I'm not sure i understand... beyond the code that i posted, you'd have to include a skeleton program, an import statement or 2, and definitions for your circles to get working code. You don't need to override the LinkedList class already provided.
guess the kid doesn't know about the existence of LinkedList and/or has a homework assignment (you're late kid, that was 2 weeks ago) to make his own LinkedList class.
sry guys maybe i did not make myself clear the first time. I want to practise using LinkedList. My task is to creat multiple circle using linkedlist instead of using arrays. here is my sample code. but im confused on how to go about the linked list of shapes?
MyProject2007.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.JFrame;
publicclass project2007 extends JFrame {
public project2007(){
DrawShapes ball = new DrawShapes();
JFrame content = new JFrame();
content.add(ball);
content.setSize(800, 800);
content.setVisible(true);
content.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}/* end of public constructor for project 2007 */
/** set size, visible, and exit properly when frame is closed */
publicstaticvoid main(String args[])
{
project2007 project = new project2007();
} /* END OF MAIN */
}/* END OF CLASS PROJECT 2007 */
Shape.java
abstractclass Shape {
// A class representing shapes that can be displayed on a ShapeCanvas.
// The subclasses of this class represent particular types of shapes.
// When a shape is first constucted, it has height and width zero
// and a default color of white.
int left, top; // Position of top left corner of rectangle that bounds this shape.
int width, height; // Size of the bounding rectangle.
Color color = Color.white; // Color of this shape.
void reshape(int left, int top, int width, int height) {
// Set the position and size of this shape.
this.left = left;
this.top = top;
this.width = width;
this.height = height;
}
void moveBy(int dx, int dy) {
// Move the shape by dx pixels horizontally and dy pixels veritcally
// (by changing the position of the top-left corner of the shape).
left += dx;
top += dy;
}
void setColor(Color color) {
// Set the color of this shape
this.color = color;
}
boolean containsPoint(int x, int y) {
// Check whether the shape contains the point (x,y).
// By default, this just checks whether (x,y) is inside the
// rectangle that bounds the shape. This method should be
// overridden by a subclass if the default behaviour is not
// appropriate for the subclass.
if (x >= left && x < left+width && y >= top && y < top+height)
returntrue;
elsereturnfalse;
}
abstractvoid draw(Graphics g);
// Draw the shape in the graphics context g.
// This must be overriden in any concrete subclass.
} // end of class Shape
class RectShape extends Shape {
// This class represents rectangle shapes.
void draw(Graphics g) {
g.setColor(color);
g.fillRect(left,top,width,height);
g.setColor(Color.black);
g.drawRect(left,top,width,height);
}
}
class OvalShape extends Shape {
// This class represents oval shapes.
void draw(Graphics g) {
g.setColor(color);
g.fillOval(left,top,width,height);
g.setColor(Color.black);
g.drawOval(left,top,width,height);
}
boolean containsPoint(int x, int y) {
// Check whether (x,y) is inside this oval, using the
// mathematical equation of an ellipse.
double rx = width/2.0; // horizontal radius of ellipse
double ry = height/2.0; // vertical radius of ellipse
double cx = left + rx; // x-coord of center of ellipse
double cy = top + ry; // y-coord of center of ellipse
if ( (ry*(x-cx))*(ry*(x-cx)) + (rx*(y-cy))*(rx*(y-cy)) <= rx*rx*ry*ry )
returntrue;
elsereturnfalse;
}
}