📄 drawtest.txt
字号:
int l=in.readInt();
int r=in.readInt();
int e=in.readInt();
for(int k=0;k<l;k++)
{
try
{
LineShape g=(LineShape) in.readObject();
SimplePanel.line.add(g);
}
catch(IOException m) {}
}
for(int k=0;k<r;k++)
{
try
{
RectShape g=(RectShape) in.readObject();
SimplePanel.rect.add(g);
}
catch(IOException m) {}
catch(ClassNotFoundException n) {}
}
for(int k=0;k<e;k++)
{
try
{
ElliShape g=(ElliShape) in.readObject();
SimplePanel.elli.add(g);
}
catch(IOException m) {}
}
}
}
///////////////////////////////////////////////////////////////////////////////////
//file filter class
class ExtensionFileFilter extends FileFilter
{
private String description = "";
private ArrayList<String> extensions = new ArrayList<String>();
public void addExtension(String extension)
{
if (!extension.startsWith("."))
extension = "." + extension;
extensions.add(extension.toLowerCase());
}
public void setDescription(String aDescription)
{
description = aDescription;
}
public String getDescription()
{
return description;
}
public boolean accept(File f)
{
if (f.isDirectory()) return true;
String name = f.getName().toLowerCase();
for (String extension : extensions)
if (name.endsWith(extension))
return true;
return false;
}
}
///////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------
//panel class
class SimplePanel extends JPanel
{
public static ArrayList<LineShape> line;
public static ArrayList<RectShape> rect;
public static ArrayList<ElliShape> elli;
private Point2D begin=null;
private Point2D end=null;
private static int vernier=0;
private static int[] a=new int[100];
public SimplePanel()
{
line=new ArrayList<LineShape>();
rect=new ArrayList<RectShape>();
elli=new ArrayList<ElliShape>();
for(int m=0;m<100;m++)
a[m]=0;
addMouseListener(new MouseAction());
addMouseMotionListener(new MouseMotionAction());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D) g;
for(LineShape l:line)
{
g2.setPaint(l.getColor());
g2.draw(l.getLine());
}
for(RectShape r:rect)
{
g2.setPaint(r.getColor());
g2.draw(r.getRect());
}
for(ElliShape e:elli)
{
g2.setPaint(e.getColor());
g2.draw(e.getElli());
}
}
public void addLine(Point2D p1,Point2D p2)
{
if(p1!=null && p2!=null)
{
Line2D l=new Line2D.Double(p1,p2);
LineShape lineShape=new LineShape(l,SimpleFrame.getColorFlag());
line.add(lineShape);
if(vernier==99)
{
a[vernier]=SimpleFrame.getFlag();
vernier=0;
}
else
{
a[vernier]=SimpleFrame.getFlag();
vernier++;
}
repaint();
}
begin=null;
end=null;
}
public void addRect(Point2D p1,Point2D p2)
{
if(p1!=null && p2!=null)
{
double leftX=p1.getX();
double leftY=p1.getY();
double width=p2.getX()-p1.getX();
double height=p2.getY()-p1.getY();
Rectangle2D l=new Rectangle2D.Double();
if(p2.getX()>p1.getX() && p2.getY()>p1.getY())
{
l=new Rectangle2D.Double(leftX,leftY,width,height);
}
if(p2.getX()<p1.getX() && p2.getY()<p1.getY())
{
l=new Rectangle2D.Double(p2.getX(),p2.getY(),-width,-height);
}
if(p2.getX()>p1.getX() && p2.getY()<p1.getY())
{
l=new Rectangle2D.Double(p1.getX(),p2.getY(),width,-height);
}
if(p2.getX()<p1.getX() && p2.getY()>p1.getY())
{
l=new Rectangle2D.Double(p2.getX(),p1.getY(),-width,height);
}
RectShape rectShape=new RectShape(l,SimpleFrame.getColorFlag());
rect.add(rectShape);
if(vernier==99)
{
a[vernier]=SimpleFrame.getFlag();
vernier=0;
}
else
{
a[vernier]=SimpleFrame.getFlag();
vernier++;
}
repaint();
}
begin=null;
end=null;
}
public void addElli(Point2D p1,Point2D p2)
{
if(p1!=null && p2!=null)
{
Point2D center=new Point2D.Double((p2.getX()+p1.getX())/2,(p2.getY()+p1.getY())/2);
Ellipse2D e=new Ellipse2D.Double();
e.setFrameFromCenter(center,p1);
ElliShape elliShape=new ElliShape(e,SimpleFrame.getColorFlag());
elli.add(elliShape);
if(vernier==99)
{
a[vernier]=SimpleFrame.getFlag();
vernier=0;
}
else
{
a[vernier]=SimpleFrame.getFlag();
vernier++;
}
repaint();
}
begin=null;
end=null;
}
public static void clean()
{
for(int i=0;i<100;i++)
a[i]=0;
vernier=0;
line.clear();
rect.clear();
elli.clear();
}
public static void removeLast()
{
int i;
if(vernier==0)
vernier=99;
else
vernier--;
if(a[vernier]==0)
{
if(vernier==99)
vernier=0;
else
vernier++;
return;
}
else
{
if(a[vernier]==1)
{
i=line.size();
line.remove(i-1);
a[vernier]=0;
}
if(a[vernier]==2)
{
i=rect.size();
rect.remove(i-1);
a[vernier]=0;
}
if(a[vernier]==3)
{
i=elli.size();
elli.remove(i-1);
a[vernier]=0;
}
}
}
private class MouseAction extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
begin=event.getPoint();
}
public void mouseReleased(MouseEvent event)
{
end=event.getPoint();
if(SimpleFrame.getFlag()==1) addLine(begin,end);
if(SimpleFrame.getFlag()==2) addRect(begin,end);
if(SimpleFrame.getFlag()==3) addElli(begin,end);
}
public void mouseClicked(MouseEvent event)
{
int i;
if(SimpleFrame.getFlag()==1) {i=line.size();line.remove(i-1);repaint();}
if(SimpleFrame.getFlag()==2) {i=rect.size();rect.remove(i-1);repaint();}
if(SimpleFrame.getFlag()==3) {i=elli.size();elli.remove(i-1);repaint();}
}
}
private class MouseMotionAction implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
setCursor(Cursor.getDefaultCursor());
}
public void mouseDragged(MouseEvent event)
{
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
}
public static int getLineSize()
{
return line.size();
}
public static int getRectSize()
{
return rect.size();
}
public static int getElliSize()
{
return elli.size();
}
}
//--------------------------------------------------------------------------------
//new class,in order to keep the shape of color
class LineShape implements Serializable
{
private Line2D line;
private Color c;
public LineShape()
{
line=null;
c=Color.BLACK;
}
public LineShape(Line2D line,Color c)
{
this.line=line;
this.c=c;
}
public Line2D getLine() {return line;}
public Color getColor() {return c;}
public void setLine(Line2D line) {this.line=line;}
public void setColor(Color c) {this.c=c;}
}
class RectShape implements Serializable
{
private Rectangle2D rect;
private Color c;
public RectShape()
{
rect=null;
c=Color.BLACK;
}
public RectShape(Rectangle2D rect,Color c)
{
this.rect=rect;
this.c=c;
}
public Rectangle2D getRect() {return rect;}
public Color getColor() {return c;}
public void setRect(Rectangle2D rect) {this.rect=rect;}
public void setColor(Color c) {this.c=c;}
}
class ElliShape implements Serializable
{
private Ellipse2D elli;
private Color c;
public ElliShape()
{
elli=null;
c=Color.BLACK;
}
public ElliShape(Ellipse2D elli,Color c)
{
this.elli=elli;
this.c=c;
}
public Ellipse2D getElli() {return elli;}
public Color getColor() {return c;}
public void setElli(Ellipse2D elli) {this.elli=elli;}
public void setColor(Color c) {this.c=c;}
}
//----------------------------------------------
// AboutDialog class
class AboutDialog extends JDialog
{
public AboutDialog(JFrame owner)
{
super(owner,"About Dialog",true);
add(new JLabel("It was desined by Phil !"),BorderLayout.CENTER);
JButton okButton=new JButton("ok");
okButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setVisible(false);
}
});
JPanel dialogPanel=new JPanel();
dialogPanel.add(okButton);
add(dialogPanel,BorderLayout.SOUTH);
setSize(200,150);
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int height=screenSize.height;
int width=screenSize.width;
setLocation(width/5*2,height/2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -