import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class RandomShapesComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
RandomShapeGenerator r = new RandomShapeGenerator(getWidth(), getHeight());
for (int i = 1; i <= 10; i++)
g2.draw(r.randomShape());
}
}
import javax.swing.JFrame;
public class RandomShapeViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
final int FRAME_WIDTH = 300;
final int FRAME_HEIGHT = 400;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("RandomShapeViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RandomShapesComponent component = new RandomShapesComponent();
frame.add(component);
frame.setVisible(true);
}
}
I am suppose to write a method randomShape that randomly generates objects implementing the Shape Interface: rectangles, circles ect. call it 10 times. They gave me the RandomShapeViewer and RandonShapeComponent above.
I do not understand how do i make a shape interface and call or create different shapes in my program. I created the class RandomShapeGeneator but i donot know how to write the correct code for the interface. Can someone point me in the write direction. I thought I needed to create a circle.draw() in my program.
import java.awt.Shape;
public class RandomShapeGenerator
{
public RandomShapeGenerator(int i, int j)
{
// TODO Auto-generated constructor stub
}
public Shape randomShape()
{
// TODO Auto-generated method stub
return null;
}
}

New Topic/Question
Reply



MultiQuote







|