package lab14;
import java.awt.*;
import javax.swing.*;
public class Paint extends JPanel{
private int[] type = new int[100];
private int[] colorType = new int[100];
private int[] xStart = new int[100];
private int[] yStart = new int[100];
private int[][] xEnd = new int[100][10000];
private int[][] yEnd = new int[100][10000];
private int[] num = new int[100]; //每次画图实际经过的点数(除了开始那点外)
private int[] x; //用于curve
private int[] y; //用于curve
private int count,count1; //前者记录清空后已画图形数目。后者记录某一次画的瞬间已经过的点数(除了开始点)
private boolean isClear = false; //用于判断是否要清空。
public Paint(){
}
void setColor(int number){
switch(number){
case 1:
setForeground(Color.red);
break;
case 2:
setForeground(Color.blue);
break;
case 0:
setForeground(Color.black);
}
}
void setType(int type, int colorType){
count++;
if ((count) >= this.type.length){
this.type = expand(this.type);
this.colorType = expand(this.colorType);
xStart = expand(xStart);
yStart = expand(yStart);
xEnd = expand(xEnd);
yEnd = expand(yEnd);
num = expand(num);
}
this.type[count] = type;
this.colorType[count] = colorType;
}
void setLocation1(int x1, int y1){
count1 = 0;
if (type[count] !=4){
this.xStart[count] = x1;
this.yStart[count] = y1;
}
else{
xEnd[count][0] = x1;
yEnd[count][0] = y1;
}
}
void setLocation2(int x2, int y2){
count1++;
num[count] = count1;
if (count1 >= xEnd[0].length){
xEnd = expand(xEnd);
yEnd = expand(yEnd);
}
xEnd[count][count1] = x2;
yEnd[count][count1] = y2;
}
void setIsClear(boolean isClear){
this.isClear = isClear;
}
void undo(){
if (count >= 1)
count--;
else
return;
}
void redo(){
if(count < type.length && count >= 0)
count++;
else
return;
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if (isClear){
return;
}
else{
for (int i = 1;i <= count;i++){
setColor(colorType[i]);
g.setColor(getForeground());
int j = num[i];
int xTemper = (xEnd[i][j] - xStart[i]) > 0? (xEnd[i][j] - xStart[i]): Math.abs(xEnd[i][j] - xStart[i]);
int yTemper = (yEnd[i][j] - yStart[i]) > 0? (yEnd[i][j] - yStart[i]): Math.abs(yEnd[i][j] - yStart[i]);
switch (type[i]){
case 1:
g.drawLine(xStart[i], yStart[i], xEnd[i][j], yEnd[i][j]);
break;
case 2:
if(xEnd[i][j] - xStart[i] > 0)
g.drawRect(xStart[i], yStart[i], xTemper, yTemper);
else
g.drawRect(xEnd[i][j], yStart[i], xTemper, yTemper);
break;
case 3:
if(xEnd[i][j] - xStart[i] > 0)
g.drawOval(xStart[i], yStart[i], xTemper, yTemper);
else
g.drawOval(xEnd[i][j], yStart[i], xTemper, yTemper);
break;
case 4:
convert(i , j);
g.drawPolyline(x, y, x.length);
}
}
}
}
void convert(int number1, int number2){
x = new int[number2 + 1];
y = new int[number2 + 1];
for(int i=0;i <= number2;i++){
x[i] = xEnd[number1][i];
y[i] = yEnd[number1][i];
}
}
void clear(){
for (int i = 0;i <= count; i++){
xStart[i] = 0;
yStart[i] = 0;
for (int j = 0; j <= num[i]; j++){
xEnd[i][j] = 0;
yEnd[i][j] = 0;
}
num[i] = 0;
}
count = 0;
count1 = 0;
isClear = false;
}
int[] expand(int[] list){
int number = list.length;
int[] temper = new int[2 * number];
for (int i = 0; i < number; i++){
temper[i] = list[i];
}
return temper;
}
int[][] expand(int[][] list){
int number1 = list.length;
int number2 = list[0].length;
int[][] temper = new int[2 * number1][2 * number2];
for (int i = 0; i < number1; i++){
for (int j = 0; j < number2; j++){
temper[i][j] = list[i][j];
}
}
return temper;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页