制作一个画图界面主要分为四个步骤
一、 创建一个图画界面
利用Java 的 swing,awt 提供的相关内容进行绘画
二、 处理事件
事件监听机制
三. 编写事件处理的具体方法
四. 实现重绘
创建一个可以保存相关信息的类(Shape)来记录信息,在建立Shape[] array数组来记录数据
1. Drawing 主要用于实现画图界面的绘画
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Drawing extends JFrame{
public Shape[] array = new Shape[1000]; //利用数组将数据存起来
public static void main(String[] args) {
Drawing draw = new Drawing();
draw.InitUI();
}
public void InitUI() {
setTitle("画图界面");
setLayout(new FlowLayout());
setSize(800,800);
setLocationRelativeTo(null);
setDefaultCloseOperation(3);
//定义图型下拉框 1. 用一个数组记录下拉框中的内容
String[] typeArray = {"直线","矩形","圆","3D矩形","图片","文字Hello","等腰三角形","五角星","任意多边形","立方体","铅笔","喷枪","橡皮擦"};
JComboBox<String> cbType = new JComboBox<String>(typeArray);
cbType.setName("图型"); //给下拉框取名字,以便区分点击的 图型 、 颜色 、粗细中哪一个下拉框
add(cbType);
//定义颜色下拉框 1. 用一个数组记录下拉框中的内容
String[] colorArray = {"红色","绿色","蓝色"};
JComboBox<String> cbColor = new JComboBox<String>(colorArray);
cbColor.setName("颜色"); //给下拉框取名字,以便区分点击的 图型 、 颜色 、粗细中哪一个下拉框
add(cbColor);
//定义粗细下拉框 1. 用一个数组记录下拉框中的内容
String[] strokeArray = {"1","3","5"};
JComboBox<String> cbStroke = new JComboBox<String>(strokeArray);
cbStroke.setName("粗细"); //给下拉框取名字,以便区分点击的 图型 、 颜色 、粗细中哪一个下拉框
add(cbStroke);
setVisible(true);
Graphics g = getGraphics(); //画笔一定要在setVisible(boolean)方法后定义,否则 g = null;
DrawingListener dl = new DrawingListener((Graphics2D)g,array);
cbType.addActionListener(dl); //处理下拉框 图型中的 事件
cbColor.addActionListener(dl); //处理下拉框 颜色中的 事件
cbStroke.addActionListener(dl); //处理下拉框 粗细中的 事件
addMouseListener(dl);
//处理在窗口上发生的动作
//主要是鼠标的点击(mouseClicked),按下(mousePressed),释放( mouseReleased),进入(mouseEntered),离开(mouseExited)
addMouseMotionListener(dl);
//处理在窗口上发生的动作
//鼠标的拖动(mouseDragged),移动(mouseMoved)
}
public void paint(Graphics g) { //Drawing类继承看JFrame,这里重写JFrame类中的paint()方法,以实现重绘
super.paint(g); //调用父类中的paint方法,以画出窗口和下拉框
System.out.println("重绘窗体和组件");
//将窗口重绘前已经画的内容绘画一遍,以实现窗口重绘后内容的复原
for(int i = 0;i < array.length;i++)
if(array[i] != null)
array[i].draw((Graphics2D) g);
}
}
2.DrawingListener 主要用于处理事件
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Scanner;
import javax.swing.JComboBox;
import javax.swing.JFrame;
/** DrawingListener 继承JFrame以得到画笔,Toolkit
* 继承ActionListener,MouseListener,MouseMotionListener 事件接听接口
* */
public class DrawingListener extends JFrame implements ActionListener,MouseListener,MouseMotionListener {
private String type = "直线";
private Color color = Color.red;
private float stroke = 1;
private Graphics2D g;
private int x1,y1,x2,y2;
int fx,fy,lx,ly; //fx,fy记录起点的坐标,lx、ly记录上一个点的坐标
private boolean sign = true; //记录是否为起点(多边形)
public Shape[] array ;
int index;
/**
* 构造方法
* g是从窗体上传递过来的画笔对象
* array 将数组传过来以储存数据
*/
public DrawingListener(Graphics2D g,Shape[] array) {
this.g = g;
this.array = array;
}
public void actionPerformed(ActionEvent e) {
JComboBox<String> cb = (JComboBox<String>)e.getSource(); //获取事件源对象
String str = cb.getName(); //获取下拉框的名字,根据name属性来做判断
if(str.equals("图型")) {
type = cb.getSelectedItem().toString(); //获取选择的图形信息
}else if(str.equals("颜色")) { //获取选择的颜色信息
if(cb.getSelectedItem().toString().equals("红色"))
color = Color.RED;
else if(cb.getSelectedItem().toString().equals("绿色"))
color = Color.GREEN;
else if(cb.getSelectedItem().toString().equals("蓝色"))
color = Color.BLUE;
}else if(str.equals("粗细")) {
//Integer.parseInt(String) 将字符串转化为整型
stroke = (float)Integer.parseInt(cb.getSelectedItem().toString());
}
}
public void mouseClicked(MouseEvent e) { //鼠标点击调用的方法
};
public void mousePressed(MouseEvent e) { //鼠标按下调用的方法
x1 = e.getX(); //得到按下时的坐标
y1 = e.getY();
g.setColor(color); //设置线条粗细
g.setStroke(new BasicStroke(stroke)); //设置粗细,注意setStroke方法只在Graphics2D中有,在Graphics中没有
//所以你的画笔g的类型为Graphics2D
}
public void mouseReleased(MouseEvent e) { //鼠标(按下)释放时调用的方法
x2 = e.getX(); //得到释放时的坐标
y2 = e.getY();
//我这里是想将做需要的信息存到array数组中,在调用Shape中的draw方法
if(type.equals("直线")||type.equals("矩形")||type.equals("圆")||type.equals("3D矩形")||type.equals("文字Hello")||type.equals("等腰三角形")||type.equals("五角星")|type.equals("立方体")) {
Shape s = new Shape(x1,y1,x2,y2,color,stroke,type); //储存数据
if(index < 1000) //当储存的数据超过1000后将不再存储
array[index++] = s;
s.draw(g);
}else if(type.equals("图片")) {
Toolkit tool = this.getToolkit();
//这个this应该指向JFrame,JPanel,所以我在治理继承了JFrame类
Image image = tool.getImage("C:\\Users\\ASUS\\Desktop\\p2.png"); //给image赋值
Shape s = new Shape(x1,y1,x2,y2,color,stroke,type,image);
if(index < 1000)
array[index++] = s;
s.draw(g);
}else if(type.equals("任意多边形")){
if(sign) { //当此时画的是任意多边形的第一条线
fx = x1; fy = y1; //fx,fy记录起点的坐标,lx、ly记录上一个点的坐标
lx = x1; ly = y1;
Shape s = new Shape(lx,ly,x2,y2,color,stroke,type); //直接存储需要画的直线
if(index < 1000)
array[index++] = s;
s.draw(g);
sign = false; //sign = false 即接下来画的不是任意多边形的第一条线
}else { //画的不是任意多边形的第一条线
if(x2 > fx-9 && x2 < fx+9 && y2 > fy-9 && y2 < fy+9) { //判断此时释放的位置是不是任意多边形结束的位置
Shape s = new Shape(lx,ly,fx,fy,color,stroke,type);
if(index < 1000)
array[index++] = s;
s.draw(g);
sign = true;
}
else { //释放位置不是终止位置时
Shape s = new Shape(lx,ly,x2,y2,color,stroke,type);
if(index < 1000)
array[index++] = s;
s.draw(g);
}
}
lx = x2; ly = y2;
}
}
public void mouseEntered(MouseEvent e) { //鼠标进入是调用呃方法
}
public void mouseExited(MouseEvent e) { //鼠标离开时调用的方法
}
public void mouseDragged(MouseEvent e) { //鼠标拖动时调用的方法
if(type.equals("铅笔")) { //实现铅笔
x2 = e.getX();
y2 = e.getY();
Shape s = new Shape(x1,y1,x2,y2,color,stroke,type);
if(index < 1000)
array[index++] = s;
s.draw(g);
x1 = x2;
y1 = y2;
}else if(type.equals("喷枪")) { //实现喷枪
x2 = e.getX();
y2 = e.getY();
Shape s = new Shape(x2,y2,color,stroke,type);
if(index < 1000)
array[index++] = s;
s.draw(g);
}else if(type.equals("橡皮擦")) { //实现橡皮擦
x2 = e.getX();
y2 = e.getY();
Shape s = new Shape(x2,y2,color,stroke,type);
if(index < 1000)
array[index++] = s;
s.draw(g);
}
}
public void mouseMoved(MouseEvent e) { //鼠标移动是调用的方法
}
}
3. Shape 主要用于储存数据,实现重绘
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.Random;
import javax.swing.JFrame;
public class Shape extends JFrame{
public int x1,y1,x2,y2;
public Color color;
public float stroke;
public String type;
public Image img;
public Random rand = new Random();
/*
* 构造函数的重载
*/
public Shape(int x1,int y1,int x2,int y2,Color color,float strock,String type) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
this.stroke = strock;
this.type = type;
}
public Shape(int x1,int y1,int x2,int y2,Color color,float stroke,String type,Image img) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
this.stroke = stroke;
this.type = type;
this.img = img;
}
public Shape(int x2,int y2,Color color,float stroke,String type) {
this.x2 = x2;
this.y2 = y2;
this.color = color;
this.stroke = stroke;
this.type = type;
}
public void draw(Graphics2D g) { //画图
g.setColor(color);
g.setStroke(new BasicStroke(stroke));
if(type.equals("直线")) {
g.drawLine(x1, y1, x2, y2);
}
else if(type.equals("矩形")) {
MyDrawRect myDrawRect = new MyDrawRect(g);
myDrawRect.drawRect(x1, y1, x2, y2); //自定义画矩形的方法,以使之不必局限于只能往右下角画矩形
}
else if(type.equals("圆")) {
MyDrawCircle myDrawCircle = new MyDrawCircle(g);
myDrawCircle.drawCircle(x1, y1, x2, y2); //自定义画圆的方法,以使之不必局限于只能往右下角画圆
}
else if(type.equals("3D矩形")) {
MyDraw3DRect myDraw3DRect = new MyDraw3DRect(g);
myDraw3DRect.draw3DRect(x1, y1, x2, y2); //自定义画3D矩形的方法,以使之不必局限于只能往右下角画3D矩形
}else if(type.equals("图片")) {
MyDrawImag myDrawImage = new MyDrawImag(g);
myDrawImage.drawImage(x1,y1,x2,y2,img); //自定义画图片的方法,以使之不必局限于只能往右下角画图片
}else if(type.equals("文字Hello")) {
String str = "Hello";
g.drawString(str, x1, y1);
}else if(type.equals("等腰三角形")) {
g.drawLine((x1+x2)/2, y1, x1, y2);
g.drawLine(x1, y2, x2, y2);
g.drawLine((x1+x2)/2, y1, x2, y2);
}else if(type.equals("五角星")) {
MyDrawPentagram myDrawPentagram = new MyDrawPentagram(g);
myDrawPentagram.drawPentagram(x1, y1, x2, y2); //自定义画五角星的方法,以使之不必局限于只能往右下角画图片
}else if(type.equals("任意多边形")) {
g.drawLine(x1, y1, x2, y2); //因为在DrawingListener类中已经进行了相关数据的处理,所以这里只需画直线即可
}else if(type.equals("立方体")) {
MyDrawCube myDrawCube = new MyDrawCube(g);
myDrawCube.drawCube(x1,y1,x2,y2); //自定义画立方体的方法,以使之不必局限于只能往右下角画图片
}else if(type.equals("铅笔")) {
g.drawLine(x1, y1, x2, y2); //铅笔实际上就是在画直线而已,只不过两点之间的距离很短
}else if(type.equals("喷枪")) {
for(int num = 0;num < 25;num++) {
x1 = rand.nextInt(10)+x2-5; //在以(x2,y2)为中点,长为10的正方形中画点
y1 = rand.nextInt(10)+y2-5;
g.drawLine(x1,y1,x1,y2);
}
}else if(type.equals("橡皮擦")) {
g.clearRect(x2-2, y2-2, 15, 15); //用clearRect方法画与背景颜色一样的矩形
}
}
}
4. 画3D矩形
import java.awt.Graphics;
/*
* 画3D矩形
*/
public class MyDraw3DRect {
private Graphics g;
int x1,y1,x2,y2;
MyDraw3DRect(Graphics g){
this.g = g;
}
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void draw3DRect(int x1,int y1,int x2,int y2) {
setXY(x1,y1,x2,y2);
//区分x2,y2相对x1,y1的位置
if(x2 < x1 && y2 <y1) //左上
g.draw3DRect(x2, y2, x1-x2, y1-y2,true); //直接用draw3DRect()方法即可
else if(x2 > x1 && y2 < y1) //右上
g.draw3DRect(x1, y2, x2-x1, y1-y2,true);
else if(x2 < x1 && y2 > y1) //左下
g.draw3DRect(x2, y1, x1-x2, y2-y1,true);
else //右下
g.draw3DRect(x1, y1, x2-x1, y2-y1,true);
}
}
5. 画圆
import java.awt.Graphics;
public class MyDrawCircle {
private Graphics g;
int x1,y1,x2,y2;
int d;
StrictMath math;
MyDrawCircle(Graphics g){
this.g = g;
}
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void drawCircle(int x1,int y1,int x2,int y2) {
setXY(x1,y1,x2,y2);
if(x2 < x1 && y2 <y1) {
d = (int)math.sqrt(math.pow(x1-x2, 2)+math.pow(y1-y2, 2));
g.drawOval(((x1+x2)/2)-d/2, ((y1+y2)/2)-d/2, d, d);
}
else if(x2 > x1 && y2 < y1) {
d = (int)math.sqrt(math.pow(x2-x1, 2)+math.pow(y1-y2, 2));
g.drawOval(((x1+x2)/2)-d/2, ((y1+y2)/2)-d/2, d, d);
}
else if(x2 < x1 && y2 > y1) {
d = (int)math.sqrt(math.pow(x1-x2, 2)+math.pow(y2-y1, 2));
g.drawOval(((x1+x2)/2)-d/2, ((y1+y2)/2)-d/2, d, d);
}
else {
d = (int)math.sqrt(math.pow(x2-x1, 2)+math.pow(y2-y1, 2));
g.drawOval(((x1+x2)/2)-d/2, ((y1+y2)/2)-d/2, d, d);
}
}
}
6. 画立方体(其实此处画的哪里可以单独分出来为一个方法)
import java.awt.Graphics;
/*
* 画立方体
*/
public class MyDrawCube {
private Graphics g;
int x1,y1,x2,y2;
MyDrawCube(Graphics g){
this.g = g;
}
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
/*
* 画立方体的虚线
*/
public void drawDashedShu(int x1,int y1,int x2,int y2) { // 画竖虚线
g.drawLine(x1, y1, x1, y1+(y2-y1)/11);
g.drawLine(x1, y1+((y2-y1)*2)/11, x1, y1+((y2-y1)*3 )/11);
g.drawLine(x1, y1+((y2-y1)*4)/11, x1, y1+((y2-y1)*5)/11);
g.drawLine(x1, y1+((y2-y1)*6)/11, x1, y1+((y2-y1)*7)/11);
g.drawLine(x1, y1+((y2-y1)*8)/11, x1, y1+((y2-y1)*9)/11);
g.drawLine(x1, y1+((y2-y1)*10)/11, x1, y2);
}
public void drawDashedHeng(int x1,int y1,int x2,int y2) { //画横虚线
g.drawLine(x1, y1, x1+((x2-x1)*1)/11, y1);
g.drawLine(x1+((x2-x1)*2)/11, y1,x1+((x2-x1)*3)/11, y2);
g.drawLine(x1+((x2-x1)*4)/11, y1, x1+((x2-x1)*5)/11, y2);
g.drawLine(x1+((x2-x1)*6)/11, y1, x1+((x2-x1)*7)/11, y2);
g.drawLine(x1+((x2-x1)*8)/11, y1, x1+((x2-x1)*9)/11, y2);
g.drawLine(x1+((x2-x1)*10)/11, y1,x2, y2);
}
public void drawDashedXie(int x1,int y1,int x2,int y2) { //画斜虚线
g.drawLine(x1, y1, x1+((x2-x1)*1)/7, y2+((y1-y2)*6)/7);
g.drawLine(x1+((x2-x1)*2)/7, y2+((y1-y2)*5)/7, x1+((x2-x1)*3)/7, y2+((y1-y2)*4)/7);
g.drawLine(x1+((x2-x1)*4)/7, y2+((y1-y2)*3)/7, x1+((x2-x1)*5)/7, y2+((y1-y2)*2)/7);
g.drawLine(x1+((x2-x1)*6)/7, y2+((y1-y2)*1)/7, x2, y2);
}
public void drawCube(int x1,int y1,int x2,int y2) { //画立方体
setXY(x1,y1,x2,y2);
int w,h;
if(x2 < x1 && y2 <y1) { //左上角
w = (x1-x2)/4; h = (y1-y2)/4;
g.drawLine(x2+w, y2, x1, y2); //横
g.drawLine(x2, y2+h, x1-w, y2+h);
g.drawLine(x2, y1, x1-w, y1);
drawDashedHeng(x2+w,y1-h,x1,y1-h);
g.drawLine(x2, y2+h, x2, y1); //竖
g.drawLine(x1-w, y2+h, x1-w, y1);
g.drawLine(x1, y2, x1, y1-h);
drawDashedShu(x2+w,y2,x2+w,y1-h);
g.drawLine(x2, y2+h, x2+w, y2); //斜
g.drawLine(x1-w, y2+h, x1, y2);
g.drawLine(x1-w, y1, x1, y1-h);
drawDashedXie(x2,y1,x2+w,y1-h);
}
else if(x2 > x1 && y2 < y1) { //右上角
w = (x2-x1)/4; h = (y1-y2)/4;
g.drawLine(x1+w, y2, x2, y2);
g.drawLine(x1, y2+h, x2-w, y2+h);
g.drawLine(x1, y1, x2-w, y1);
drawDashedHeng(x1+w,y1-h,x2,y1-h);
g.drawLine(x1, y2+h, x1, y1);
g.drawLine(x2-w, y2+h, x2-w, y1);
g.drawLine(x2, y2, x2, y1-h);
drawDashedShu(x1+w,y2,x1+w,y1-h);
g.drawLine(x1, y2+h, x1+w, y2);
g.drawLine(x2-w, y2+h, x2, y2);
g.drawLine(x2-w, y1, x2, y1-h);
drawDashedXie(x1,y1,x1+w,y1-h);
}
else if(x2 < x1 && y2 > y1) { //左下角
w = (x1-x2)/4; h = (y2-y1)/4;
g.drawLine(x2+w, y1, x1, y1);
g.drawLine(x2, y1+h, x1-w, y1+h);
g.drawLine(x2, y2, x1-w, y2);
drawDashedHeng(x2+w,y2-h,x1,y2-h);
g.drawLine(x2, y1+h, x2, y2);
g.drawLine(x1-w, y1+h, x1-w, y2);
g.drawLine(x1, y1, x1, y2-h);
drawDashedShu(x2+w,y1,x2+w,y2-h);
g.drawLine(x2, y1+h, x2+w, y1);
g.drawLine(x1-w, y1+h, x1, y1);
g.drawLine(x1-w, y2, x1, y2-h);
drawDashedXie(x2,y2,x2+w,y2-h);
}
else { //右下角
w = (x2-x1)/4; h = (y2-y1)/4;
g.drawLine(x1+w, y1, x2, y1);
g.drawLine(x1, y1+h, x2-w, y1+h);
g.drawLine(x1, y2, x2-w, y2);
drawDashedHeng(x1+w,y2-h,x2,y2-h);
g.drawLine(x1, y1+h, x1, y2);
g.drawLine(x2-w, y1+h, x2-w, y2);
g.drawLine(x2, y1, x2, y2-h);
drawDashedShu(x1+w,y1,x1+w,y2-h);
g.drawLine(x1, y1+h, x1+w, y1);
g.drawLine(x2-w, y1+h, x2, y1);
g.drawLine(x2-w, y2, x2, y2-h);
drawDashedXie(x1,y2,x1+w,y2-h);
}
}
}
7. 画图
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
/*
* 画图
*/
public class MyDrawImag extends JFrame{
private Graphics g;
private int x1,y1,x2,y2;
private Observer observer;
//Image img = new Image("C:\\Users\\ASUS\\Desktop\\p2.png"); 是错误的,Image不能够由于实例化
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public MyDrawImag(Graphics g) {
this.g = g;
}
public void drawImage(int x1,int y1,int x2,int y2,Image image) {
setXY(x1,y1,x2,y2);
if(x2 < x1 && y2 <y1)
g.drawImage(image, x2, y2, x1-x2, y1-y2, observer);
else if(x2 > x1 && y2 < y1)
g.drawImage(image,x1, y2, x2-x1, y1-y2,observer);
else if(x2 < x1 && y2 > y1)
g.drawImage(image,x2, y1, x1-x2, y2-y1,observer);
else
g.drawImage(image,x1, y1, x2-x1, y2-y1,observer);
}
}
8. 画五角星
import java.awt.Graphics;
/*
* 画五角星
*/
public class MyDrawPentagram {
private Graphics g;
int x1,y1,x2,y2;
int w,h; //记录宽,高
int a1X,a1Y,a2X,a2Y,a3X,a3Y,a4X,a4Y,a5X,a5Y,a6X,a6Y,a7X,a7Y,a8X,a8Y,a9X,a9Y,a10X,a10Y; //记录五角星的每个角
MyDrawPentagram(Graphics g){
this.g = g;
}
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void Line() {
g.drawLine(a1X, a1Y, a2X,a2Y);
g.drawLine(a2X, a2Y, a3X,a3Y);
g.drawLine(a3X, a3Y, a4X,a4Y);
g.drawLine(a4X, a4Y, a5X,a5Y);
g.drawLine(a5X, a5Y, a6X,a6Y);
g.drawLine(a6X, a6Y, a7X,a7Y);
g.drawLine(a7X, a7Y, a8X,a8Y);
g.drawLine(a8X, a8Y, a9X,a9Y);
g.drawLine(a9X, a9Y, a10X,a10Y);
g.drawLine(a10X, a10Y, a1X,a1Y);
}
public void drawPentagram(int x1,int y1,int x2,int y2) {
setXY(x1,y1,x2,y2);
if(x2 < x1 && y2 <y1) {
w = x1-x2; h = y1-y2;
a1X = x2+w/2; a1Y = y2;
a2X = x2+(w*3)/5; a2Y = y2+(h*2)/5;
a3X = x1; a3Y = y2+(h*2)/5;
a4X = x2+(w*2)/3; a4Y = y2+(h*2)/3;
a5X = x2+(w*4)/5; a5Y = y1;
a6X = x2+w/2; a6Y = y2+(h*4)/5;
a7X = x2+w/5; a7Y = y1;
a8X = x2+w/3; a8Y = y2+(h*2)/3;
a9X = x2; a9Y = y2+(h*2)/5;
a10X = x2+(w*2)/5; a10Y = y2+(h*2)/5;
Line();
}
else if(x2 > x1 && y2 < y1) {
w = x2-x1; h = y1-y2;
a1X = x1+w/2; a1Y = y2;
a2X = x1+(w*3)/5; a2Y = y2+(h*2)/5;
a3X = x2; a3Y = y2+(h*2)/5;
a4X = x1+(w*2)/3; a4Y = y2+(h*2)/3;
a5X = x1+(w*4)/5; a5Y = y1;
a6X = x1+w/2; a6Y = y2+(h*4)/5;
a7X = x1+w/5; a7Y = y1;
a8X = x1+w/3; a8Y = y2+(h*2)/3;
a9X = x1; a9Y = y2+(h*2)/5;
a10X = x1+(w*2)/5; a10Y = y2+(h*2)/5;
Line();
}
else if(x2 < x1 && y2 > y1) {
w = x1-x2; h = y2-y1;
a1X = x2+w/2; a1Y = y1;
a2X = x2+(w*3)/5; a2Y = y1+(h*2)/5;
a3X = x1; a3Y = y1+(h*2)/5;
a4X = x2+(w*2)/3; a4Y = y1+(h*2)/3;
a5X = x2+(w*4)/5; a5Y = y2;
a6X = x2+w/2; a6Y = y1+(h*4)/5;
a7X = x2+w/5; a7Y = y2;
a8X = x2+w/3; a8Y = y1+(h*2)/3;
a9X = x2; a9Y = y1+(h*2)/5;
a10X = x2+(w*2)/5; a10Y = y1+(h*2)/5;
Line();
}
else {
w = x2-x1; h = y2-y1;
a1X = x1+w/2; a1Y = y1;
a2X = x1+(w*3)/5; a2Y = y1+(h*2)/5;
a3X = x2; a3Y = y1+(h*2)/5;
a4X = x1+(w*2)/3; a4Y = y1+(h*2)/3;
a5X = x1+(w*4)/5; a5Y = y2;
a6X = x1+w/2; a6Y = y1+(h*4)/5;
a7X = x1+w/5; a7Y = y2;
a8X = x1+w/3; a8Y = y1+(h*2)/3;
a9X = x1; a9Y = y1+(h*2)/5;
a10X = x1+(w*2)/5; a10Y = y1+(h*2)/5;
Line();
}
}
}
9.画矩形
import java.awt.Graphics;
/*
* 画矩形
*/
public class MyDrawRect {
private Graphics g;
int x1,y1,x2,y2;
MyDrawRect(Graphics g){
this.g = g;
}
public void setXY(int x1,int y1,int x2,int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void drawRect(int x1,int y1,int x2,int y2) {
setXY(x1,y1,x2,y2);
if(x2 < x1 && y2 <y1)
g.drawRect(x2, y2, x1-x2, y1-y2);
else if(x2 > x1 && y2 < y1)
g.drawRect(x1, y2, x2-x1, y1-y2);
else if(x2 < x1 && y2 > y1)
g.drawRect(x2, y1, x1-x2, y2-y1);
else
g.drawRect(x1, y1, x2-x1, y2-y1);
}
}