向各位仁兄贤弟请教:下面这段代码怎么运行结果不对?
本文导语: import java.awt.*; import java.applet.Applet; import java.awt.event.*; import java.lang.Math; import java.awt.Graphics; /* * draw.java * * Created on 2002年11月8日, 下午1:20 */ /** * * @author root */ public class draw extends java.applet.Applet...
import java.applet.Applet;
import java.awt.event.*;
import java.lang.Math;
import java.awt.Graphics;
/*
* draw.java
*
* Created on 2002年11月8日, 下午1:20
*/
/**
*
* @author root
*/
public class draw extends java.applet.Applet implements ActionListener, MouseListener,MouseMotionListener {
/** Initialization method that will be called after the applet is loaded
* into the browser.
*/
Button bDraw,bLine,bOval,bRect,bRounded;
Point dot[]=new Point[1000];
Point start,end;
int dots=0;
boolean mouseUp=false;
boolean draw=false;
boolean line=true;
boolean oval=false;
boolean rounded=false;
boolean rectangle=false;
public void init() {
bLine=new Button("Draw Lines");
bOval=new Button("Draw ovals");
bRect=new Button("Draw Rectangles");
bRounded=new Button("Draw rounded rects");
bDraw=new Button("Draw freehanded");
add(bLine);
add(bOval);
add(bRect);
add(bRounded);
add(bDraw);
// setColor(Color.red);
bLine.addActionListener(this);
bOval.addActionListener(this);
bRect.addActionListener(this);
bRounded.addActionListener(this);
bDraw.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
/** Invoked when an action occurs.
*
*/
public void actionPerformed(ActionEvent e) {
setFlagsFalse();
if(e.getSource()==bDraw)
draw=true;
else if(e.getSource()==bLine)
line=true;
else if(e.getSource()==bOval)
oval=true;
else if(e.getSource()==bRect)
rectangle=true;
else if(e.getSource()==bRounded)
rounded=true;
}
/** Invoked when the mouse button has been clicked (pressed
* and released) on a component.
*
*/
public void mouseClicked(MouseEvent e) {
}
/** Invoked when the mouse enters a component.
*
*/
public void mouseEntered(MouseEvent e) {
}
/** Invoked when the mouse exits a component.
*
*/
public void mouseExited(MouseEvent e) {
}
/** Invoked when a mouse button has been pressed on a component.
*
*/
public void mousePressed(MouseEvent e) {
mouseUp=false;
start=new Point(getX(),getY());
}
/** Invoked when a mouse button has been released on a component.
*
*/
public void mouseReleased(MouseEvent e) {
start=new Point(Math.min(e.getX(),start.x),Math.min(e.getY(),start.y));
if(line){
end=new Point(e.getX(),e.getY());
}else{
end=new Point(Math.max(e.getX(),start.x), Math.max(e.getY(),start.y));
}
mouseUp=true;
repaint();
}
/** Invoked when a mouse button is pressed on a component and then
* dragged. MOUSE_DRAGGED events will continue to be
* delivered to the component where the drag originated until the
* mouse button is released (regardless of whether the mouse position
* is within the bounds of the component).
*
* Due to platform-dependent Drag&Drop implementations,
* MOUSE_DRAGGED events may not be delivered during a native
* Drag&Drop operation.
*
*/
public void mouseDragged(MouseEvent e) {
if(draw){
dot[dots]=new Point(e.getX(), e.getY());
dots++;
repaint();
}
}
/** Invoked when the mouse button has been moved on a component
* (with no buttons down).
*
*/
public void mouseMoved(MouseEvent e) {
}
void setFlagsFalse(){
rounded=false;
line=false;
oval=false;
rectangle=false;
draw=false;
}
public void paint(Graphics g){
//if(mouseUp){
int width=end.x-start.x;
int height=end.y-start.y;
if(line){
g.setColor(Color.red);
g.drawLine(start.x,start.y,end.x,end.y);
}
else if(oval){
g.setColor(Color.green);
g.fillOval(start.x,start.y, width,height);
}
else if(rectangle){
g.setColor(Color.blue);
g.fillRect(start.x,start.y,width,height);
}
else if(rounded){
g.setColor(Color.yellow);
g.fillRoundRect(start.x,start.y, width, height, 50,50);
}
else{
g.setColor(Color.orange);
for(int loop_index=0;loop_index