标签:
//利用awt.Graphics画图
//Graph用于显示所要画的图型
//Shape的子类定义可以图形
//要改变图形,可以利用Graphics类提供的方法实现
//要在同一窗口显示更多图形,通过重载paint()实现。
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Graph extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private Shape shape;
public Graph(Shape shp) {
super();
this.shape = shp;
}
@Override
public void paint(Graphics g) {
super.paint(g);
shape.draw(g);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
frame.setTitle("Shape");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rect rect = new Rect(10,10,100,100);
Graph grp = new Graph(rect);
frame.add(grp);
frame.setLocationRelativeTo(null);
frame.setSize(600,400);
frame.setVisible(true);
}
}
abstract class Shape{
public abstract void draw(Graphics g);
}
class Rect extends Shape{
private int x,y,sizeX,sizeY;
public Rect(int x,int y,int sizeX,int sizeY){
this.x = x;
this.y = y;
this.sizeX = sizeX;
this.sizeY = sizeY;
}
public void draw(Graphics g){
g.drawRect(x,y,sizeX,sizeY);
}
}
class Line extends Shape{
private int x,y,sizeX,sizeY;
public Line(int x,int y,int sizeX,int sizeY){
this.x = x;
this.y = y;
this.sizeX = sizeX;
this.sizeY = sizeY;
}
public void draw(Graphics g){
g.drawLine(x,y,sizeX,sizeY);
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/flushhj/article/details/47992839