码迷,mamicode.com
首页 > 编程语言 > 详细

Java画图

时间:2015-08-26 07:08:10      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

//利用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);
	}
	}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Java画图

标签:

原文地址:http://blog.csdn.net/flushhj/article/details/47992839

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!