简体   繁体   中英

How do I repaint my window? (Java)

Edit: What it's supposed to do CURRENTLY is open a new window and display the title image (over the background). When I run it, it only displays the background (no error messages or anything). And yes, I will be doing animation later, this is a test program through which I am learning to code games in Java.

Edit: I got the title image to display by following what MadProgrammer said (I didn't remove the while because I didn't know what to replace it with), but it is not displaying at the coordinates specified by my lists. Any ideas? Or does anyone know what I could substitute for the while ?

I have tried about a million different things, and I still can't even get multiple images moving on the screen. Here is my code so far:

Main class:

import java.awt.Toolkit;

import javax.swing.JFrame;

public class Game {

    static JFrame window = new JFrame();
    static GameCanvas canvas = new GameCanvas();

    public static void main(String[] args0) {

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.setTitle("Fastball");
        window.setIconImage(Toolkit.getDefaultToolkit().getImage("icon.png"));
        window.setBounds(300, 30, 805, 625);
        window.getContentPane().add(canvas);
        window.setVisible(true);
        mainMenu();

    }

    public static void mainMenu() {

        canvas.addImage("title.png", 0, 0);
        canvas.repaint();

    }

}

Canvas class:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.List;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.JComponent;

class GameCanvas extends JComponent {

    List<String> ImagePathes = new ArrayList<String>();
    List<Integer> xPositions = new ArrayList<Integer>();
    List<Integer> yPositions = new ArrayList<Integer>();

    int paintProgress;
    Image imageToPaint;

    public void addImage(String path, int x, int y) {

        ImagePathes.add(path);
        xPositions.add(x);
        yPositions.add(y);

    }

    public void clearImages() {

        ImagePathes.clear();
        xPositions.clear();
        yPositions.clear();

    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;

        Image background = Toolkit.getDefaultToolkit().getImage("background.png");
        g2.drawImage(background, 0, 0, this);

        paintProgress = 0;
        while(ImagePathes.size() > paintProgress) {

            imageToPaint = Toolkit.getDefaultToolkit().getImage(ImagePathes.get(paintProgress));
            g2.drawImage(imageToPaint, xPositions.get(paintProgress), yPositions.get(paintProgress), this);

            paintProgress++;

        }

        g2.finalize();

    }

}

Please tell me as simply as possible how to fix my code. And avoid redirecting me to other questions or whatnot, because I have already Googled this about a jillion times. Thanks ahead of time. :)

Look... Iv´e practise to make a window In java:

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

public class Window extends JFrame

{


JPanel pnl = new JPanel();

This is the way...

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