?? drawpanel.java
字號:
import java.awt.*;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.JPanel;
public class DrawPanel extends JPanel //圖形處理,畫圖類
{
//用于設置畫圖面板布局方式
BorderLayout borderLayout1 = new BorderLayout();
private AnimatorPanel animatorPanel; //繪制動畫的面板
Animator animator;
private ImagePanel imagePanel; //繪制圖像的面板
//聲明布爾型變量v1、v2、v3,用來控制哪個桿被選擇,值為true的,對應序號的桿被選
boolean v1 = true,v2 = false,v3 = false;
private ImageIcon images,images2[];
private int currentImage = 0; //當前幀
private Timer timer; //定時器
private int x, y;
// Container container = getContentPane();
//聲明用于存儲盤子總數的變量plate_num
int plate_num;
SuperPlate superPlate[];
//聲明主類對象
MainFrame mainframe;
//聲明三個盤子類SuperPlate對象數組,用來分別保存三個桿上的盤子對象
SuperPlate superplate1[]; //柱1上盤子對象
SuperPlate superplate2[]; //柱2上盤子對象
SuperPlate superplate3[]; //柱3上盤子對象
//聲明控制游戲狀態的變量:
//win判斷是否取勝,start判斷游戲是否開始,choose判斷是否已選擇等級
boolean start=false,win=false,choose=false;
//聲明變量winNum,用來存儲最右邊桿上的盤子數
int winNum;
//用于記錄移動盤子次數
int press_num;
//構造方法,并將主類對象作為參數傳遞進來
public DrawPanel(MainFrame mainframe)
{
//獲取內容面板
// Container container = getContentPane();
//創建用于繪制圖像的面板
imagePanel = new ImagePanel();
images= new ImageIcon("1234.jpg");
images2 = new ImageIcon[4]; //創建圖像對象數組
for(int i = 0; i < images2.length; i++)
{ //載入圖像
images2[i] = new ImageIcon("boy0" + (i+1) + ".png");
}//創建圖像對象數組
//animatorPanel = new AnimatorPanel();
animator=new Animator ();
//初始化面板布局
this.setLayout(borderLayout1);
//container.add(animatorPanel, BorderLayout.CENTER);
//獲取主類對象并保存
this.mainframe = mainframe;
}
class ImagePanel extends JPanel
{
public ImagePanel()
{
super();
setBackground(Color.WHITE);
}
}
class AnimatorPanel extends JPanel implements ActionListener
{
// private ImageIcon images[];
// private int currentImage = 0; //當前幀
// private Timer timer; //定時器
// private int x = -100, y; //圖像的坐標
/* public AnimatorPanel()
{
super();
setBackground(Color.WHITE);
// animatorPanel.start();
images2 = new ImageIcon[4]; //創建圖像對象數組
for(int i = 0; i < images2.length; i++)
{ //載入圖像
images2[i] = new ImageIcon("boy0" + (i+1) + ".png");
}
}*/
/* public void paintComponent(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
//繪制圖像,坐標為屏幕中心
y = (this.getHeight() - images[currentImage].getIconHeight())/2;
images[currentImage].paintIcon(this, g, x, y);
//定時器正在運行
currentImage = (++currentImage)%4;
}*/
//開始動畫
public void start()
{
System.out.println("開始動畫");
if(timer == null)
{
currentImage = 0;
timer = new Timer(100, this); //創建定時器
timer.start();
}
else if(!timer.isRunning())
timer.restart();
//timer.stop();
//startButton.setEnabled(false);
// stopButton.setEnabled(true);
}
//動畫暫停
public void stop()
{
timer.stop();
//startButton.setEnabled(true);
//stopButton.setEnabled(false);
}
//處理定時器事件
public void actionPerformed(ActionEvent event)
{
System.out.println("前進");
x += 10;
repaint();
if(x >= 400)
timer.stop();
}
}
//畫圖方法
public void paintComponent(Graphics g)
{
// ****************先載入圖片***********************
//獲取Graphics2D對象g_2d,從而進行2D畫圖
Graphics2D g_2d = (Graphics2D)g;
g_2d.setColor(getBackground());
g_2d.fillRect(0, 0, getWidth(), getHeight());
//繪制圖像,坐標為屏幕中心
//int x = (this.getWidth() - images.getIconWidth())/2;
//
int y = (this.getHeight() - images.getIconHeight())/2;
images.paintIcon(this, g, x, y);
//用白色清屏
// g_2d.setColor(Color.WHITE);
// g_2d.clearRect(0,0,400,400);
// ****************畫柱子和盤子***********************
//游戲界面中,設置說明性文字的字體、顏色、位置及內容
BasicStroke bs2 = new BasicStroke(7f,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER);
g_2d.setStroke(bs2);
g_2d.setColor(Color.BLUE);
g_2d.drawLine(50,310,350,310);
g_2d.setColor(Color.DARK_GRAY);
g_2d.drawString("說明:光標<左><右>鍵選擇柱子,<回車>鍵移動盤子",30,330);
//游戲開始前,未選定游戲等級提示
if(choose == true)
{
g_2d.setColor(Color.RED);
g_2d.drawString("請選擇游戲等級",30,30);
}
//柱1(最左邊桿上)被選定畫圖
if(v1 == true)
{
//用紅色畫出直線,表示被選中桿
g_2d.setColor(Color.RED);
g_2d.drawLine(100,303,100,100);
g_2d.setColor(Color.BLUE);
g_2d.drawLine(200,303,200,100);
g_2d.drawLine(300,303,300,100);
}
//柱2(中間桿上)被選定畫圖
else if(v2 == true)
{
//用紅色畫出直線,表示被選中桿
g_2d.setColor(Color.RED);
g_2d.drawLine(200,303,200,100);
g_2d.setColor(Color.BLUE);
g_2d.drawLine(100,303,100,100);
g_2d.drawLine(300,303,300,100);
}
//柱2(最右邊桿上)被選定畫圖
else if(v3 == true)
{
//用紅色畫出直線,表示被選中桿
g_2d.setColor(Color.RED);
g_2d.drawLine(300,303,300,100);
g_2d.setColor(Color.BLUE);
g_2d.drawLine(100,303,100,100);
g_2d.drawLine(200,303,200,100);
}
//若游戲開始,則畫盤子
if(start == true)
{
//使用直線表示所畫盤子,設置盤子的顏色和畫盤子直線的樣式
g_2d.setColor(Color.GREEN);
BasicStroke bs1 = new BasicStroke(8f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
g_2d.setStroke(bs1);
//若柱1上有盤子時,畫柱1上的盤子
if(mainframe.s1.isEmpty() == false)
{
//按循環畫出柱1上每一個盤子,
for(int i = 0; i < mainframe.s1.size(); i++)
{
//畫盤子左半邊直線
g_2d.drawLine(superplate1[i].plate_x,
superplate1[i].plate_y,
superplate1[i].plate_x - superplate1[i].plate_width,
superplate1[i].plate_y);
//畫盤子右半邊直線
g_2d.drawLine(superplate1[i].plate_x,
superplate1[i].plate_y,
superplate1[i].plate_x + superplate1[i].plate_width,
superplate1[i].plate_y);
}
}
//若柱2上有盤子時,畫柱2上的盤子
if(mainframe.s2.isEmpty() == false)
{
for(int i = 0; i < mainframe.s2.size(); i++)
{
g_2d.drawLine(superplate2[i].plate_x,
superplate2[i].plate_y,
superplate2[i].plate_x -
superplate2[i].plate_width,
superplate2[i].plate_y);
g_2d.drawLine(superplate2[i].plate_x,
superplate2[i].plate_y,
superplate2[i].plate_x +
superplate2[i].plate_width,
superplate2[i].plate_y);
}
}
//若柱3上有盤子時,畫柱3上的盤子
if(mainframe.s3.isEmpty() == false) //畫柱3上的盤子
{
for(int i = 0; i < mainframe.s3.size(); i++)
{
g_2d.drawLine(superplate3[i].plate_x,
superplate3[i].plate_y,
superplate3[i].plate_x -
superplate3[i].plate_width,
superplate3[i].plate_y);
g_2d.drawLine(superplate3[i].plate_x,
superplate3[i].plate_y,
superplate3[i].plate_x +
superplate3[i].plate_width,
superplate3[i].plate_y);
}
}
}
//在游戲界面上畫游戲中,移動盤子次數的顯示
g_2d.setColor(Color.orange);
Font f = new Font("Dialog",Font.ITALIC,20);
g_2d.setFont(f);
g_2d.drawString("移動盤子次數為:"+String.valueOf(press_num)+"次",30,70);
//當游戲勝利后,畫游戲勝利畫面
if(win == true) //游戲勝利畫面
{
//設置字體
// Font f = new Font("Dialog",Font.ITALIC,20);
// g2d.setColor(getBackground());
//g2d.fillRect(0, 0, getWidth(), getHeight());
//繪制圖像,坐標為屏幕中心
//清屏
g_2d.setColor(Color.cyan);
g_2d.clearRect(0,0,400,400);
/* y = (this.getHeight() - images2[currentImage].getIconHeight())/2;
images2[currentImage].paintIcon(this, g, x, y);
//定時器正在運行
currentImage = (++currentImage)%4;
animatorPanel.start();
//animatorPanel.repaint();*/
setVisible(false);
mainframe.setVisible(false);
animator.setVisible(true);
g_2d.setColor(Color.BLUE);
g_2d.setFont(f);
g_2d.drawString("恭喜你,順利過關",120,200);
g_2d.drawString("移動盤子次數為:"+String.valueOf(press_num)+"次",120,250);
}
}
//初始化SuperPlate對象,用于初始化盤子數組對象
public void setPlateNum(int x)
{
this.plate_num = x;
superplate1 = new SuperPlate[plate_num];
superplate2 = new SuperPlate[plate_num];
superplate3 = new SuperPlate[plate_num];
}
//數據更新方法
public void updata()
{
//判斷柱1(最左邊桿上)是否有盤子,若有則取出全部盤子對象
if(mainframe.s1.isEmpty() == false)
{
//從vector數組中取對象
for (int i = 0; i < mainframe.s1.size(); i++)
{
superplate1[i] = null;
superplate1[i] = (SuperPlate) mainframe.s1.elementAt(i);
}
}
//判斷柱2(中間桿上)是否有盤子,若有則取出全部盤子對象
if(mainframe.s2.isEmpty() == false)
{
for (int i = 0;i < mainframe.s2.size(); i++)
{
superplate2[i] = null;
superplate2[i] = (SuperPlate) mainframe.s2.elementAt(i);
}
}
//判斷柱3(最右邊桿上)是否有盤子,若有則取出全部盤子對象
if(mainframe.s3.isEmpty() == false)
{
for (int i = 0;i < mainframe.s3.size(); i++) {
superplate3[i] = null;
superplate3[i] = (SuperPlate) mainframe.s3.elementAt(i);
}
}
//判斷游戲勝利條件,當柱3上盤子總數為plate_num,設置控制游戲勝利的變量為true
if(winNum == plate_num)
{
win = true;
// repaint();
}
repaint();
}
//取得柱3上的盤子總數
public void getWinNum(int t)
{
this.winNum = t;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -