仿佛昨天. 2022-05-14 16:50 采纳率: 25%
浏览 91
已结题

JFrame运行后为什么是空白的,附码

package snake_game;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class SnakePanel extends JPanel implements ActionListener {
ImageIcon title = new ImageIcon("images/title.jpg");
ImageIcon body = new ImageIcon("images/body.png");
ImageIcon down = new ImageIcon("images/down.png");
ImageIcon food = new ImageIcon("images/food.png");
ImageIcon left = new ImageIcon("images/left.png");
ImageIcon right = new ImageIcon("images/right.png");
ImageIcon up = new ImageIcon("images/up.png");

int score = 0;
int length = 3;
Random rd = new Random();
int foodx = 20 + 25*rd.nextInt(34);
int foody = 80 + 25*rd.nextInt(24);


int[] snakex = new int[750];
int[] snakey = new int[750];

String direct = "R";

boolean isStart = false ;
boolean isFail = false;

Timer timer = new Timer(100,this);


public SnakePanel() {
    this.setFocusable(true);
    init();
    timer.start();
    
    addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            
                if(e.getKeyCode() == KeyEvent.VK_SPACE) {
                if(isFail) {
                    isFail = false;
                    init();
                }else{
                    isStart = !isStart;
                }
        }else if(e.getKeyCode() == KeyEvent.VK_UP && isStart) {
            direct = "U";
        }else if(e.getKeyCode() == KeyEvent.VK_DOWN && isStart) {
            direct = "D";
        }else if(e.getKeyCode() == KeyEvent.VK_LEFT && isStart) {
            direct = "L";
        }else if(e.getKeyCode() == KeyEvent.VK_RIGHT && isStart) {
            direct = "R";
        }
    }            
});

}

public void init(){
    score = 0;
    length = 3;
    snakex[0] = 95;
    snakey[0] = 105;
    snakex[1] = 70;
    snakey[1] = 105;
    snakex[2] = 45;
    snakey[2] = 105;
    
    direct = "R";
    
    isStart = false;
    isFail = false;
    
}

public void painnt(Graphics g) {
    super.paint(g);
    g.drawImage(title.getImage(), 20 , 10 , this);
    g.setColor(Color.WHITE);
    g.fillRect(20 , 80, 850, 600);
    g.setColor(Color.BLACK);
    for(int i = 0; i<25; i++) {
        g.drawLine(20, 80+i*25, 870, 80+i*25);
        
    }
    for(int i = 0; i < 35; i++) {
        g.drawLine(20+i*25, 80, 20+i*25, 680);
    }
    
    if("R".equals(direct)) {
        right.paintIcon(this, g, snakex[0], snakey[0]);
    }else if("L".equals(direct)) {
        left.paintIcon(this, g, snakex[0], snakey[0]);
    }else if("D".equals(direct)) {
        down.paintIcon(this, g, snakex[0], snakey[0]);
    }else if("U".equals(direct)) {
        up.paintIcon(this, g, snakex[0], snakey[0]);
    }
    
    
    
    
    
    for(int i = 1; i < length; i++) {
        body.paintIcon(this, g, snakex[i], snakey[i]);
    }
    
    if(!isStart) {
        g.setFont(new Font("华文行楷",Font.BOLD,40));
        g.setColor(Color.blue);
    }
    
    
    
    right.paintIcon(this, g, snakex[0], snakey[0]);
    body.paintIcon(this, g, snakex[1], snakey[1]);
    body.paintIcon(this, g, snakex[2], snakey[2]);
    
    g.setFont(new Font("华文行楷",Font.BOLD,40));
    g.setColor(Color.BLUE);
    g.drawString("按空格开始或暂停...",280,330);
    
    g.setColor(Color.WHITE);
    g.setFont(new Font("黑体",Font.BOLD,20));
    g.drawString("分数:" + score, 750, 34);
    g.drawString("长度" + length , 750, 60);
    
    food.paintIcon(this, g, foodx, foody);
    
}



@Override
public void actionPerformed(ActionEvent e) {
    if(isStart && !isFail) {
        for(int i = length ; i > 0;i--) {
            snakex[i] = snakex[i-1];
            snakey[i] = snakey[i-1];
        }
        if("U".equals(direct)) {
            if(snakey[0] <= 80) {
                snakey[0] = 680;
            }
            snakey[0] = snakey[0] -25;
        }else if("D".equals(direct)) {
            if(snakey[0] >= 655) {
                snakey[0] = 55;
            }
            snakey[0] = snakey[0] + 25;    
        }else if("L".equals(direct)) {
            if(snakex[0] <= 20) {
                snakex[0] = 870;
            }
            snakex[0] = snakex[0] -25;
        }else if("R".equals(direct)) {
            if(snakex[0] >= 845) {
                snakex[0] = -5;
            }
            snakex[0] = snakex[0] + 25;
        }
        
        if(snakex[0] == foodx && snakey[0] == foody) {
            length++;
            score++;
            
            foodx = 20 + 25 *rd.nextInt(34);
            foody = 80 + 25 *rd.nextInt(24);
        }
        for(int i = 1 ; i< length ; i++) {
            if(snakex[0] == snakex[i] && snakey[0] == snakey[i]) {
                isFail = true;
            }
        }
    }
    repaint();
    
}

}

package snake_game;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class SnakeFrame extends JFrame{
public SnakeFrame() {
//SnakePanel panel = new SnakePanel();
//add(panel);
add(new SnakePanel());

    this.setTitle("贪吃蛇大作战");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(905,730);
    setVisible(true);
}
public static void main (String[] args) {
    new SnakeFrame();
    
}        

}

  • 写回答

1条回答 默认 最新

  • 溪风沐雪 2022-05-14 17:02
    关注

    暂时先解决不显示的问题,时因为单词拼写错误
    public void paint(Graphics g) {
    paint写成了painnt,多了一个n

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月16日
  • 已采纳回答 5月14日
  • 创建了问题 5月14日

悬赏问题

  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥60 如何鉴定微信小程序数据被篡改过
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)