📄 aipattern.java
字号:
/*
* AIPattern.java
*
* Created on 2007年3月22日, 下午1:47
*/
package gameai;
/**
*
* @author Administrator
*/
public class AIPattern extends javax.swing.JPanel implements Runnable{
/** Creates new form AIPattern */
private class Pattern{
int x1,y1,x2,y2;
public Pattern(int x1, int y1, int x2, int y2){
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
}
private java.util.ArrayList myPattern = new java.util.ArrayList();
private Thread my = null;
private int speed = 1;
public AIPattern() {
text();
initComponents();
}
private int bx[]={25,105,105,188,188,100, 45,25};
private int by[]={50, 50, 25, 25,250,210,150,50};
private void text(){
int i=0;
for(; i<bx.length-1; i++){
System.out.println(i);
addPattern(bx[i],by[i],bx[i+1],by[i+1]);
}
// addPattern(bx[i+1],by[i+1],bx[0],by[0]);
}
public void addPattern(int x1, int y1, int x2, int y2){
myPattern.add(new Pattern(x1,y1,x2,y2));
}
public boolean removePattern(int x1, int y1, int x2, int y2){
return myPattern.remove(new Pattern(x1,y1,x2,y2));
}
public void start(){
if(my==null){
yes = true;
my = new Thread(this);
my.start();
}
}
private boolean yes = true;
public void stop(){
if(my!=null){
yes = false;
my = null;
}
}
private int x,y;
private void patternPaint(java.awt.Graphics g){
g.setColor(java.awt.Color.red);
Pattern temp = null;
for(int i=0; i<myPattern.size(); i++){
temp = (Pattern)myPattern.get(i);
g.fillOval(temp.x1+30,temp.y1+30,5,5);
}
if(myPattern.size()!=0)
g.fillOval(temp.x2+30,temp.y2+30,5,5);
}
public void run(){
int i = 0;
Pattern temp = null;
Pattern demp = null;
java.awt.Point p;
while(yes){
for(i=0; i<myPattern.size(); i++){
temp = (Pattern)myPattern.get(i);
while(yes){
patternPaint(this.getGraphics());
try{
Thread.sleep(10);
}catch(Exception e){}
if(Math.abs(rabbit.getLocation().x-temp.x2)<=1 &&
Math.abs(rabbit.getLocation().y-temp.y2)<=1) {
break;
}
if(rabbit.getLocation().x < temp.x2){
x=rabbit.getLocation().x+speed;
}
else if(rabbit.getLocation().x > temp.x2){
x=rabbit.getLocation().x-speed;
}
if(rabbit.getLocation().y > temp.y2){
y=rabbit.getLocation().y-speed;
}
else if(rabbit.getLocation().y < temp.y2){
y=rabbit.getLocation().y+speed;
}
rabbit.setLocation(x,y);
}
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
private void initComponents() {
rabbit = new javax.swing.JLabel();
setBorder(javax.swing.BorderFactory.createTitledBorder("AIPattern"));
rabbit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gameai/rabbit.png")));
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(rabbit)
.addContainerGap(365, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(18, 18, 18)
.add(rabbit)
.addContainerGap(231, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JLabel rabbit;
// 变量声明结束//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -