完成以下Java代码:Write an IShape interface with the following UML specification: +------------------------------------+ | <<interface>> | | IShape | +------------------------------------+ | + getX(): int | | + getY(): int | | + setX(int x): void | | + setY(int y): void | | + isVisible(int w, int h): boolean | | + isIn(int x, int y): boolean | | + draw(Graphics g): void | +------------------------------------+ and a Shape class that implements IShape and has the following UML specification: +------------------------------------+ | Shape | +------------------------------------+ | - x: int | | - y: int | | - color: Color | +------------------------------------+ | + Shape(int x, int y) | | + getX(): int | | + getY(): int | | + setX(int x): void | | + setY(int y): void | | + isVisible(int w, int h): boolean | | + isIn(int x, int y): boolean | | + draw(Graphics g): void | | + testShape(): void | +------------------------------------+ The x and y instance variables indicate the position of the center of the shape, and the color instance variable indicates the color of the shape. The color of the shape is computed randomly in the constructor of the shape class and never changes after that, like this: color = new Color((float)Math.random(), (float)Math.random(), (float)Math.random()); The isVisible method is abstract, and indicates whether the shape is currently visible or not inside a window of width w and of height h. The isIn method is abstract, and indicates whether the point at coordinates (x, y) is currently inside the shape or not. The draw method simply changes the color of the graphics object g to be the correct color for the shape
时间: 2023-06-16 10:04:52 浏览: 99
IShape.java:
```java
import java.awt.Graphics;
public interface IShape {
int getX();
int getY();
void setX(int x);
void setY(int y);
boolean isVisible(int w, int h);
boolean isIn(int x, int y);
void draw(Graphics g);
}
```
Shape.java:
```java
import java.awt.Color;
import java.awt.Graphics;
public class Shape implements IShape {
private int x;
private int y;
private Color color;
public Shape(int x, int y) {
this.x = x;
this.y = y;
this.color = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public boolean isVisible(int w, int h) {
// Implement visibility logic here
return true;
}
public boolean isIn(int x, int y) {
// Implement containment logic here
return true;
}
public void draw(Graphics g) {
g.setColor(color);
// Draw the shape here
}
public void testShape() {
// Test the shape here
}
}
```
阅读全文