2

I am writing a 2D strategy game and require to be able to display hundreds of images at one time.

So far I have many classes, but I have one dilemma as I fear I do not understand the JFrame components well enough. I have written a smaller 3 class program to show my problem:

First I have a main class which constructs the frame:

import javax.swing.JFrame;

public class MainClass {

    static JFrame Frame = new JFrame("MainFrame");

    public static void main(String[] args){
    Frame.add(new Board());
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.setSize(1024, 500);
    Frame.setVisible(true);
    Frame.setResizable(false);
    }
  }

Currently the board class is the class which sits at the center of the program and pulls all of the strings to make the program run. Previously all of the tutorials I have seen on the internet have pointed towards having the paint(Graphics g) method inside the board class, but this gives me the main dilemma. Currently I have to type out manually every time I want something painted to the screen:

  public board{

 //......code....

     public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

        g2d.drawImage(b.getImage(0), 0, 0, null);//Black
        g2d.drawImage(b.getImage(1), GameMachanic.getCurrentPosX(), GameMachanic.getCurrentPosY(), null);//background
        g2d.drawImage(b.getImage(3), GameMachanic.getCurrentPosX(), GameMachanic.getCurrentPosY(), null);//background
 //...more paint to screen code
        }
   //......code......
}

This method is repainted every few milliseconds and everything is great! except... now I have gotten to the point where I need hundreds of different elements painted to the screen at one time, from different classes and with this current method U have to type out manually each and every paint command, making the code ineffective and frankly pointless.

Therefore I tried making a class that replaces the need of this manual labour, making the method reusable, but I am finding it extremely hard to make it work:

  public class GraphicsPaint extends JFrame{ 

     public void paint(Graphics g, ){
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;

        g2d.drawImage(b.getImage(0), 0, 0, null);//image
        }
}

But how do I implement this class into my code, do I add it as a component? (And if so, how?) Because I have tried this and it just throws an error at me, and I have tried using a few layouts but they just glitch up for some reason.

Do I add them to Panels? I have no idea how that would pan out..

Other than that I am clueless as I have spent the whole day looking at this problem.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
John
  • 346
  • 1
  • 11
  • 1
    "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks), for [example](http://stackoverflow.com/q/15961412/230513). – trashgod Dec 20 '14 at 09:51
  • 1
    In addition to the sound advice of @trashgod, note that `g2d.drawImage(b.getImage(0), 0, 0, null);` should better be `g2d.drawImage(b.getImage(0), 0, 0, this);` since every `JComponent` **is an** `ImageObserver`.. – Andrew Thompson Dec 20 '14 at 09:52
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Dec 20 '14 at 09:53
  • .. 4) Words typed in all lower case are hard to read, like trying to listen to someone who is mumbling. Please use an upper case letter at the start of sentences, for the word I, and proper names like `ArrayList` or Oracle. – Andrew Thompson Dec 20 '14 at 10:00
  • 1
    Separate the images into at least two categories, low chance of change (ie things like background elements) and high chance of change. Render as much of the "static" elements onto a single `BufferedImage` and paint this instead. You could do this over a series of layers, building up back/mid/foreground plates. This "should" reduce the number of images you need to paint. Render everything to a single buffer and render that to the screen, further reducing the amount of work required to push content over the hardware buffers... – MadProgrammer Dec 20 '14 at 10:30

1 Answers1

2

What is an effective way of painting hundreds of images to a JFrame at a time?

  • Put them in a collection such as an ArrayList.
  • Iterate the collection in a loop.
  • Within the loop body, paint each member of the collection in turn.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433