📄 drawpad.java
字号:
break;
case 6:
itemList[index]=new Oval();
break;
case 7:
itemList[index]=new fillOval();
break;
case 8:
itemList[index]=new Circle();
break;
case 9:
itemList[index]=new fillCircle();
break;
case 10:
itemList[index]=new RoundRect();
break;
case 11:
itemList[index]=new fillRoundRect();
break;
case 12:
itemList[index]=new Triangle();
break;
case 13:
itemList[index]=new Rubber();
break;
}
itemList[index].R=R;
itemList[index].G=G;
itemList[index].B=B;
itemList[index].stroke=stroke;
}
//保存图形文件程序段
public void saveFile()
{
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result =fileChooser.showSaveDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
return ;
File fileName=fileChooser.getSelectedFile();
fileName.canWrite();
if (fileName==null||fileName.getName().equals(""))
JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE);
else{
try {
fileName.delete();
FileOutputStream fos=new FileOutputStream(fileName);
output=new ObjectOutputStream(fos);
drawings record;
output.writeInt( index );
for(int i=0;i< index ;i++)
{
drawings p= itemList[ i ] ;
output.writeObject(p);
output.flush(); //将所有图形信息强制转换成父类线性化存储到文件中
}
output.close();
fos.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
//打开一个图形文件程序段
public void loadFile()
{
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result =fileChooser.showOpenDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
return ;
File fileName=fileChooser.getSelectedFile();
fileName.canRead();
if (fileName==null||fileName.getName().equals(""))
JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE);
else {
try {
FileInputStream fis=new FileInputStream(fileName);
input=new ObjectInputStream(fis);
drawings inputRecord;
int countNumber=0;
countNumber=input.readInt();
for(index=0;index< countNumber ;index++)
{
inputRecord=(drawings)input.readObject();
itemList[ index ] = inputRecord ;
}
createNewItem();
input.close();
repaint();
}
catch(EOFException endofFileException){
JOptionPane.showMessageDialog(this,"no more record in file",
"class not found",JOptionPane.ERROR_MESSAGE );
}
catch(ClassNotFoundException classNotFoundException){
JOptionPane.showMessageDialog(this,"Unable to Create Object",
"end of file",JOptionPane.ERROR_MESSAGE );
}
catch (IOException ioException){
JOptionPane.showMessageDialog(this,"error during read from file",
"read Error",JOptionPane.ERROR_MESSAGE );
}
}
}
//新建一个文件程序段
public void newFile()
{
index=0;
currentChoice=0;
color=Color.black;
//stroke=1.0f;
createNewItem();
repaint();//将有关值设置为初始状态,并且重画
}
//选择当前颜色程序段
public void chooseColor()
{
color=JColorChooser.showDialog(DrawPad.this,
"Choose a color",color);
R=color.getRed();
G=color.getGreen();
B=color.getBlue();
itemList[index].R=R;
itemList[index].G=G;
itemList[index].B=B;
}
//选择线条粗细程序段
public void setStroke()
{
String input;
input=JOptionPane.showInputDialog("Input a stroke value(>0):");
stroke=Integer.parseInt(input);
itemList[index].stroke=stroke;
}
//用主函数main()来测试
public static void main(String s[]){
DrawPad example = new DrawPad();
example.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}});
}
}
//定义画图的基本图形单元
class drawings implements Serializable//父类,基本图形单元,用到串行化接口,保存时所用
{
int x1,y1,x2,y2; //定义坐标属性
int R,G,B; //定义色彩属性
int stroke=1; //定义笔画粗细属性
void draw(Graphics2D g2d){};//定义绘图函数
}
//各种基本图形单元的子类,都继承父类drawings,
class Pencil extends drawings//随笔画类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.drawLine(x1,y1,x2,y2);
}
}
class Line extends drawings //直线类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.drawLine(x1,y1,x2,y2);
}
}
class Rect extends drawings//空心矩形类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.drawRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2));
}
}
class fillRect extends drawings//实心矩形类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.fillRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2));
}
}
class Square extends Rect//空心正方形类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.drawRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(x1-x2));
}
}
class fillSquare extends fillRect//实心正方形类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.fillRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(x1-x2));
}
}
class Oval extends drawings//空心椭圆类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.drawOval(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2));
}
}
class fillOval extends drawings//实心椭圆
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.fillOval(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2));
}
}
class Circle extends drawings//空心圆类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.drawOval(Math.min(x1,x2),Math.min(y1,y2),
Math.max(Math.abs(x1-x2),Math.abs(y1-y2)),
Math.max(Math.abs(x1-x2),Math.abs(y1-y2))
);
}
}
class fillCircle extends drawings//实心圆
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.fillOval(Math.min(x1,x2),Math.min(y1,y2),
Math.max(Math.abs(x1-x2),Math.abs(y1-y2)),
Math.max(Math.abs(x1-x2),Math.abs(y1-y2))
);
}
}
class RoundRect extends drawings//空心圆角矩形类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.drawRoundRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2),
50,35);
}
}
class fillRoundRect extends drawings//实心圆角矩形类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.fillRoundRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2),
50,35);
}
}
class Triangle extends drawings//空心直角三角形类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.drawLine(x1,y1,x2,y1);
g2d.drawLine(x1,y1,x1,y2);
g2d.drawLine(x1,y2,x2,y1);
}
}
class Rubber extends drawings//橡皮擦类
{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(255,255,255));
g2d.setStroke(new BasicStroke(10,
BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));
g2d.drawLine(x1,y1,x2,y2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -