📄 huaban.java
字号:
class mouseB extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{statusBar.setText(" 鼠标在绘画面板的移动位置:["
+ e.getX() + ", " + e.getY() + "]");
if(currentChoice==3||currentChoice==8)
{
itemList[index-1].x1=itemList[index].x2=itemList[index].x1=e.getX();
itemList[index-1].y1=itemList[index].y2=itemList[index].y1=e.getY();
index++;
createNewItem();
}
else { itemList[index].x2=e.getX();
itemList[index].y2=e.getY();
}
repaint();
}
public void mouseMoved(MouseEvent e)
{statusBar.setText(" 鼠标在绘画面板的位置:["
+ e.getX() + ", " + e.getY() + "]");
}
}
//选择字体风格时候用到的事件侦听器类,加入到字体风格的选择框中
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]);
j++;
}
}
void draw(Graphics2D g2d,drawings i)
{ i.draw(g2d);} //用来完成各子类的绘图
}
//新建一个画图基本单元对象的程序段
void createNewItem()
{ if(currentChoice==9) 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 Oval(); break;
case 7:itemList[index]=new Circle(); break;
case 8:itemList[index]=new Rubber();break;
case 9:itemList[index]=new Word();break;
case 11:itemList[index]=new fillRect();break;
case 12:itemList[index]=new fillOval();break;
case 13:itemList[index]=new fillCircle();break;
}
itemList[index].type=currentChoice;
itemList[index].R=R;
itemList[index].G=G;
itemList[index].B=B;
itemList[index].brush=brush;
}
//选择当前颜色程序段
public void chooseColor()
{ color=JColorChooser.showDialog(Huaban.this,
"选择你所需要的颜色",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(
"请输入你想要的粗度数字! ( 默认是1)");
brush=Float.parseFloat(input);
itemList[index].brush=brush;
}
//新建一个文件程序段
public void newFile()
{ index=0; currentChoice=3;
color=Color.black; brush=1.0f;
createNewItem();
repaint();//将有关值设置为初始状态,并且重画
}
//保存图形文件程序段
public void saveFile()
{
JFileChooser filesave=new JFileChooser();
filesave.setFileSelectionMode(JFileChooser.FILES_ONLY);
if(filesave.showSaveDialog(this)==JFileChooser.CANCEL_OPTION)return ;
File fileName=filesave.getSelectedFile();
fileName.canWrite();
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(Exception ioe){}
}
//打开一个图形文件程序段
public void openFile()
{
JFileChooser fileopen=new JFileChooser();
fileopen.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result =fileopen.showOpenDialog(this);
if(result==JFileChooser.CANCEL_OPTION)return ;
File fileName=fileopen.getSelectedFile();
fileName.canRead();
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(Exception e){JOptionPane.showMessageDialog(this,"读取文件格式错误",
"读取错误",JOptionPane.ERROR_MESSAGE );}
}
//主函数段
public static void main(String args[])
{try {
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
}
catch ( Exception e ) {}//将界面设置为当前windows风格
Huaban newPad=new Huaban();
newPad.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}});
}
}
//定义画图的基本图形单元
class drawings implements Serializable//父类,基本图形单元,用到串行化接口,保存时所用
{
int x1,y1,x2,y2; //坐标属性
int R,G,B;
float brush; //定义线条粗细属性
int type; //定义字体属性
String s1;
String s2; //定义字体风格属性
void draw(Graphics2D g2d){};//定义绘图函数
}
// 下面是各种基本图形单元的子类,都继承自父类drawings
class Line extends drawings //直线类
{ void draw(Graphics2D g2d)
{ g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(brush,
BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));
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(brush));
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.setStroke(new BasicStroke(brush));
g2d.fillRect(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(x1-x2),Math.abs(y1-y2));
}
}
class Oval extends drawings//椭圆类
{ void draw(Graphics2D g2d)
{ g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(brush));
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.setStroke(new BasicStroke(brush));
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(brush));
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.setStroke(new BasicStroke(brush));
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 Pencil extends drawings//随笔画类
{ void draw(Graphics2D g2d)
{ g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(brush,
BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));
g2d.drawLine(x1,y1,x2,y2);
}
}
class Rubber extends drawings//橡皮擦类
{ void draw(Graphics2D g2d)
{ g2d.setPaint(new Color(255,255,255));
g2d.setStroke(new BasicStroke(brush+4,
BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));
g2d.drawLine(x1,y1,x2,y2);
}
}
class Word extends drawings//输入文字类
{ void draw(Graphics2D g2d)
{ g2d.setPaint(new Color(R,G,B));
g2d.setFont(new Font(s2,x2+y2,((int)brush)*18));
if (s1!= null )
g2d.drawString(s1,x1,y1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -