Move JPanel and JButton?

import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.event.*; public class Engine extends Thread {  public JFrame frame = new JFrame("drawWorld");  public JPanel panel;  public JButton startGame = new JButton("START");  public JLabel highscore = new JLabel("HIGHSCORE: ");  public JLabel score = new JLabel("CURRENT SCORE: ");  public int shift = 0;  public int bounce = 0;  public Player player;  public Engine(Player pPlayer) {    frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    frame.setLocationRelativeTo(null);    this.player = pPlayer;  }  public void startdraw(List blocks) {    panel = new JPanel() {      @Override      protected void paintComponent(Graphics g) {        List<Block> world = blocks.copy(); //Kopiert das Listenobjekt, damit es nicht zu einem doppeltem Zugriff kommt        super.paintComponent(g);        BufferedImage buffer = new BufferedImage(frame.getWidth(), frame.getHeight(), BufferedImage.TYPE_INT_ARGB);        Graphics bufferGraphics = buffer.getGraphics();        player.isUpdating = true;        if (player.dead == false) {          bufferGraphics.drawImage(player.texture, (int)frame.getWidth() / 2 - 75, (int)frame.getHeight() / 2, 150, 150, null);          world.toFirst();          while (world.getContent() != null) {            Block currentDrawing = (Block)world.getContent();            if (currentDrawing != null) {              bufferGraphics.drawImage(currentDrawing.texture, (int)currentDrawing.x*100 - shift + ((int)frame.getWidth() - 1000) / 2 - 350, (int)currentDrawing.y*100 + bounce + 45 + ((int)frame.getHeight() - 1000) / 2, (int)100, (int)100, null);            }            world.next();          }        }        score.setText("CURRENT SCORE: " + player.world.score);        highscore.setText("HIGHSCORE: " + player.world.highscore);        g.drawImage(buffer, 0, 0, null);        player.isUpdating = false;      }    };    startGame.addActionListener(new ActionListener() {        @Override        public void actionPerformed(ActionEvent e) {          player.reset();          player.dead = false;          panel.remove(startGame);          frame.requestFocusInWindow();          frame.repaint();        }      });    frame.add(panel);    panel.add(this.highscore);    panel.add(this.score);    if (player.dead) {      panel.add(this.startGame);    }    frame.setVisible(true);  }  public void update() {    frame.repaint();  } }

Hello folks,

I was wondering how I could move the objects this.highscore, this.score, and this.startGame. I've tried several options. setBounds, setHorizontal, and setVerticalAlignment didn't work. Why is that?

Thanks in advance!

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
1 Answer
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
jo135
1 year ago

A LayoutManager will take care of the placement if you don’t add the FlowLayout.

If you want to be more flexible in order, you need to select a suitable LayoutManager.

https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html