import java.*;
import java.awt.event.*;
public class DrawLine3 extends Frame
{
/**
* Method main
*
*
* @param args
*
*/
int orgX;
int orgY;
int endX;
int endY;
public static void main(String[] args) {
// TODO: 在这添加你的代码
DrawLine3 dl =new DrawLine3();
dl.init();
}
/**
* Method init
*
*
*/
public void init() {
// TODO: 在这添加你的代码
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
endX=e.getX();
endY=e.getY();
Graphics g=getGraphics();
g.setColor(Color.red);
g.setFont(new Font( "宋体 ",Font.ITALIC|Font.BOLD,30));
g.drawString(new String(orgX+ ", "+orgY),orgX,orgY);
g.drawString(new String(e.getX()+ ", "+e.getY()),e.getX(),e.getY());
g.drawLine(orgX,orgY,e.getX(),e.getY());
}
}
);
}
/**
* Method paint
*
*
*/
public void paint() {
// TODO: 在这添加你的代码
g.drawLine(orgX,orgY,endX,endY);
}
}
------解决方案--------------------
import java.*; //此处改为 import java.awt.*;
import java.awt.event.*;
public class DrawLine3 extends Frame
{
/**
* Method main
*
*
* @param args
*
*/
int orgX;
int orgY;
int endX;
int endY;
public static void main(String[] args) {
// TODO: 在这添加你的代码
DrawLine3 dl =new DrawLine3();
dl.init();
}
/**
* Method init
*
*
*/
public void init() {
// TODO: 在这添加你的代码
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
endX=e.getX();
endY=e.getY();
Graphics g=getGraphics();
g.setColor(Color.red);
g.setFont(new Font( "宋体 ",Font.ITALIC|Font.BOLD,30));
g.drawString(new String(orgX+ ", "+orgY),orgX,orgY);
g.drawString(new String(e.getX()+ ", "+e.getY()),e.getX(),e.getY());
g.drawLine(orgX,orgY,e.getX(),e.getY());
}
}
);
}
/**
* Method paint
*
*
*/
public void paint() { //此处改为public void paint(Graphics g)
// TODO: 在这添加你的代码
g.drawLine(orgX,orgY,endX,endY);
}
}