#Problem with background while moving JFrame

1 messages · Page 1 of 1 (latest)

fringe crestBOT
#

<@&987246487241105418> please have a look, thanks.

quartz laurel
#

use the 2nd response

#
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        try {
            f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("test.jpg")))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        f.pack();
        f.setVisible(true);
    }

}
agile pumice
#

when i move theres a white line, and thats the ammount the frame moved, how can i make it not do that

#
public class MainScreen extends JFrame implements KeyListener {
    Herbivore herbivorePanel;
    Image image;

    MainScreen() {
        image = new ImageIcon(getClass().getResource("resources\\grassImage.png")).getImage();

        this.setTitle("Main Screen");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(1000, 1000);
        this.setLocationRelativeTo(null);
        this.setLayout(null);
        this.addKeyListener(this);
        this.setResizable(false);

        herbivorePanel = new Herbivore();

        this.add(herbivorePanel);
        this.setVisible(true);
    }

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

        g2d.drawImage(image, 0, 0, this);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        paint(this.getGraphics());
    }


    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyChar()) {
            case 'w':
                System.out.println("w");
                herbivorePanel.setLocation(herbivorePanel.getX(), herbivorePanel.getY()-10);
                break;
            case 's':
                System.out.println("s");
                herbivorePanel.setLocation(herbivorePanel.getX(), herbivorePanel.getY()+10);
                paint(herbivorePanel.getGraphics());
                break;
            case 'a':
                System.out.println("a");
                herbivorePanel.setLocation(herbivorePanel.getX()-10, herbivorePanel.getY());
                break;
            case 'd':
                System.out.println("d");
                herbivorePanel.setLocation(herbivorePanel.getX()+10, herbivorePanel.getY());
                break;
        }
    }
}```
#

its re-drawing before i move and when i move the line of where the previous position is stays

#

i tried to put "paint(this.getGraphics());" after changing the location but it didnt change anything

#

Problem with background while moving JFrame

quartz laurel
#

maybe thatll work

#

wait hmm

#

not sure lol not that good with gui

#

sry

agile pumice
#

the image panel is herbivorepanel

#

the same thing

#

yea ui in java is stupid

#

ive done allot of langauges and apps and java has the worst ui making

quartz laurel
quartz laurel
#

that isnt imagePanel or is it?

#

idts

agile pumice
#

its an JPanel(red square), and the background is a JFrame

#

the object added is the square

quartz laurel
#

hm

#

the only thing i can think of is constantly setting the x and y to the square x and y and call paint

agile pumice
#

well im doing that every time the square changes, i also made it so literly one line of code below the square location change it redraws it and still leaves that line for some reason

#

like it would of first redraw then move the square

#

even tho its the other way around

thorny mantle
#

So you move a red square image named test, while moving it, a white line appears on your background?

#

In your panel, in the overloaded draw method, do you draw the background then draw the red square? Then in your movement listener, you call your panel draw?

agile pumice
thorny mantle
#

Do you call the background draw each time you move?

agile pumice
#

i also did use paint() function, it does the same thing

thorny mantle
#

Do you have a 1 file reproduction exemple of the error you could send?

#

Also, public class MainScreen extends JFrame implements KeyListener what are you doing remaking key listener? Read about .getActionMap .getInputMap You will be able to add key actions.

#

if you really want to use addKeyListener, java addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { super.keyPressed(e); } }); KeyAdapter is the way to do it.

agile pumice
#

its listning for input