Java画图板程序设计报告(5)
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 ); …… 此处隐藏:2083字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [资格考试]机械振动与噪声学部分答案
- [资格考试]空调工程课后思考题部分整合版
- [资格考试]电信登高模拟试题
- [资格考试]2018年上海市徐汇区中考物理二模试卷(
- [资格考试]坐标转换及方里网的相关问题(椭球体、
- [资格考试]语文教研组活动记录表
- [资格考试]广东省2006年高应变考试试题
- [资格考试]LTE学习总结—后台操作-数据配置步骤很
- [资格考试]北京市医疗美容主诊医师和外籍整形外科
- [资格考试]中学生广播稿400字3篇
- [资格考试]CL800双模站点CDMA主分集RSSI差异过大
- [资格考试]泵与泵站考试复习题
- [资格考试]4个万能和弦搞定尤克里里即兴弹唱(入
- [资格考试]咽喉与经络的关系
- [资格考试]《云南省国家通用语言文字条例》学习心
- [资格考试]标准化第三范式
- [资格考试]GB-50016-2014-建筑设计防火规范2018修
- [资格考试]五年级上册品社复习资料(第二单元)
- [资格考试]2.对XX公司领导班子和班子成员意见建议
- [资格考试]关于市区违法建设情况的调研报告
- 二0一五年下半年经营管理目标考核方案
- 2014年春八年级英语下第三次月考
- 北师大版语文二年级上册第十五单元《松
- 2016国网江苏省电力公司招聘高校毕业生
- 多渠道促家长督导家长共育和谐 - 图文
- 2018 - 2019学年高中数学第2章圆锥曲线
- 竞争比合作更重要( - 辩论准备稿)课
- “案例积淀式”校本研训的实践与探索
- 新闻必须客观vs新闻不必客观一辩稿
- 福师大作业 比较视野下的外国文学
- 新编大学英语第二册1-7单元课文翻译及
- 年产13万吨天然气蛋白项目可行性研究报
- 河南省洛阳市2018届高三第二次统一考试
- 地下车库建筑设计探讨
- 南京大学应用学科教授研究方向汇编
- 2018年八年级物理全册 第6章 第4节 来
- 毕业论文-浅析余华小说的悲悯性 - 以《
- 2019年整理乡镇城乡环境综合治理工作总
- 广西民族大学留学生招生简章越南语版本
- 故宫旧称紫禁城简介




