package llk;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JTextArea;
import javax.swing.border.Border;
public class LLKComponent extends JComponent {
private Image image[] = new Image[12];
private Point p;
private Point p2;
private int[][] map;
private LianLianKan llk;
private boolean islink;
private boolean isnewclick;
private JDialog jd;
private JTextArea jt;
private JButton jok;
private int time=600;
private int score;
private int sub;
/**
* 构造
*/
public LLKComponent() {
sub=5;
try {
map = MapUtil.initMap(10, 10);
for (int i = 0; i < 12; i++) {
image[i] = ImageIO.read(new File("image/" + (i + 1) + ".gif"));
}
} catch (IOException e) {
e.printStackTrace();
}
llk = new LianLianKan(map);
jd = new JDialog();
// jd.setModal(true);
jt=new JTextArea();//文本,显示得分
jt.setFont(new Font("SansSerif", Font.BOLD, 20));
jt.setBounds(100, 60, 150, 50);
jt.setBackground(Color.ORANGE);
jok = new JButton();//按钮
jok.setBounds(100, 100, 130, 50);
jok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LLKComponent.this.replay();
jd.setVisible(false);
}
});
jd.add(jok);
}
/**
* 响应
*
* @param e
*/
public void domouseClicked(MouseEvent e) {
isnewclick=true;
p = p2;
p2 = e.getPoint();
if ( !p2.equals(p)) {
if(p!=null && p2 !=null){
Point temp = new Point(p.y / 60, p.x / 60);
Point temp2 = new Point(p2.y / 60, p2.x / 60);
if (temp.equals(temp2))
return;
islink = llk.link(temp, temp2);
}
//LLKComponent.this.repaint(0,0,607,605);
LLKComponent.this.repaint();
}
}
/**
* 重来
*/
public void replay() {
this.setEnabled(true);
map = MapUtil.initMap(10, 10);
llk.setMap(map);
llk.setSuccess();
llk.setPlist(null);
islink = false;
repaint();
this.setVisible(true);
time=600;
}
/**
* 给图
*/
public void paintComponent(Graphics g) {
/*
* 黑色背景
*/
g.setColor(Color.BLACK);
g.fillRect(0, 0, 607, 605);
if (image == null)
return;
for (int i = 0; i < 10; i++) {//画图片
for (int j = 0; j < 10; j++) {
if (map[i][j] != 0)
g.drawImage(image[map[i][j] - 1], i * 60, j * 60, this);
}
}
for (int i = 0; i <= 10; i++) {//画格子
g.drawLine(i * 60, 0, i * 60, 600);
g.drawLine(0, i * 60, 600, i * 60);
}
/*
* 红色线或者框
*/
g.setColor(Color.RED);
if(isnewclick){
if (islink)//画矩形
g.drawRect((p.x / 60) * 60, (p.y / 60) * 60, 60, 60);
if (p2 != null)//画矩形
g.drawRect((p2.x / 60) * 60, (p2.y / 60) * 60, 60, 60);
if (islink) {//连通画线
time+=10;//时间+10
ArrayList<Point> plist = llk.getPlist();
Point temp = null;
for (Point pp : plist) {
if (temp == null){
temp = pp;
}
else{
g.drawLine(temp.y * 60 + 30, temp.x * 60 + 30,
pp.y * 60 + 30, pp.x * 60 + 30);
}
temp = pp;
}
if (llk.isSuccess()) {
this.setVisible(false);
score+=1000-llk.getTowin()*10-200+time*10;
jt.setText("您赢了!\n 得分:"+score);
jok.setText("继续下一关!");
jd.add(jt);
jd.setTitle("您赢了");
jd.setBounds(300, 300, 300, 200);
jd.setAlwaysOnTop(true);
jd.setVisible(true);
sub+=5;
}
/*
* 半秒钟后,自动删除画的连接线
*/
new Thread(){
@Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
islink=false;
isnewclick=false;
LLKComponent.this.repaint();
}
}.start();
}
g.setColor(Color.BLUE);
g.fillRect(0, 605, 607, 30);
g.setColor(Color.CYAN);
g.drawString("单击鼠标开始!", 300, 630);
}
}
public void timeZone(Graphics g){
while(time>0&&!llk.isSuccess()){
g.setColor(Color.BLACK);
g.fillRect(0, 659, 607, 30);
time-=sub;
if(time>=250)
g.setColor(Color.GREEN);
else
g.setColor(Color.RED);
g.fillRect(0, 659, time, 30);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!llk.isSuccess()) {
this.setEnabled(false);
jd.setBounds(300, 300, 300, 200);
this.setVisible(false);
score+=1000-llk.getTowin()*10-200;
jt.setText("您输了!\n 得分:"+score);
jd.add(jt);
jok.setText("再来一盘");
jd.setTitle("输了!");
jd.setAlwaysOnTop(true);
jd.setVisible(true);
score=0;
sub=5;
}
}
}