定义4个类,MyShape、MyLine、MyRectangle和MyOval,其中MyShape是其他三个类的父类。MyShape为抽象类,包括图形位置的四个坐标;一个无参的构造方法,将所有的坐标设置为0;一个带参的构造函数,将所有的坐标设置为相应值;每个坐标的设置和读取方法;abstract void draw(Graphics g)方法。MyLine类负责画直线,实现父类的draw方法;MyRectangle负责画矩形,实现父类的draw方法;MyOval负责画椭圆,实现父类的draw方法。编写一个应用程序,使用上面定义的类,随机选取位置和形状,绘制20个图形。示例输出下图所示。
package priv.lhw.random.shape;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
}
package priv.lhw.random.shape;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
public MyFrame(){
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = toolkit.getScreenSize();
int screenHeight = dimension.height;
int screenWidth = dimension.width;
setSize(screenWidth/2, screenHeight/2);
int x = (screenWidth - getWidth()) / 2;
int y = (screenHeight - getHeight()) / 2;
setLocation(x, y);
setTitle("绘制随机形状");
MyPanel myPanel = new MyPanel();
add(myPanel);
}
}
package priv.lhw.random.shape;
import java.awt.*;
import java.awt.geom.Line2D;
public class MyLine extends MyShape {
public MyLine(double x1, double y1, double x2, double y2) {
super(x1, y1, x2, y2);
}
@Override
public void draw(Graphics g) {
Graphics2D graphics2D = (Graphics2D) g;
Line2D line2D = new Line2D.Double(getX1(), getY1(), getX2(), getY2());
graphics2D.draw(line2D);
}
}
package priv.lhw.random.shape;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class MyOval extends MyShape{
public MyOval(double x1, double y1, double x2, double y2) {
super(x1, y1, x2, y2);
}
@Override
public void draw(Graphics g) {
Graphics2D graphics2D = (Graphics2D) g;
Ellipse2D ellipse2D = new Ellipse2D.Double(getX1(), getY1(), getX2(), getY2());
graphics2D.draw(ellipse2D);
}
}
package priv.lhw.random.shape;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class MyPanel extends JPanel {
private static final int NUM = 5;
@Override
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
MyShape myShape;
for (int i = 0; i < NUM; i++){
myShape = new MyLine(getRandomX(), getRandomY(), getRandomX(), getRandomY());
myShape.draw(graphics);
myShape = new MyRectangle(getRandomX(), getRandomY(), getRandomX(), getRandomY());
myShape.draw(graphics);
myShape = new MyOval(getRandomX(), getRandomY(), getRandomX(), getRandomY());
myShape.draw(graphics);
}
}
private double getRandomX(){
return new Random().nextDouble() * getWidth() / 2;
}
private double getRandomY(){
return new Random().nextDouble() * getHeight() / 2;
}
}
package priv.lhw.random.shape;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class MyRectangle extends MyShape{
public MyRectangle(double x1, double y1, double x2, double y2) {
super(x1, y1, x2, y2);
}
@Override
public void draw(Graphics g) {
Graphics2D graphics2D = (Graphics2D) g;
Rectangle2D rectangle2D = new Rectangle2D.Double(getX1(), getY1(), getX2(), getY2());
graphics2D.draw(rectangle2D);
}
}
package priv.lhw.random.shape;
import java.awt.*;
abstract class MyShape {
private double x1;
private double y1;
private double x2;
private double y2;
public MyShape(){
this.x1 = 0;
this.y1 = 0;
this.x2 = 0;
this.y2 = 0;
}
public MyShape(double x1, double y1, double x2, double y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public double getX1() {
return x1;
}
public double getY1() {
return y1;
}
public double getX2() {
return x2;
}
public double getY2() {
return y2;
}
public void setX1(double x1) {
this.x1 = x1;
}
public void setX2(double x2) {
this.x2 = x2;
}
public void setY1(double y1) {
this.y1 = y1;
}
public void setY2(double y2) {
this.y2 = y2;
}
/**
* 绘制图像
* @param g
*/
public abstract void draw(Graphics g);
}