import java.awt.event.MouseEvent
import java.awt.event.MouseListener
import javax.swing.*
import java.awt.*
import java.util.LinkedList
public class DrawPoint extends JFrame implements MouseListener{
private LinkedList<Point> list//新建一个Point类型的链表存储用于存储“节点”
private final int WIDTH=318,HEIGHT=340
private Dimension scrnsize
private Toolkit toolkit = Toolkit.getDefaultToolkit()
public DrawPoint() {
// TODO Auto-generated constructor stub
list=new LinkedList<Point>()//链表初始化
//窗口设置部分,不比细究
this.setSize(WIDTH,HEIGHT)
this.setResizable(false)
this.setDefaultCloseOperation(EXIT_ON_CLOSE)
scrnsize = toolkit.getScreenSize()
setLocation(scrnsize.width / 2 - getWidth() / 2,
scrnsize.height / 2 - getHeight() / 2)
this.addMouseListener(this)//添加窗口鼠标监听
this.setVisible(true)
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
//为鼠标按钮释放添加事件处理
// TODO Auto-generated method stub
int x=e.getX()//获取鼠标的x坐标
int y=e.getY()//获取鼠标的y坐标
String str=JOptionPane.showInputDialog(null, "请输入改点标记文字")
//显示对话框,根据用户输入得到标记文字
if(str!=null){//若标记文字不为空,则该点视为有效点,将该点加入链表
list.add(new Point(x,y,str))
}
repaint()//用该函数调用paint()方法,进行重构 *** 作
this.validate()
}
public void paint(Graphics g){
if(list.isEmpty())//如果链表为空,不进行任何 *** 作,直接返回
return
for(Point p:list){//遍历数组,获取所有节点
g.drawOval(p.getX(), p.getY(), 2, 2)//先画点
g.drawString(p.getS(), p.getX()+5, p.getY()+6)//再标注节点的描述性文字
}
}
public static void main(String args[]){
DrawPoint p=new DrawPoint()
}
}
class Point {//节点类,包含x坐标,y坐标,描述性文字3个属性
private int x,y//x坐标和y坐标
private String s//描述性文字
public Point(){
x=0
y=0
s=""
}
public Point(int x,int y,String s){
this.x=x
this.y=y
this.s=s
}
public int getX(){
return x
}
public int getY(){
return y
}
public String getS(){
return s
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)