📄 minidrawpad.java
字号:
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("Mouse Moved@:["+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==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;
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,
"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(
"Please input a float stroke value!(>0)");
stroke=Float.parseFloat(input);
itemList[index].stroke=stroke;
}
public void saveFile()
{
JFileChooser fileChoose=new JFileChoose();
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(indx);
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.anRead();
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 inoutRecord;
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(ClassNotFoundPaneException classNotFoundException){
JOptionPane.showMessageDialog(this,"Unable to Create Object","end of file",JOption.ERROR_MESSAGE);
}
catch(IOException ioException){
JOptionPane.showMessageDialog(this,"error during read from file","read Error",JOptionPane.ERROR_MESSAGE);
}
}
}
public void newFile()
{
index=0;
currentChoice=3;
color=Color.black;
stroke=1.0f;
createNewItem();
repaint();
}
public static void main(String args[])
{try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e ){}
MiniDrawPad newPad=new MiniDrawPad();
newPad.addWindowListener(
new WindowAdapter(){
public void windowCloseing(WindowEvent e)
{System.exit(0);}});
}
}
class drawings implements Serializable
{
int x1,y1,x2,y2;
int R,G,B;
float stroke;
int type;
String s1;
String s2;
void draw(Graphics2D g2d){};
}
class Line extends drawings
{
void draw(Graphics2D g2d)
{g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_POUND,BAsicStroke.JOIN_BEVEL));
ged.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.setStroke(new BasicStroke(stroke));
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(stroke));
g2d.drawOval(Math.min(x1,x2),Math.min(y1,y2),
Math.abs(y1-y2));
}
}
class fillOval extends drawings
{
void draw(Graphics2D g2d)
{g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
g2d.fillOval(Math.min(x1,x2),Math.min(y1,y2),
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.setStroke(new BasicStroke(stroke));
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.setStroke(new BasicStroke(stroke));
g2d.fillRoundRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2),50,35);
}
}
class Pencil extends drawings
{
void draw(Graphics2D g2d)
{g2d.setPaint(new Color(R,G,N));
g2d.setStroke(new BasicStroke(stroke,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(stroke+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)stroke)*18));
if(s1!=null)
g2d.drawString(s1,x1,y1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -