import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
public class DrawLine extends JFrame{
public DrawLine()
{
setTitle("DrawLineTest");
Toolkit kit =Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
double screenWidth =screenSize.getWidth();
double screenHeight = screenSize.getHeight();
setBounds((int)screenWidth/2-DEFAULT_WIDTH/2,(int)screenHeight/2-DEFAULT_HEIGHT/2,DEFAULT_WIDTH,DEFAULT_HEIGHT);
addMouseListener(new MouseAdapter(){
int orgX;
int orgY;
public void mousePressed(MouseEvent e)
{
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e)
{
Graphics g = getGraphics();
g.setColor(Color.ORANGE);
g.drawString("("+orgX+","+orgY+")",orgX ,orgY);
g.drawString("("+e.getX()+","+e.getY()+")", e.getX(), e.getY());
g.setFont(new Font("Serif",Font.ITALIC,70));
g.drawLine(orgX, orgY, e.getX(), e.getY());
}
});
setVisible(true);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
new DrawLine();
}
private static final int DEFAULT_WIDTH =400;
private static final int DEFAULT_HEIGHT=350;
}
————————————————
版权声明:本文为CSDN博主「明明如月学长」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/w605283073/article/details/46572393