小偷涂鸦 java_用Java做一个涂鸦板

本文介绍了如何使用Java创建一个简单的涂鸦板应用。通过继承Frame类,添加鼠标事件监听器,实现在画布上画线的功能。当鼠标按下、拖动和抬起时,记录并绘制从起点到终点的线条。通过重写paint()和update()方法,确保图形的实时更新。
摘要由CSDN通过智能技术生成

效果:用鼠标在面板上划过,会留下痕迹。实现涂鸦效果。

首先,创建项目和包。并创建类DrawLine。继承java.awt.Frame类

需要定义几个变量来定义画图的起点坐标(X,Y)和终点。drawing定义是否要画图的判断变量。

然后添加鼠标事件监听器(MouseListener和MouseMotionListener)只需要再三个鼠标事件中实现方法,即按下鼠标,拖动鼠标,抬起鼠标。

重写pain()方法,调用画线的方法drawLine(startX,startY,endX,endY)在Frame中绘制。只有这个方法还不行,因为我们需要完整的不断的显示在Frame中,所以还要重写update()方法一边更新Frame中的Graphics。

这样,一个涂鸦面板就做好了。

代码如下:

package

com.godtzsd.cn;

import java.awt.*;

import

java.awt.event.*;

import

java.util.EventListener;

import

java.awt.event.MouseMotionListener;

public class DrawLine extends

Frame implements MouseListener,MouseMotionListener {

int startX,startY;

//定义画图的起点X和Y的坐标

int endX,endY;

//定义画图的终点X和Y的坐标

boolean drawing=false;

public DrawLine() {

// TODO Auto-generated

constructor stub

super("涂鸦面板");

this.addMouseListener(this);

this.addMouseMotionListener(this);

this.addWindowListener(new

WindowListener(){

public void

windowActivated(WindowEvent arg0) {

// TODO Auto-generated method

stub

}

public void

windowClosed(WindowEvent arg0) {

// TODO Auto-generated method

stub

}

public void

windowClosing(WindowEvent arg0) {

// TODO Auto-generated method

stub

System.exit(0);

}

public void

windowDeactivated(WindowEvent arg0) {

// TODO Auto-generated method

stub

}

public void

windowDeiconified(WindowEvent arg0) {

// TODO Auto-generated method

stub

}

public void

windowIconified(WindowEvent arg0) {

// TODO Auto-generated method

stub

}

public void

windowOpened(WindowEvent arg0) {

// TODO Auto-generated method

stub

}

});

this.setSize(180,160);

this.setVisible(true);

}

public void paint(Graphics

g){

super.paint(g);

g.drawLine(startX, startY,

endX,endY);

}

public void update(Graphics

g){

this.paint(g);

}

public static void main(String[]

args) {

// TODO Auto-generated method

stub

new DrawLine();

}

public void

mouseClicked(MouseEvent e) {

// TODO Auto-generated method

stub

}

public void

mouseEntered(MouseEvent e) {

// TODO Auto-generated method

stub

}

public void

mouseExited(MouseEvent e) {

// TODO Auto-generated method

stub

}

public void

mousePressed(MouseEvent e) {

// TODO Auto-generated method

stub

this.drawing=true;

this.startX=e.getX();

this.startY=e.getY();

}

public void

mouseReleased(MouseEvent e) {

// TODO Auto-generated method

stub

this.drawing=false;

}

public void

mouseDragged(MouseEvent e) {

// TODO Auto-generated method

stub

if(drawing){

this.endX=e.getX();

this.endY=e.getY();

this.startX=e.getX();

//当前点作为起始点

this.startY=e.getY();

this.repaint();

}

}

public void mouseMoved(MouseEvent

e) {

// TODO Auto-generated method

stub

}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给你一个基于 Dijkstra 算法的追踪游戏实现。这个游戏的规则是,有一个警察和一个小偷,在一个城市里行动。警察要尽快地抓住小偷小偷要尽可能地逃脱。每个街区都有一个固定的警察和小偷的位置。 这个游戏的实现可以分为以下几个步骤: 1.定义一个地图,包括街区的位置和道路的连接情况。 2.定义警察和小偷的初始位置。 3.使用 Dijkstra 算法计算从警察到小偷的最短路径。 4.警察按照最短路径移动,小偷则根据一定的策略移动。 5.重复步骤 3 和 4,直到小偷被捕或者逃脱成功。 下面是这个游戏的 Python 实现代码: ```python import heapq class Graph: def __init__(self): self.vertices = {} def add_vertex(self, name, edges): self.vertices[name] = edges def shortest_path(self, start, end): heap = [(0, start)] visited = set() while heap: (cost, current) = heapq.heappop(heap) if current in visited: continue visited.add(current) if current == end: return cost for (neighbor, next_cost) in self.vertices[current].items(): heapq.heappush(heap, (cost + next_cost, neighbor)) return -1 class Police: def __init__(self, graph, start): self.graph = graph self.location = start def move(self, target): path = self.graph.shortest_path(self.location, target) if path != -1: self.location = target class Thief: def __init__(self, graph, start): self.graph = graph self.location = start def move(self, police): min_distance = float('inf') best_location = self.location for neighbor in self.graph.vertices[self.location]: distance = self.graph.shortest_path(neighbor, police.location) if distance < min_distance: min_distance = distance best_location = neighbor self.location = best_location if __name__ == '__main__': g = Graph() g.add_vertex('A', {'B': 5, 'C': 2}) g.add_vertex('B', {'D': 4}) g.add_vertex('C', {'D': 2, 'E': 3}) g.add_vertex('D', {'F': 1}) g.add_vertex('E', {'F': 7}) g.add_vertex('F', {}) police = Police(g, 'A') thief = Thief(g, 'F') while True: police.move(thief.location) thief.move(police) print(f'Police: {police.location}, Thief: {thief.location}') if police.location == thief.location: print('Thief is caught!') break ``` 这个实现中,我们首先定义了一个 `Graph` 类,用于表示地图和计算最短路径。然后,我们定义了一个 `Police` 类和一个 `Thief` 类,分别表示警察和小偷。在每个回合中,警察会按照最短路径移动,小偷则会选择离警察最远的位置移动。如果警察和小偷在同一个位置,游戏结束。 上面的代码只是一个简单的示例,你可以根据自己的需求更改地图和初始位置,以及警察和小偷的移动策略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值