0

I want to show an image (in a JPanel) that must update quickly (about 30 fps). I also want to keep my CPU usage as low as possible.

Each image update will consist of either:

  • Moving a block of pixels to a new location
  • Replacing a block of pixels with a new block

The first solution that came to mind was something like this:

private BufferedImage screen;

public void runBlockUpdate(int x, int y, int pieceWidth, int pieceHeight byte[] piece){
    ImageIcon imgPiece = new ImageIcon(piece);
    Graphics2D g = screen.createGraphics();
    g.drawImage(imgPiece.getImage(), x, y, pieceWidth, pieceHeight, null);
    repaint();
}

@Override
public void paintComponent(Graphics g) {
    g.drawImage(screen, 0, 0, (int) (screenRect.width * screenScale), (int) (screenRect.height * screenScale), this);
}

My main performance concern regards the paint() method. I want to know if there are any more efficient ways of doing this before I fully implement this technique.

cilki
  • 125
  • 1
  • 13
  • 1
    Consider `TexturePaint`, seen [here](http://stackoverflow.com/a/16880714/230513) and [here](http://stackoverflow.com/a/24746585/230513). – trashgod Jul 04 '16 at 22:43
  • 1
    paint: "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). – trashgod Jul 04 '16 at 22:46

1 Answers1

0

You can try to use a swing timer an call the refresh method(JFrame.repaint()) every 30millisec

Irazza
  • 311
  • 2
  • 10