简体   繁体   中英

How can I move a graphics element on my java panel?

I am currently working on a simple pong-like game, but I got stucked with positioning the rectangle. I want to change it's Y position to be at the half of the actual panel's height.

package com.game;
import javax.swing.*;
import java.awt.*;

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame("gEngine");
        Player playerOne = new Player(frame);
        Player playerTwo = new Player(frame);
    }
}


package com.game;

import javax.swing.*;
import java.awt.*;

public class Player {
    public Player(JFrame frame) {
        MyPanel panel = new MyPanel();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.BLACK);
        frame.setSize(1280, 720);
        frame.setVisible(true);
    }
}
class MyPanel extends JPanel {
    public void paintComponent(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(50, 60, 20, 120);
    }
}

Your code has a lot of "problems". I suggest you to find some kind of tutorials or something. You frame.setVisible(true) and stuff inside Player 's class constructor. Do you realize that every time you create a Player object, all these things will be applied to the JFrame ? Is this necessary? Maybe you should do them only once. Also in order to paint the compnent according to its position according size, you can do g.fillRect(50, getHeight() / 2, 20, 120);

public class Test {

    public static void main(String[] args) {
        JFrame frame = new JFrame("gEngine");
        Player playerOne = new Player();
        Player playerTwo = new Player();

        // Set the proper layout manager
        frame.setLayout(new GridLayout());

        frame.add(playerOne.getMyPanel());
        frame.add(playerTwo.getMyPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.BLACK);
        frame.setSize(1280, 720);
        frame.setVisible(true);
    }

    public static class Player {
        private JPanel myPanel;

        public Player() {
            this.myPanel = new MyPanel();
        }

        public JPanel getMyPanel() {
            return myPanel;
        }

    }

    static class MyPanel extends JPanel {
        @Override
        public void paintComponent(Graphics g) {
            // let the component be painted "natural"
            super.paintComponent(g);
            // Do custom painting
            g.setColor(Color.WHITE);
            g.fillRect(50, getHeight() / 2, 20, 120);
        }
    }

}

Edit (based on comment):

The background is the default one because GridLayout splits the screen into 2 panels (in the middle of the frame). Even the frame has BLACK background, these 2 panels cover it. So the background you see is from the panels, and not from the frame. In order to change it you will have to change the background of the panels (and make them non-transparent):

static class MyPanel extends JPanel {
    public MyPanel() {
        super();
        setOpaque(true);
        setBackground(Color.BLACK);
    }

    @Override
    public void paintComponent(Graphics g) {
        // let the component be painted "natural"
        super.paintComponent(g);
        // Do custom painting
        g.setColor(Color.WHITE);
        g.fillRect(50, getHeight() / 2, 20, 120);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM