简体   繁体   中英

Problem with getting an image and put in a jframe

actually I want to make a snake in java, and i'm at the point where i have to put images in my frame for the snake body. I search for solutions, but none of them worked, i tried jlabel, or just an image, and it does exactly the same thing. After searching for some github pages, i found out that i've got a problem somewhere else, because i've the same code for images.

here's my code:

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.

Wait so you wrote all this before you tested if one image is appearing? can you give me a working example with the main function so we can test this?

Here is a simple code to view an image i created:

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);


    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM