Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the below program I am just trying to move the box on the screen based on the key pressed.I have to clear the last position of the box and draw its new position .When flag is true I will clear the screen and when the flag is false I will draw it on the screen but the flag value is false its not changing inside the keyPressed(KeyEvent e) method of Shoot class.

Remove the comment statement inside the paint(Graphics g) method of Shoot class and u will find the flag is always false.

Why is it so?
Thanks in advance ;)

import java.awt.Color;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;

/**
 *
 * @author Dev Parzival
 */
public class Shoot extends JPanel implements KeyListener,WindowListener{
    int x=300,y=300;
    int width=50,height=50;
    boolean flag=false;
    @Override
    public void keyTyped(KeyEvent e) {
    }
    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            flag=true;
            repaint();
            x--;
            flag=false;
            repaint();
        }
        if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            flag=true;
            repaint();
            x++;
            flag=false;
            repaint();
        }
    }
    
    @Override
    public void paint(Graphics g){
        //System.out.println(flag);
        if(flag){
            g.setColor(getBackground());
            g.drawRect(x, y, width, height);
            g.setColor(Color.black);
        }
        else
        g.drawRect(x, y, width, height);
    }
    
    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }
    
    public static void main(String $[]){
        JFrame frame =new JFrame();
        frame.setSize(700,700);
        Shoot shoot=new Shoot();
        shoot.setSize(700,700);
        frame.addKeyListener(shoot);
        frame.addWindowListener(shoot);
        frame.add(shoot);
        frame.setVisible(true);
    }

}


What I have tried:

@Override
    public void paint(Graphics g){
        System.out.println(flag);
        if(flag){
            g.setColor(getBackground());
            g.drawRect(x, y, width, height);
            g.setColor(Color.black);
        }
        else
        g.drawRect(x, y, width, height);
    }


The flag value is always coming false.
Posted
Updated 5-Mar-20 18:15pm

Events don't happen immediately: they go into a "queue" and are answered in order (except paint - it's a low priority event, so anything more important will be moved up the queue past it).

And the messages queue is only examined once processing of the current even is finished and the event handler method has returned.
Your method calls repaint which ads a Paint message to the queue, and then sets the flag back to false - so by the time the Paint event is actioned the flag has already been reset.
Try moving the flag reset into the paint handler - that may do what you wanted.
 
Share this answer
 
Comments
Udesh-Ranjan 5-Mar-20 10:52am    
@OriginalGriff thanks for your suggestion.
I tried it now but it didn't work for me.
Can u sent me the code?
Thanks in advance ;)
OriginalGriff 5-Mar-20 10:59am    
"it didn't work for me."
"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

"Can u sent me the code?"
You are kidding, right? You can't move a line of code from one method to another for yourself?
The problem with the above code was that the flag value inside keyPressed(KeyEvent e) method is always false.
As @OriginalGriff has mentioned that the flag is always set to false;

package animation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author Dev Parzival
 */
public class MovingImage extends JPanel implements KeyListener{

    static class Shooter{
        int startx,starty;
        int width;
        int height;
        Shooter(int x,int y,int w,int h){
            startx=x;
            starty=y;
            width=w;
            height=h;
        }
    }
    Graphics2D g;
    BufferedImage img;
    Shooter shoot;
    MovingImage(){
        setSize(700,700);
        img=new BufferedImage(700,700,BufferedImage.TYPE_4BYTE_ABGR);
        g=img.createGraphics();
        shoot=new Shooter(300,300,50,50);
        g.fillRect(shoot.startx, shoot.starty, shoot.width, shoot.height);
        repaint();
    }
    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            Color back=this.getBackground();
            Color front=g.getColor();
            g.setColor(back);
            g.fillRect(shoot.startx, shoot.starty, shoot.width, shoot.height);
            g.setColor(front);
            shoot.startx++;
            g.fillRect(shoot.startx, shoot.starty,shoot.width , shoot.height);
            g.drawImage(img, 0,0, this);
            repaint();
        }
        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            Color back=this.getBackground();
            Color front=g.getColor();
            g.setColor(back);
            g.fillRect(shoot.startx, shoot.starty, shoot.width, shoot.height);
            g.setColor(front);
            shoot.startx--;
            g.fillRect(shoot.startx, shoot.starty,shoot.width , shoot.height);
            g.drawImage(img, 0,0, this);
            repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
    @Override
    public void paint(Graphics g){
        g.drawImage(img,0,0, this);
    }
    public static void main(String[] args) {
        JFrame frame =new JFrame();
        frame.setSize(700,700);
        MovingImage moving_img=new MovingImage();
        frame.add(moving_img);
        frame.addKeyListener(moving_img);
        frame.setVisible(true);
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900