Ivan Cern
Ivan Cern

Reputation: 41

Acceleration of output images

Hello) There was a question whether it is possible to speed up the code as that which is below:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

public class Game extends Canvas {

    private static final long serialVersionUID = 1L;

    private static final int WIDTH = 400;
    private static final int HEIGHT = 400;

    @Override
    public void paint(Graphics g) {

        super.paint(g);

        int w = 20;
        int h = 20;
        int type = BufferedImage.TYPE_INT_ARGB;

        BufferedImage image = new BufferedImage(w, h, type);
        Graphics2D g2d = image.createGraphics();
        g2d.setColor(Color.GREEN);
        g2d.fillRect(0, 0, w, h);
        g2d.dispose();
        int MapWidth = image.getWidth(this);
        int MapHeight = image.getHeight(this);

        for (int s = MapWidth - MapWidth; s < MapWidth * 10; s++) {
            for (int i = MapHeight - MapHeight; i < MapHeight * 10; i++) {
                g.drawImage(image, s, i, (int) w, (int) h, this);
            }
        }

    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();

                frame.setSize(WIDTH, HEIGHT);
                frame.add(new Game());

                frame.setVisible(true);
            }
        });
    }
}

He draws pictures for so long. How can it speed up? Help please)

And one program can not be closed until the drawn image. It is necessary to accelerate their drawing

Upvotes: 0

Views: 45

Answers (2)

libik
libik

Reputation: 23029

It is slow, because in your

    for (int s = 0; s < MapWidth * 10; s++) {
        for (int i = 0; i < MapHeight * 10; i++) {
            g.drawImage(image, s, i, (int) w, (int) h, this);
        }
    }

For 400*400 MapWidth, MapHeight you are painting 4000*4000 objects, so you paint 16 000 000 objects, it should be slow.

If you replace it with this, it will be fast as hell :) :

    for (int s = 0; s < MapWidth * 10; s += w) {
        for (int i = 0; i < MapHeight * 10; i += h) {
            g.drawImage(image, s, i, (int) w, (int) h, this);
        }
    }

Upvotes: 1

shreyansh jogi
shreyansh jogi

Reputation: 2102

Just Try below code

import java.awt.Canvas;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

public class Game extends Canvas {

    private static final long serialVersionUID = 1L;

    private static final int WIDTH = 400;
    private static final int HEIGHT = 400;

    @Override
    public void paint(Graphics g) {

        super.paint(g);

        int w = 20;
        int h = 20;
        int type = BufferedImage.TYPE_INT_ARGB;

        BufferedImage image = new BufferedImage(w, h, type);
        Graphics2D g2d = image.createGraphics();
        g2d.setColor(Color.GREEN);
        g2d.fillRect(0, 0, w, h);
        g2d.dispose();
        int MapWidth = image.getWidth(this);
        int MapHeight = image.getHeight(this);

        for (int s = MapWidth - MapWidth; s < MapWidth * 10; s++) {
            for (int i = MapHeight - MapHeight; i < MapHeight * 10; i=i+10) {
                g.drawImage(image, s, i, (int) w, (int) h, this);
            }
        }

    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();

                frame.setSize(WIDTH, HEIGHT);
                frame.add(new Game());

                frame.setVisible(true);
            }
        });
    }
}

Upvotes: 0

Related Questions