gui学习小结

 

花了2天看完了狂神的gui教程,也把贪吃蛇的小游戏做出来了,但是感觉不知道怎么做笔记,就感觉是不停的认识新类,新方法,然后用起来,但是又感觉这些新类没有记的必要,因为感觉这些类,方法以后用到的机会很少,但是不做个笔记有感觉浪费了这两天,就是这么纠结=。= 还是记录下吧,就把贪吃蛇的用到的一些东西记录下,顺便把学习过程中的一些技巧也记录下。

 

贪吃蛇方法记录


public class GameStart{
   public static void main(String[] args) {
       JFrame frame = new JFrame();//启动一个弹窗swing方法,后续所有方法都用这个执行
       frame.add(new GamePanel());   //这里时添加了一个自定义画笔类
       frame.setResizable(false);  //设置无法拉升弹窗
       frame.setVisible(true);     //设置弹窗可见
       frame.setBounds(10,10,900,720);//设置坐标,长宽
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置点击关闭按钮
  }
}


//自定义的画笔类,继承了JPanel画笔类,实现了KeyListener键盘监听类,ActionListener定时器监听类
public class GamePanel extends JPanel implements KeyListener, ActionListener {

   int length;
   int[] snakeX = new int[600];
   int[] snakeY = new int[600];
   String direction;
   boolean isstart = false;
   Timer timer = new Timer(100,this);//100毫秒刷帧
   int foodx;
   int foody;
   Random random = new Random();
   boolean isfaile = false;
   int score;


   public GamePanel() {
       init();
       this.setFocusable(true);//设置焦点
       this.addKeyListener(this);//添加键盘监听类
  }

   public void init(){
       length = 3;
       snakeX[0] = 100;snakeY[0] = 100;
       snakeX[1] = 75;snakeY[1] = 100;
       snakeX[2] = 50;snakeY[2] = 100;
       direction = "right";

       foodx = 25+25*random.nextInt(34);
       foody = 75+25*random.nextInt(24);

       score = 0;
       timer.start();//定时器启动
  }

//继承JPanel后重写的paintComponent方法
   @Override
   protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       this.setBackground(Color.white);//设置背景色
       Data.header.paintIcon(this,g,25,11);//将图片用画笔画进去
       g.fillRect(25,75,850,600);//画一个矩形

       //写出一段字
       g.setColor(Color.white);//设置字体颜色
       g.setFont(new Font("微软雅黑",Font.BOLD,18));//设置字体样式
       //写字
       g.drawString("长度:"+length,750,35);
       g.drawString("分数:"+score,750,50);


       Data.food.paintIcon(this,g,foodx,foody);


       if(direction.equals("right")){
           Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
      }else if(direction.equals("left")){
           Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
      }else if(direction.equals("up")){
           Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
      }else if(direction.equals("down")){
           Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
      }

       for (int i = 1; i < length; i++) {
           Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
      }

       if(isfaile){
           g.setColor(Color.red);
           g.setFont(new Font("微软雅黑",Font.BOLD,40));
           g.drawString("失败,请按下空格重新开始游戏!",300,300);
      }

       if(isstart==false){
           g.setColor(Color.white);
           g.setFont(new Font("微软雅黑",Font.BOLD,40));
           g.drawString("按下空格开始游戏!",300,300);
      }


  }

   //实现KeyListener重写的方法 键盘按压时要执行的操作方法在这里写
   @Override
   public void keyPressed(KeyEvent e) {
       int keyCode = e.getKeyCode();//获得键盘按下的键
       if(keyCode==KeyEvent.VK_SPACE){
           if (isfaile) {
               isfaile = false;
               init();
          }else{
               isstart = !isstart;
          }
           repaint();//重新绘制组件
      }
       if(keyCode==KeyEvent.VK_A){
           direction = "left";
      }else if(keyCode==KeyEvent.VK_W){
           direction = "up";
      }else if(keyCode==KeyEvent.VK_D){
           direction = "right";
      }else if(keyCode==KeyEvent.VK_S){
           direction = "down";
      }


  }

    //实现ActionListener重写的方法   小蛇行径过程中的动态方法在这里写
   @Override
   public void actionPerformed(ActionEvent e) {
       if(isstart&&!isfaile) {
           if(snakeX[0]==foodx&&snakeY[0]==foody){
               length++;
               score+=10;
               foodx = 25+25*random.nextInt(34);
               foody = 75+25*random.nextInt(24);
          }

           for (int i = 1; i < length; i++) {
               if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                   isfaile = true;
              }
          }


           for (int i = length-1; i >0; i--) {
               snakeX[i] = snakeX[i-1];
               snakeY[i] = snakeY[i-1];
          }
           if(direction.equals("right")){
               snakeX[0] = snakeX[0]+25;
               if(snakeX[0]>850){snakeX[0] = 25;}
          }else if(direction.equals("left")){
               snakeX[0] = snakeX[0]-25;
               if(snakeX[0]<25){snakeX[0] = 850;}
          }else if(direction.equals("up")){
               snakeY[0] = snakeY[0]-25;
               if(snakeY[0]<75){snakeY[0] = 650;}
          }else if(direction.equals("down")){
               snakeY[0] = snakeY[0]+25;
               if(snakeY[0]>650){snakeY[0] = 75;}
          }
           repaint();
      }

       timer.start();
  }



   @Override
   public void keyReleased(KeyEvent e) {

  }
   @Override
   public void keyTyped(KeyEvent e) {

  }
}


public class Data {

   public static URL headerURL= Data.class.getResource("statics/header.png"); //获取图片路径
   public static URL leftURL = Data.class.getResource("statics/left.png");
   public static URL rightURL = Data.class.getResource("statics/right.png");
   public static URL upURL = Data.class.getResource("statics/up.png");
   public static URL downURL = Data.class.getResource("statics/down.png");
   public static URL bodyURL = Data.class.getResource("statics/body.png");
   public static URL foodURL = Data.class.getResource("statics/food.png");

   public static ImageIcon header = new ImageIcon(headerURL);//将图片转换成图标
   public static ImageIcon left = new ImageIcon(leftURL);
   public static ImageIcon right = new ImageIcon(rightURL);
   public static ImageIcon up = new ImageIcon(upURL);
   public static ImageIcon down = new ImageIcon(downURL);
   public static ImageIcon body = new ImageIcon(bodyURL);
   public static ImageIcon food = new ImageIcon(foodURL);
}

 

 

 

 

学习的一些小技巧

  1. 在用到某些方法时直接点进去看,java源码里都会又很详细的注释,英语不要就直接复制百度翻译,看下来基本就能用这个类时干嘛的,怎么用的了,以前我感觉我都是硬记这些方法的=。=

  2. 用到一个类后想知道这个类中又哪些方法可以用idea左下角的structure的列表来看

posted @   sumling  阅读(475)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示