⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawpanel.java

📁 。使用Java语言来开发一个简单的游戏一直以来是我的想法
💻 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 + -