jy00057800 2004年12月22日
自做拉个画板程序(1)基本可以画圆,矩形,直线但有个问题想改善希望大哥门帮我看看,请进有源程序,问题描述里面。。
每次画拉一个图形比如直线,再画第2个时先画的就没拉 应该重写update()吗?
请大哥门帮我看看

程序结构:
一个自定义画布类Canva
一个主类 PhotoShop
3个选者所画图形的按钮类 Cril,Rect,Line

import java.awt.*;
import java.awt.event.*;
public class PhotoShop extends Frame
{

int x,y; int xf,yf;
Cril cril;Rect rect;Line line;
boolean l=false,cr=false,r=false,cl=false,f=false,co=false;
Canva canvas=new Canva(this);
Dialog dialog=new Dialog(this,"工具条",false);
public PhotoShop()
{


cril=new Cril("圆",this);
rect=new Rect("矩形",this);
line=new Line("直线",this);

dialog.setSize(100,100);
dialog.setLayout(new GridLayout(3,1));
dialog.add(cril);
dialog.add(line);
dialog.add(rect);
dialog.setVisible(true);

this.add(canvas,BorderLayout.CENTER);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}



public static void main(String args[])
{
System.out.println("Starting PhotoShop...");
PhotoShop mainFrame = new PhotoShop();
mainFrame.setSize(400, 400);
mainFrame.setTitle("PhotoShop");
mainFrame.setVisible(true);
}
}

class Cril extends Button implements ActionListener
{
PhotoShop photoShop;
int x,y;
Cril(String s,PhotoShop photoShop)
{
super(s);
this.setSize(30,15);
this.photoShop=photoShop;
this.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
photoShop.f=false;
photoShop.co=false;
photoShop.cr=true;
photoShop.l=false;
photoShop.r=false;
photoShop.cl=false;
photoShop.f=false;
}
}
class Rect extends Button implements ActionListener
{
PhotoShop photoShop;
int x,y;
Rect(String s,PhotoShop photoShop)
{
super(s);
this.setSize(30,15);
this.photoShop=photoShop;
this.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
photoShop.f=false;
photoShop.co=false;
photoShop.cr=false;
photoShop.l=false;
photoShop.r=true;
photoShop.cl=false;
photoShop.f=false;
}

}
class Line extends Button implements ActionListener
{
PhotoShop photoShop;
int x,y;
Line(String s,PhotoShop photoShop)
{
super(s);
this.setSize(30,15);
this.photoShop=photoShop;
this.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
photoShop.f=false;
photoShop.co=false;
photoShop.cr=false;
photoShop.l=true;
photoShop.r=false;
photoShop.cl=false;
photoShop.f=false;
}
}
class Canva extends Canvas implements MouseMotionListener,MouseListener
{

PhotoShop photoShop;
Canva(PhotoShop photoShop)
{
this.photoShop=photoShop;
this.setSize(300,300);
this.addMouseMotionListener(this);
this.addMouseListener(this);
}
public void paint(Graphics g)
{
g.setColor(Color.black);
if(photoShop.l==true)
g.drawLine(photoShop.xf,photoShop.yf,photoShop.x,photoShop.y);
if(photoShop.cr==true)
g.drawOval(photoShop.xf,photoShop.yf,photoShop.x,photoShop.y);
if(photoShop.r==true)
g.drawRect(photoShop.xf,photoShop.yf,photoShop.x,photoShop.y);
this.validate();
}
public void mouseDragged(MouseEvent me)
{
photoShop.x=me.getX();photoShop.y=me.getY();
repaint();
}
public void mouseMoved(MouseEvent me){}
public void mousePressed(MouseEvent me)
{
photoShop.xf=me.getX();photoShop.yf=me.getY();
repaint();
}
public void mouseEntered(MouseEvent me){}
public void mouseClicked(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mouseReleased(MouseEvent me)
{
photoShop.x=me.getX();photoShop.y=me.getY();
repaint();
}

}
...全文
130 点赞 收藏 4
写回复
4 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Java SE
创建于2007-09-28

3.4w+

社区成员

30.7w+

社区内容

Java 2 Standard Edition
社区公告
暂无公告