Java画图板程序设计报告(5)

2019-08-03 10:29

repaint(); }

public void mouseMoved(MouseEvent e)

{statusBar.setText(\ \ }

//选择字体风格时候用到的事件侦听器类,加入到字体风格的选择框中 private class checkBoxHandler implements ItemListener {

public void itemStateChanged(ItemEvent e) {

if(e.getSource()==bold)

if(e.getStateChange()==ItemEvent.SELECTED) f1=Font.BOLD; else

f1=Font.PLAIN;

if(e.getSource()==italic)

if(e.getStateChange()==ItemEvent.SELECTED) f2=Font.ITALIC; else

f2=Font.PLAIN; } }

//画图面板类,用来画图

class DrawPanel extends JPanel {

public DrawPanel() {

setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); setBackground(Color.white); addMouseListener(new mouseA()); addMouseMotionListener(new mouseB()); }

public void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d=(Graphics2D)g; //定义画笔 int j=0;

while (j<=index) {

draw(g2d,itemList[j]);

21 / 31

j++; }

}

void draw(Graphics2D g2d,drawings i) {

i.draw(g2d);//将画笔传入到各个子类中,用来完成各自的绘图 } }

//新建一个画图基本单元对象的程序段 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:

22 / 31

itemList[index]=new RoundRect(); break; case 12:

itemList[index]=new fillRoundRect(); break; case 13:

itemList[index]=new Rubber(); break; case 14:

itemList[index]=new Word(); break; }

itemList[index].type=currentChoice; itemList[index].R=R; itemList[index].G=G; itemList[index].B=B;

itemList[index].stroke=stroke; }

//选择当前颜色程序段

public void chooseColor() {

color=JColorChooser.showDialog(MiniDrawPad.this, \ 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(

\ stroke=Float.parseFloat(input); itemList[index].stroke=stroke; }

//保存图形文件程序段

public void saveFile() {

23 / 31

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,\ \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 ;

24 / 31

File fileName=fileChooser.getSelectedFile(); fileName.canRead();

if (fileName==null||fileName.getName().equals(\

JOptionPane.showMessageDialog(fileChooser,\ \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,\ \ERROR_MESSAGE ); }

catch(ClassNotFoundException classNotFoundException){ JOptionPane.showMessageDialog(this,\Object\

\ERROR_MESSAGE ); }

catch (IOException ioException){

JOptionPane.showMessageDialog(this,\file\

\ERROR_MESSAGE ); } } }

//新建一个文件程序段

25 / 31


Java画图板程序设计报告(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:《喂自己影子吃饭的人》详细解析

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: