1

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. :)

Phyremaster
  • 87
  • 2
  • 8
  • What exactly does not work? What is the expected behaviour? What is the observed behaviour? Do you get an `Exception`? If so, please post the full stack trace. – Turing85 Nov 17 '15 at 22:40
  • 1
    1- Use `paintComponent` of `paint`; 2- Call the paint methods `super` method to ensure you are maintaining the painting contract; 3- Swing is single `Threaded, so your `while` could be causing issues; 4- Don't load resources within the context of any paint method; 5- Painting may occur for any number of reasons, so clearing your `List`s may result in nothing getting painted. Any paint method should paint the current state and never do any state management work – MadProgrammer Nov 17 '15 at 22:44
  • If you intention is to some animation, you might consider having a look at [this example](http://stackoverflow.com/questions/13022754/java-bouncing-ball/13022788#13022788) – MadProgrammer Nov 17 '15 at 22:45
  • I partially fixed the issue (thanks MadProgrammer), but I am still experiencing issues. Please see edits. – Phyremaster Nov 18 '15 at 23:21
  • To avoid looking like "that guy": this problem was never solved, because, quite simply, you shouldn't use Java for games and the like. Currently, I am using C++ with the SFML library to write a game without using an engine. – Phyremaster Jan 21 '20 at 23:19

1 Answers1

-1

I´ve 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...

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Dylan
  • 1
  • 1
    Welcome to SO. What you outlined is _one_ way of creating Windows in Java. The approach of the OP is valid as well, the question is about something very different. – ahuemmer Sep 07 '22 at 16:24