[英]Problem with getting an image and put in a jframe
實際上,我想在 java 中制作一條蛇,而我現在必須在我的框架中為蛇身放置圖像。 我搜索解決方案,但沒有一個有效,我嘗試了 jlabel,或者只是一個圖像,它做的事情完全相同。 在搜索了一些 github 頁面后,我發現我在其他地方遇到了問題,因為我有相同的圖像代碼。
這是我的代碼:
private ImageIcon upMouth;
private ImageIcon downMouth;
private ImageIcon leftMouth;
private ImageIcon rightMouth;
private ImageIcon body;
private ImageIcon dot;
private int[] lensnakeX = new int[750];
private int[] lensnakeY = new int[750];
private int moves = 0;
private int length = 3;
private boolean left = false;
private boolean up = false;
private boolean down = false;
private boolean right = false;
private Timer t;
private int delay = 140;
public Content(){
initGame();
}
public void initGame(){
setBackground(Color.GRAY);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
t = new Timer(delay, this);
t.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
public void doDrawing(@NotNull Graphics g){
if(moves == 0){
lensnakeX[2] = 50;
lensnakeX[1] = 75;
lensnakeX[0] = 100;
lensnakeY[2] = 100;
lensnakeY[1] = 100;
lensnakeY[0] = 100;
}
//title
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.WHITE);
g.fillRect(24,10, 851, 55);
Font f = new Font("Serif", Font.PLAIN, 60);
g2d.setFont(f);
g2d.setColor(Color.BLACK);
g2d.drawString("Snake", 375, 55);
//gameplay
g.setColor(Color.BLACK);
g.fillRect(24, 74, 851, 600);
//background
g.setColor(Color.black);
g.drawRect(25, 75, 850, 575);
for (int i = 0; i < length; i++) {
if(i == 0 && right){
rightMouth = new ImageIcon("src/resources/right.png");
rightMouth.paintIcon(this, g, lensnakeX[i], lensnakeY[i]);
}
if(i == 0 && left){
leftMouth = new ImageIcon("src/resources/left.png");
leftMouth.paintIcon(this, g, lensnakeX[i], lensnakeY[i]);
}
if(i == 0 && up){
upMouth = new ImageIcon("src/resources/up.png");
upMouth.paintIcon(this, g, lensnakeX[i], lensnakeY[i]);
}
if(i == 0 && down){
downMouth = new ImageIcon("src/resources/down.png");
Image image = downMouth.getImage();
downMouth.paintIcon(this, g, lensnakeX[i], lensnakeY[i]);
}
if(i != 0){
body = new ImageIcon("src/resources/body.png");
body.paintIcon(this, g, lensnakeX[i], lensnakeY[i]);
}
}
g.dispose();
}```
The expected result is a snake with 3 parts (1 head and 2 parts for the body), but i've only got a jframe with a black rect, and a white rect for the title with "Snake" in it. Thank you for taking time to answer.
等一下,你在測試是否出現一張圖像之前寫了所有這些? 你能給我一個主要的 function 的工作示例,以便我們進行測試嗎?
這是查看我創建的圖像的簡單代碼:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestGround extends JPanel {
private Image img;
ImageIcon ii ;
public TestGround(){
setBackground(Color.BLACK);
setFocusable(true);
// load the image in the constructor:
ImageIcon ii = new ImageIcon("src/resources/picon.png");
img = ii.getImage();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
Toolkit.getDefaultToolkit().sync();
}
private void doDrawing(Graphics g) {
//drawing a grid of lines
Graphics2D g2d = (Graphics2D) g;
// draw the image:
g.drawImage(img, 0, 0, null);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
frame.add(new TestGround());
frame.setTitle("Show Image");
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.