user8234870
user8234870

Reputation:

How to fix the blinking of the screen due to loading of the Image?

I am trying to load an and Image draw on it and then display that on the frame. I can also load the image on the frame and draw on the frame but this will create more blinking on my screen.

In the below code the screen blinks when users presses any key because image is loaded again and on that the new position of ball is drawn.

if up_key y--
if down_key y++
if left_key x--
if right_key x++

based on the key pressed the image is re-displayed and ball position is updated.

How to reduce the blinking effect in-order to make it smooth?

The path which I have given in the file constructor must be replaced.

import java.awt.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.awt.event.*;

class MyFrame extends Frame implements KeyListener 
{
    BufferedImage img;
    int x=100,y=100;
    public MyFrame()
    {
        super("Box");
        this.setSize(1382,784);
        this.setVisible(true);
        this.addKeyListener(this);
        this.requestFocus();
        this.setResizable(false);
    }
    public void make()
    {
        try{
        img=ImageIO.read(new File("c:/users/udesh_2/desktop/Boundary.jpg"));
        Graphics gfx=img.getGraphics();
        gfx.setColor(Color.black);
        gfx.fillOval(x,y,40,40);
        }
        catch(IOException io){}
    }
    @Override
    public void paint(Graphics g)
    {
        make();
        g.drawImage(img,0,0,getWidth(),getHeight(),null);
    }
    public static void main(String $[])
    {
        MyFrame f=new MyFrame();
        f.make();
        f.repaint();
    }

    public void keyPressed(KeyEvent key)
    {
        boolean found=false;

        if(key.getKeyCode()==KeyEvent.VK_UP)
        {
            found=true;
            y--;
        }
        if(key.getKeyCode()==KeyEvent.VK_DOWN)
        {
            found=true;
            y++;
        }
        if(key.getKeyCode()==KeyEvent.VK_LEFT)
        {
            found=true;
            x--;
        }
        if(key.getKeyCode()==KeyEvent.VK_RIGHT)
        {
            found=true;
            x++;
        }
                    if(found)
        repaint();
    }
    public void keyTyped(KeyEvent key){}
    public void keyReleased(KeyEvent key){}
}

Upvotes: 1

Views: 140

Answers (2)

user8234870
user8234870

Reputation:

import java.awt.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.awt.event.*;

class MyFrame extends Frame implements KeyListener {

    BufferedImage img;
    Image _img;
    Graphics gfx;
    int x = 100, y = 100;

    public MyFrame() {
    super("Box");
    this.setSize(1000,780);
    this.setVisible(true);
    this.addKeyListener(this);
    this.requestFocus();
    this.setResizable(false);
    try {
        img = ImageIO.read(new File("c:/users/udesh_2/desktop/Boundary.jpg"));   
    } catch (IOException io) {
        io.printStackTrace();
    }
    _img=createImage(getWidth(),getHeight());
    gfx=_img.getGraphics();

    }

    @Override
    public void paint(Graphics g) {
        if(img!=null){
        gfx.drawImage((Image)img,0,0,getWidth(),getHeight(),this);
        gfx.fillOval(x,y,30,30);

        g.drawImage(_img, 0, 0, getWidth(), getHeight(), this);

        try
        {
            Thread.sleep(70);

        }catch(Exception e){System.out.println(e);}
        }
    }

    public static void main(String $[]) {
        MyFrame f = new MyFrame();
        f.repaint();
    }

    public void keyPressed(KeyEvent key) {
    boolean found = false;

        if (key.getKeyCode() == KeyEvent.VK_UP) {
            found = true;
            y-=2;
        }
        if (key.getKeyCode() == KeyEvent.VK_DOWN) {
            found = true;
            y+=2;
        }
        if (key.getKeyCode() == KeyEvent.VK_LEFT) {
            found = true;
            x-=2;
        }
        if (key.getKeyCode() == KeyEvent.VK_RIGHT) {
            found = true;
            x+=2;
        }
        if (found) {
            repaint();
        }
    }

    public void keyTyped(KeyEvent key) {
    }

    public void keyReleased(KeyEvent key) {
    }
}

Upvotes: 1

UkFLSUI
UkFLSUI

Reputation: 5672

You are setting value of variable img each and every time you are calling repaint() which causes the flickering. Rather initialize it once in your constructor. Then no need of extra make method also in this case. This should work:

import java.awt.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.awt.event.*;

class MyFrame extends Frame implements KeyListener {

    BufferedImage img;
    int x = 100, y = 100;

    public MyFrame() {
        super("Box");
        this.setSize(480, 480);
        this.setVisible(true);
        this.addKeyListener(this);
        this.requestFocus();
        this.setResizable(false);
        try {
            img = ImageIO.read(new File("c:/users/udesh_2/desktop/Boundary.jpg"));   
        } catch (IOException io) {
            io.printStackTrace();
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        g.setColor(Color.black);
        g.fillOval(x, y, 40, 40);
    }

    public static void main(String $[]) {
        MyFrame f = new MyFrame();
        f.repaint();
    }

    public void keyPressed(KeyEvent key) {
        boolean found = false;

        if (key.getKeyCode() == KeyEvent.VK_UP) {
            found = true;
            y--;
        }
        if (key.getKeyCode() == KeyEvent.VK_DOWN) {
            found = true;
            y++;
        }
        if (key.getKeyCode() == KeyEvent.VK_LEFT) {
            found = true;
            x--;
        }
        if (key.getKeyCode() == KeyEvent.VK_RIGHT) {
            found = true;
            x++;
        }
        if (found) {
            repaint();
        }
    }

    public void keyTyped(KeyEvent key) {
    }

    public void keyReleased(KeyEvent key) {
    }
}

Upvotes: 3

Related Questions