Question: using these codes make it produce a repeating image using the simpleShapes ( SCircles SPolygons SRectangles and SSquares ) package space; import painter.SPainter; import java.awt.Color;

using these codes make it produce a repeating image using the simpleShapes(SCircles SPolygons SRectangles and SSquares)
package space;
import painter.SPainter;
import java.awt.Color;
import java.util.Random;
import java.awt.geom.Point2D;
import shapes.SRectangle;
public class Droid {
private String name;
private static final String CLASS = "Astromech Droid"; // Example droid class
private int size; // Size in arbitrary units
private boolean facingDirection; // true for one direction, false for the opposite
private Color color;
// Constructor to initialize instance variables
public Droid(String name, int size, boolean facingDirection){
this.name = name;
this.size = size;
this.facingDirection = facingDirection;
this.color = getRandomColor(); // Initialize with a random color
}
// Method to generate a random color
private Color getRandomColor(){
Random rand = new Random();
return new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
}
// Getter methods
public String getName(){
return name;
}
public String getClassType(){
return CLASS;
}
public int getSize(){
return size;
}
public boolean isFacingDirection(){
return facingDirection;
}
public Color getColor(){
return color;
}
// toString method to represent Droid information
@Override
public String toString(){
return "Droid Name: "+ getName()+", Class: "+ getClassType()+", Size: "+ getSize()+
", Facing Direction: "+(isFacingDirection()? "Forward" : "Backward")+
", Color: "+ getColor().toString();
}
// Method to check if two Droids could collide
public boolean couldCollide(Droid other){
return this.facingDirection != other.facingDirection; // True if facing opposite directions
}
// Method to paint the droid using SPainter
public void paint(SPainter painter){
painter.setColor(color);
Point2D.Double currentPosition = painter.position();
SRectangle rectangle = new SRectangle(currentPosition.getX(), currentPosition.getY());
painter.paint(rectangle);
if (facingDirection){
painter.dfd( size );
} else {
painter.dbk(size);
}
}
} package space;
import painter.SPainter;
import javax.swing.SwingUtilities;
public class DroidTester {
public DroidTester(){
// Create some Droid instances to test with
Droid droid1= new Droid("R2-D2",10, true);
Droid droid2= new Droid("BB-8",8, false);
Droid droid3= new Droid("C-3PO",12, true);
System.out.println(droid1);
System.out.println(droid2);
System.out.println(droid3);
System.out.println("Collision Test:");
System.out.println("R2-D2 collides with BB-8?"+ droid1.couldCollide(droid2));
System.out.println("BB-8 collides with C-3PO?"+ droid2.couldCollide(droid3));
// Assuming SPainter is properly initialized here:
SPainter painter = new SPainter(800,800);
painter.setVisible(true); // Make sure the painter is visible
droid1.paint(painter);
droid2.paint(painter);
droid3.paint(painter);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new DroidTester();
}
});
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!