从画图程序开始,走进GUI编程世界
else{
File file=showDialog("保存图片","保存","保存图片",'s',
new File(files.getCurrentDirectory(),filename));
if(file==null)
return;
else
if(file.exists())
if(JOptionPane.NO_OPTION==JOptionPane.showConfirmDialog(DrawFrame.this,file.getName()+"已存在,覆盖?"
,"确认保存",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE))
return;
saveDraw(file);
}
}
private void saveDraw(File outFile){
try{
ObjectOutputStream out=new ObjectOutputStream(new
BufferedOutputStream(
new FileOutputStream(outFile)));
out.writeObject(theApp.getModel());
out.flush();
out.close();
}
catch(IOException e){
System.out.println(e);
JOptionPane.showMessageDialog(DrawFrame.this,"错误","保存失败",JOptionPane.ERROR_MESSAGE);
return;
}
if(outFile!=modelFile){
modelFile=outFile;
filename=modelFile.getName();
setTitle(frameTitle+modelFile.getPath());
}
changed=false;
}
public void openDraw(File inFile){
try{
ObjectInputStream in=new ObjectInputStream(new
BufferedInputStream(
new FileInputStream(inFile)));
theApp.insertModel((DrawModel)in.readObject());
in.close();
modelFile=inFile;
filename=modelFile.getName();
setTitle(frameTitle+modelFile.getPath());
}
catch(Exception e){
System.out.println(e);
JOptionPane.showMessageDialog(DrawFrame.this,"错误","打开失败",JOptionPane.ERROR_MESSAGE);
return;
}
}
public void checkForSave(){
if(changed)
if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(DrawFrame.this,"文件已修改,保存?"
,"确认保存",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE))
saveIt();
}
}
package boya;
import java.io.*;
import java.util.*;
class DrawModel extends Observable implements Serializable{
protected LinkedList elements=new LinkedList();
public void add(Elements element){
elements.add(element);
setChanged();
notifyObservers(element.getBounds());
}
public boolean remove(Elements element){
boolean removed=elements.remove(element);
if(removed){
setChanged();
notifyObservers(element.getBounds());
}
return removed;
}
public Iterator getIterator(){
return elements.listIterator();
}
}
package boya;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class DrawView extends JComponent implements Observer,ID{
private Draw theApp;
public DrawView(Draw theApp){
this.theApp=theApp;
MouseHandler handler=new MouseHandler();
addMouseListener(handler);
addMouseMotionListener(handler);
}
class MouseHandler extends MouseInputAdapter{
private Point start,end;
private Elements temp;
private Graphics2D g2D;
private Elements createElement(Point start,Point end){
switch(theApp.getWindow().getElementType()){
case LINE:
return new Elements.Line(start,end,theApp.getWindow().getElementColor());
case RECTANGLE:
return new Elements.Rectangle(start,end,theApp.getWindow().getElementColor());
case CIRCLE:
return new Elements.Circle(start,end,theApp.getWindow().getElementColor());
case CURVE:
return new Elements.Curve(start,end,theApp.getWindow().getElementColor());
}
return null;
}
public void mousePressed(MouseEvent e){
start=e.getPoint();
int modifier=e.getModifiers();
if((modifier&e.BUTTON1_MASK)!=0){
g2D=(Graphics2D)getGraphics();
g2D.setXORMode(getBackground());
g2D.setPaint(theApp.getWindow().getElementColor());
}
}
public void mouseDragged(MouseEvent e){
end=e.getPoint();
int modifier=e.getModifiers();
if((modifier&e.BUTTON1_MASK)!=0&&(theApp.getWindow()
.getElementType()!=TEXT)){
if(temp==null)
temp=createElement(start,end);
else{
temp.draw(g2D);
temp.moving(start,end);
}
temp.draw(g2D);
}
}
public void mouseReleased(MouseEvent e){
int modifier=e.getModifiers();
if(e.isPopupTrigger()){
start=e.getPoint();
theApp.getWindow().getPopup().show((Component)e.getSource(),
start.x,start.y);
start=null;
}
else if((modifier&e.BUTTON1_DOWN_MASK)==0&&(theApp.getWindow()
.getElementType()!=TEXT)){
if(temp!=null){
theApp.getModel().add(temp);
temp=null;
}
if(g2D!=null){
g2D.dispose();
g2D=null;
}
start=end=null;
}
}
public void mouseClicked(MouseEvent e){
int modifier=e.getModifiers();
if((modifier&e.BUTTON1_MASK)!=0&&(theApp.getWindow()
.getElementType()==TEXT)){
start=e.getPoint();
String text=JOptionPane.showInputDialog(
(Component)e.getSource(),"输入文字"
,"文字",JOptionPane.PLAIN_MESSAGE);
if(text!=null){
g2D=(Graphics2D)getGraphics();
Font font=theApp.getWindow().getFont();
temp=new Elements.Text(font,text,start,
theApp.getWindow().getElementColor(),
font.getStringBounds(text,g2D.getFontRenderContext()).getBounds());
temp.draw(g2D);
if(temp!=null)
theApp.getModel().add(temp);
temp=null;
g2D.dispose();
g2D=null;
start=null;
}
}
}
}
public void update(Observable o,Object rectangle){
if(rectangle==null)
repaint();
else repaint((Rectangle)rectangle);
}
public void paint(Graphics g){
Graphics2D g2D=(Graphics2D)g;
Iterator elements=theApp.getModel().getIterator();
Elements element;
while(elements.hasNext()){
element=(Elements)elements.next();
element.draw(g2D);
}
}
}
package boya;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
abstract class Elements implements Serializable{
protected Color color;
public Elements(Color color){
this.color=color;
}
public Color getColor(){
return color;
}
public abstract java.awt.Rectangle getBounds(
Tags:画图,程序,开始,走进,GUI,编程,世界

