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

📄 mainpanel.java~1~

📁 希望大家多多交流
💻 JAVA~1~
字号:
package system;

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

import snake.*;

public class MainPanel extends JPanel{
  private MainFrame mainFrame;
  private Image backGround;//背景图片

  public MainPanel(MainFrame frame){
    this.mainFrame = frame;
    //取得地板的图片
    this.backGround = new ImageIcon("src/images/land.png").getImage();
  }

  public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    //获得面板的高度和宽度
    int width = this.getWidth();
    int height = this.getHeight();

    //建立次画面,处理图象的闪烁
//    Image bImage = this.createImage(width,height);
//    Graphics2D g2 = (Graphics2D)bImage.getGraphics();

    //绘制地板


    //显示贪吃蛇
    Snake s = this.mainFrame.getSnake();
    if(s == null || s.size() < 1){
      return;
    }

    for(int i = 0; i < s.size(); i++){
      Block b = (Block)s.get(i);
      g2.drawImage(b.getImage(),b.getX(),b.getY(),b.WIDTH,b.HEIGHT,this);
    }

    //在开始新游戏时,绘制GO字符串
    if(this.mainFrame.isNewGame()){
      Font oldFont = g2.getFont();//保存原有字体
      Color oldColor = g2.getColor();//保存原有颜色

      //设置新的字体和颜色
      g2.setFont(new Font("Dialog",Font.BOLD,36));
      g2.setColor(new Color(255,128,0,0));

      //取得GO字符的相关信息
      FontMetrics fm = g2.getFontMetrics();
      int ascent = fm.getAscent();//上坡度
      int descent = fm.getDescent();//下坡度
      int length = fm.stringWidth("GO!");

      //计算字符串绘制的位置(在面板的中央)
      int x = (width - length)/2;
      int y = (height - (ascent + descent))/2 + ascent;

      //绘制字符
      g2.drawString("GO!",x,y);

      //恢复原有的字体和颜色
      g2.setFont(oldFont);
      g2.setColor(oldColor);

      //设置isNewGame为false,表示字符绘制完毕
      this.mainFrame.setNewGame(false);
    }

//    g.drawImage(bImage,0,0,width,height,this);
  }
}//:~zj

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -