📄 drawplat.java
字号:
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for(int j = 3; j < choices.length; j++) {
if(e.getSource() == choices[j]) {
currentChoice = j;
createNewItem();
repaint();
}
}
}
}
//按钮侦听器ButtonHandler1类,用来侦听颜色选择,画笔粗细设置,文字输入按钮的操作
public class ButtonHandler1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == choices[choices.length-2])
{
System.out.println("chooseColor");
chooseColor();}
if(e.getSource() == choices[choices.length-1])
{
System.out.println("stroke");
setStroke();}
}
}
//新建一个画图基本单元对象的程序段
void createNewItem() {
if(currentChoice == 14) // 进行相应的游标设置
drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
else
drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
switch(currentChoice) {
case 3:
itemList[index] = new Pencil();
break;
case 4:
itemList[index] = new Line();
break;
case 5:
itemList[index] = new Rect();
break;
case 6:
itemList[index] = new fillRect();
break;
case 7:
itemList[index] = new Oval();
break;
case 8:
itemList[index] = new fillOval();
break;
case 9:
itemList[index] = new Circle();
break;
case 10:
itemList[index] = new fillCircle();
break;
case 11:
itemList[index] = new RoundRect();
break;
case 12:
itemList[index] = new fillRoundRect();
break;
case 13:
itemList[index] = new Rubber();
break;
}
itemList[index].type = currentChoice;
itemList[index].R = R;
itemList[index].G = G;
itemList[index].B = B;
itemList[index].stroke = stroke;
}//createNewItem()
//选择当前颜色程序段
public void chooseColor() {
color = JColorChooser.showDialog(DrawPlat.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;
} //chooseColor
//选择当前线条粗细程度段
public void setStroke() {
String input;
input = JOptionPane.showInputDialog(
"Please input a float stroke value!( > 0 )"
);
stroke = Float.parseFloat(input);
itemList[index].stroke = stroke;
}
//保存图形文件程序段,saveFile()函数通过建立类FileOutputStream对象保存文件
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(itemList.length);
for(int i = 0; i < itemList.length; i++ ) {
record = itemList[i];
output.writeObject(record);
output.flush(); //将所有图形信息强制转换成父类线性化存储到文件中
}//for
output.close();
fos.close();
} catch(IOException ioe) {
ioe.printStackTrace();
} //catch
} // else
} //saveFile()
//打开一个图形文件程序段,loadFile函数通过建立FileInputStream对象读入文件
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;
}//for
// createNewItem();
input.close();
repaint();
}catch(EOFException endofFileException) {
JOptionPane.showMessageDialog(this,
"no more record in file",
"class not found",
JOptionPane.ERROR_MESSAGE);
} //catch endofFileException
catch(ClassNotFoundException classNotFoundException) {
JOptionPane.showMessageDialog(this,
"Unable to Create Object",
"End of file",
JOptionPane.ERROR_MESSAGE);
} //catch classNotFoundException
catch(IOException ioException) {
JOptionPane.showMessageDialog(this,
"error during read from file",
"read Error",
JOptionPane.ERROR_MESSAGE);
}
}
}//loadFile
//新建一个文件程序段
public void newFile() {
index = 0;
currentChoice = 3 ;
color = Color.black;
stroke = 1.0f;
createNewItem();
repaint(); //将有关值设置为初始状态,并且重画
}
public void update(Graphics g) {
if(offScreenImage == null ) {
offScreenImage = this.createImage(width , height);
}
Graphics gOffScreen = offScreenImage.getGraphics(); //虚拟背景画的画笔
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.white);
gOffScreen.fillRect(0, 100,width , height);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
/*
public void repaint() {
Graphics2D g2d = (Graphics2D)this.getGraphics();
g2d.setColor(Color.white);
g2d.drawRect(0, 100, width, height);
int j = 0;
int i = 0;
if(itemList.length >= 0 )
while(j < itemList.length-1) {
if( j == records[i]) {
itemList[records[i]].draw(g2d);
i++;
j++;
continue;
}
itemList[j].draw(g2d);
j++;
}
}//repaint
*/
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g; //定义画笔
g2d.setBackground(Color.white);
g2d.setColor(Color.white);
g2d.drawRect(0, 100, width, height);
int j = 0;
int i = 0;
if(itemList.length >= 0 )
while(j < itemList.length-1) {
if(records.length >= 0) {
itemList[records[i]].draw(g2d);
i++;
j++;
continue;
}
}
}
//主程序段
public static void main(String[] args) {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
DrawPlat newPad = new DrawPlat(); //创建MiniDrawPad实例
newPad.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
} catch(Exception e) {
e.printStackTrace();
}
}
} //DrawPlat
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -