Topic: 关于模拟画图的 |
Print this page |
1.关于模拟画图的 | Copy to clipboard |
Posted by: cainiao025 Posted on: 2006-08-18 00:20 我要通过点击单选钮来改变所画的图形,如选中直线则鼠标响应画出直线,选中圆则鼠标画出圆。 小弟我做了一个但是选中单选钮时,却不能得到正常的结果。我不知道在鼠标事件中怎样去处理单选钮,也就是说在鼠标事件中怎么样才能知道选中哪个单选钮。 还请大家帮忙指点一二。小弟不胜感激。 import java.awt.*; import java.awt.event.*; import java.applet.*; public class AppletExa11 extends Applet { CheckboxGroup cg; Checkbox line,rect,cir; userCanvas mycanvas; public void init() { cg=new CheckboxGroup(); line=new Checkbox("画线",true,cg); rect=new Checkbox("画矩形",false,cg); cir=new Checkbox("画圆",false,cg); add(line); add(rect); add(cir); mycanvas=new userCanvas(new Dimension(200,180),this); mycanvas.setBackground(Color.pink); add(mycanvas); mycanvas.requestFocus(); } } class userCanvas extends Canvas { AppletExa11 ae=new AppletExa11(); Applet mParent; boolean isLine=false; boolean isRect,isCir; int startX=0,startY=0,currentX=0,currentY=0; int w,h; public userCanvas(Dimension d,Applet p) { mParent=p; setSize(d); addMouseMotionListener(new userMouseMotion(this)); addMouseListener(new userMouse(this)); } public void setMeg(String s) { mParent.showStatus(s); } public void setIsLine(boolean b) { isLine=b; } public void setIsRect(boolean b) { isRect=b; } public void setIsCir(boolean b) { isCir=b; } public void setStart(int x,int y) { startX=x; startY=y; } public void setCurrent(int x,int y) { currentX=x; currentY=y; } public void setWH(int w,int h) { this.w=w; this.h=h; } public void paint(Graphics g) { g.drawString("("+currentX+","+currentY+")",10,10); if(isLine) { g.drawLine(startX,startY,currentX,currentY); } else if(isRect) { g.drawRect(startX,startY,w,h); } else if(isCir) { g.drawOval(startX,startY,w,h); } } } class userMouseMotion extends MouseMotionAdapter { AppletExa11 ae=new AppletExa11(); userCanvas myobj; userMouseMotion(userCanvas p) { myobj=p; } public void mouseMoved(MouseEvent e) { myobj.setCurrent(e.getX(),e.getY()); myobj.setIsLine(false); myobj.repaint(10,0,60,30); } public void mouseDragged(MouseEvent e) { //下面这句if语句不知道怎么写,不知道要怎样告知程序当前选中了哪个单选钮。 //if((ae.cg.getSelectedCheckbox()).getLabel()=="画线") //用鼠标画直线 /*myobj.setCurrent(e.getX(),e.getY()); myobj.setIsLine(true); myobj.repaint(); myobj.setMeg("dragged mouse."); */ //用鼠标画矩形 /* myobj.setWH((e.getX()-myobj.startX),(e.getY()-myobj.startY)); myobj.setIsRect(true); myobj.repaint(); */ //用鼠标画圆 /* myobj.setWH((e.getX()-myobj.startX),(e.getX()-myobj.startX)); myobj.setIsCir(true); myobj.repaint(); */ } } class userMouse extends MouseAdapter { userCanvas myobj; userMouse(userCanvas p) { myobj=p; } public void mousePressed(MouseEvent e) { myobj.setStart(e.getX(),e.getY()); myobj.setMeg("pressed mouse."+"("+e.getX()+","+e.getY()+")"); } } |
2.Re:关于模拟画图的 [Re: cainiao025] | Copy to clipboard |
Posted by: cxp108 Posted on: 2006-08-18 10:59 楼主基本功严重不足,建议好好补习一下。
中
返回的是String 值,String 是不能使用==运算符来计算等值关系的! 应该使用String 类中的 equals(String value) 这个方法来计算两个String 是否相同。 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |