RT,编译器也通过了,是不是缺少了哪句,望指点import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.io.*;
import java.awt.geom.*;class Point implements Serializable{
int x,y;
int tool;
Point(int x,int y,int tool){
this.x=x;
this.y=y;
this.tool=tool;
}
}class paintboard extends Frame implements ActionListener{
MenuBar mb=new MenuBar();
Menu f=new Menu("Flie"),d=new Menu("Draw");
MenuItem n=new MenuItem("New"),ex=new MenuItem("Exit");
MenuItem l=new MenuItem("Line"),o=new MenuItem("Oval"),r=new MenuItem("Rectangle"); int screenWidth=800;
int screenHeight=600;
int painttype=0;
MyCanvas can=new MyCanvas(painttype); public paintboard(String s){
super(s);
setSize(screenWidth,screenHeight);
setBackground(Color.white);
setMenuBar(mb);
mb.add(f);
f.add(n);
f.add(ex);
mb.add(d);
d.add(l);
d.add(o);
d.add(r);
n.addActionListener(this);
ex.addActionListener(this);
l.addActionListener(this);
o.addActionListener(this);
r.addActionListener(this);
add("Center",can);
validate();
setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==n) can.clear();
if(e.getSource()==ex) System.exit(0);
if(e.getSource()==l) painttype=0;
if(e.getSource()==o) painttype=1;
if(e.getSource()==r) painttype=2;
}
}class MyCanvas extends Canvas implements MouseListener,MouseMotionListener{
int type;
int num=0;
Vector<Point> vec=new Vector<Point>();
Point P1,P2,P3;
public MyCanvas(int type){
super();
this.type=type;
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
g.setColor(Color.red);
int n=vec.size();
if(n>=2){
for(int i=0; i<n ;i=i+2){
P1 = (Point)vec.elementAt(i);
P2 = (Point)vec.elementAt(i+1);
if(P1.tool==P2.tool) {
switch(P1.tool) {
case 0://直线
g.drawLine(P1.x,P1.y,P2.x,P2.y);
break;
case 1://圆
g.drawOval(Math.min(P1.x,P2.x),Math.min(P1.y,P2.y),Math.abs(P2.x-P1.x),Math.abs(P2.y-P1.y));
break;
case 2://矩形
g.drawRect(Math.min(P1.x,P2.x),Math.min(P1.y,P2.y),Math.abs(P2.x-P1.x),Math.abs(P2.y-P1.y));
break;
}
}
}
}
}
public void update(Graphics g){
paint(g);
}
public void clear(){
vec.removeAllElements();
repaint();
} public void mousePressed(MouseEvent e){
int x=getX();
int y=getY();
P1=new Point(x,y,type);
vec.addElement(P1);
}
public void mouseReleased(MouseEvent e){
int x=getX();
int y=getY();
P2=new Point(x,y,type);
num=0;
vec.addElement(P2);
repaint();
}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}public class Hello{
public static void main(String args[]) {
new paintboard("画图程序");
}
}画图
import java.awt.event.*;
import java.util.Vector;
import java.io.*;
import java.awt.geom.*;class Point implements Serializable{
int x,y;
int tool;
Point(int x,int y,int tool){
this.x=x;
this.y=y;
this.tool=tool;
}
}class paintboard extends Frame implements ActionListener{
MenuBar mb=new MenuBar();
Menu f=new Menu("Flie"),d=new Menu("Draw");
MenuItem n=new MenuItem("New"),ex=new MenuItem("Exit");
MenuItem l=new MenuItem("Line"),o=new MenuItem("Oval"),r=new MenuItem("Rectangle"); int screenWidth=800;
int screenHeight=600;
int painttype=0;
MyCanvas can=new MyCanvas(painttype); public paintboard(String s){
super(s);
setSize(screenWidth,screenHeight);
setBackground(Color.white);
setMenuBar(mb);
mb.add(f);
f.add(n);
f.add(ex);
mb.add(d);
d.add(l);
d.add(o);
d.add(r);
n.addActionListener(this);
ex.addActionListener(this);
l.addActionListener(this);
o.addActionListener(this);
r.addActionListener(this);
add("Center",can);
validate();
setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==n) can.clear();
if(e.getSource()==ex) System.exit(0);
if(e.getSource()==l) painttype=0;
if(e.getSource()==o) painttype=1;
if(e.getSource()==r) painttype=2;
}
}class MyCanvas extends Canvas implements MouseListener,MouseMotionListener{
int type;
int num=0;
Vector<Point> vec=new Vector<Point>();
Point P1,P2,P3;
public MyCanvas(int type){
super();
this.type=type;
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
g.setColor(Color.red);
int n=vec.size();
if(n>=2){
for(int i=0; i<n ;i=i+2){
P1 = (Point)vec.elementAt(i);
P2 = (Point)vec.elementAt(i+1);
if(P1.tool==P2.tool) {
switch(P1.tool) {
case 0://直线
g.drawLine(P1.x,P1.y,P2.x,P2.y);
break;
case 1://圆
g.drawOval(Math.min(P1.x,P2.x),Math.min(P1.y,P2.y),Math.abs(P2.x-P1.x),Math.abs(P2.y-P1.y));
break;
case 2://矩形
g.drawRect(Math.min(P1.x,P2.x),Math.min(P1.y,P2.y),Math.abs(P2.x-P1.x),Math.abs(P2.y-P1.y));
break;
}
}
}
}
}
public void update(Graphics g){
paint(g);
}
public void clear(){
vec.removeAllElements();
repaint();
} public void mousePressed(MouseEvent e){
int x=getX();
int y=getY();
P1=new Point(x,y,type);
vec.addElement(P1);
}
public void mouseReleased(MouseEvent e){
int x=getX();
int y=getY();
P2=new Point(x,y,type);
num=0;
vec.addElement(P2);
repaint();
}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}public class Hello{
public static void main(String args[]) {
new paintboard("画图程序");
}
}画图
这句话前面加个对象
比如frame试试
int x = getMousePosition().x;
int y = getMousePosition().y;
P1 = new Point(x, y, type);
vec.addElement(P1);
} public void mouseReleased(MouseEvent e) {
int x = getMousePosition().x;
int y = getMousePosition().y;
P2 = new Point(x, y, type);
num = 0;
vec.addElement(P2);
repaint();
}Component中的getX是获取当前组件原点坐标,而drawLine()在Java API中的意思是在此图形上下文的坐标系中,使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间画一条线
所以你应该获取鼠标所在的位置。但即使这样你的程序还是存在问题,即只能画直线!自己慢慢找找吧!对自己提高有很大好处!多查API
if (e.getSource() == n)
can.clear();
if (e.getSource() == ex)
System.exit(0);
if (e.getSource() == l)
painttype = 0;
if (e.getSource() == o)
painttype = 1;
if (e.getSource() == r)
painttype = 2;
this.remove(can);
can = new MyCanvas(painttype);
this.add("Center", can);
this.repaint(); }
在不大改你的程序的前提下,我只能这么做了,这样可以实现画各种图形,但存在2个bug
1.只能在一个画布上画同一个种类的图形
2.每次修改图形种类后会出现画布无法画图的bug,需要拖动下对话框边框解决
对API确实不熟,而且也刚学java两个星期,教程还是按照老版本的java编的,看的那个疼
你里面没声明frame当然会报错了。
拷贝他人的程序前提是看懂。
如果只是单纯为了效果而做真没必要。
你看哪个继承了frame?
就有frame的功能,那你就在那个对象后面加setVisible(true);
看到效果了,原来直接用getx(),鼠标按下和释放时获取到都是同一个坐标点,得改成e.getX()才行
至于其他的我再参照教程代码改改看看
谢谢
2楼我说的是
int x = getMousePosition().x;
int y = getMousePosition().y;
这样鼠标要点两次,第一次是开始位置,第二次结束位置。你说的e.getX()是点下鼠标拉到位置,两种都可以实现,但总体来说,还是你的方案贴合用户体验
这不是frame的问题,因为他的paintboard类继承了Frame然后又new了这个对象,所以本身就是了,不需要再添加一个。
paintboard类修改如下:
public void actionPerformed(ActionEvent e){
if(e.getSource()==n) can.clear();
if(e.getSource()==ex) System.exit(0);
if(e.getSource()==l){
painttype=0;
can.settype(painttype);
}
if(e.getSource()==o){
painttype=1;
can.settype(painttype);
}
if(e.getSource()==r){
painttype=2;
can.settype(painttype);
}
/*
this.remove(can);
can=new MyCanvas(painttype);
System.out.println("add new Canvas");
this.add("Center",can);
this.repaint();
*/
}
Mycan类修改如下
public void settype(int tp){
this.type=tp;
}谢谢大家的解答。@luoyan35714、@失落夏天、@AA5279AA,谢谢你们