#Problem with background while moving JFrame
1 messages · Page 1 of 1 (latest)
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);
}
}
yo
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
try setting the imagePanel X and Y to herbivorePanelX and y?
maybe thatll work
wait hmm
not sure lol not that good with gui
sry
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
no no you see the HerbivorePanel object and how you add it
its an JPanel(red square), and the background is a JFrame
the object added is the square
hm
the only thing i can think of is constantly setting the x and y to the square x and y and call paint
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
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?
the red square ir herbivorePanel which is a JPanel, i change location of it by moving, and first when it loads the background draws everywhere, but under the red square, and while the square moves, the background erases, and even if i redraw the background after the movement it doesnt make a diffrence still has the little line of the previous location
Do you call the background draw each time you move?
yes its the keyTyped() function that paints it
i also did use paint() function, it does the same thing
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.
no error
what diffrence will it make?
its listning for input