我非常不知道如何制作一个可以在面板中看到的矩形或对象。目前有下面的代码,但只是给一个背景颜色。我不确定在这种情况下如何调用paintComponent方法或repaint。
public class myProject extends JPanel {
public final int WIDTH= 1000, HEIGHT= 1000;
public Rectangle user;
public static myProject myProject;
public static void main(String[] args) {
myProject= new myProject();
myProject.start();
}
public void start() {
JFrame main= new JFrame("Main");
JPanel game= new JPanel();
main.add(game);
main.setSize(WIDTH, HEIGHT);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setVisible(true);
JLabel welcome= new JLabel("Welcome");
welcome.setSize(new Dimension(50, 50));
welcome.
game.add(welcome, BorderLayout.NORTH);
game.setBackground(Color.BLACK);
JButton startButton= new JButton("Start");
startButton.setFont(startButton.getFont().deriveFont(20.0f));
startButton.setLayout(new BorderLayout());
startButton.addActionListener((ActionEvent ae) -> startClicked());
game.add(startButton, BorderLayout.CENTER);
startButton.setVisible(true);
}
public void repaint(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.green);
g.fillRect(0, HEIGHT - 50, WIDTH, 50);
g.setColor(Color.red);
g.fillRect(user.x, user.y, user.width, user.height);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawRect(10, 10, 150, 40);
}
public void startClicked() {
System.out.println("Game Started");
repaint();
}
}
我不确定如何调用paintComponent方法
你不。paint子系统将调用组件的"paint"。方法,当它决定某些东西需要更新时,这是一个被动更新工作流,因为油漆传递可能在任何时候因任何数量的原因被触发。
您可以通过调用组件上的repaint
请求油漆通过,但是它仍然取决于油漆子系统来决定什么以及何时进行油漆通过。repaint(Graphics g)
的命名很糟糕,但是,您必须从paintComponent
方法中调用它。
我建议您查看AWT和Swing中的绘画和执行自定义绘画,以了解有关绘画如何工作以及如何使用的更多细节。
你可能还想看看如何使用CardLayout在视图之间切换。
下面的例子使用了Overlayout
,这样我就可以"覆盖"&;菜单位于游戏上方,基本上起到了"暂停"的作用。像状态。
它使用了"delegate"允许菜单/游戏相应地改变状态,但实际上不需要知道彼此。
它使用SwingTimer
来动画显示框在屏幕上的位置。这样做的好处是它可以很容易地启动和停止
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
import javax.swing.Timer;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
public interface Controller {
public void startGame();
public void pauseGame();
}
private GamePane gamePane;
private MenuPane menuPane;
public MainPane() {
setLayout(new OverlayLayout(this));
Controller controller = new Controller() {
@Override
public void startGame() {
menuPane.setVisible(false);
gamePane.start();
}
@Override
public void pauseGame() {
menuPane.setVisible(true);
gamePane.stop();
}
};
gamePane = new GamePane(controller);
menuPane= new MenuPane(controller);
add(menuPane);
add(gamePane);
}
}
public class MenuPane extends JPanel {
private MainPane.Controller controller;
public MenuPane(MainPane.Controller controller) {
this.controller = controller;
setOpaque(false);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
JLabel label = new JLabel("Welcome");
label.setForeground(Color.WHITE);
add(label, gbc);
JButton startButton = new JButton("Start");
add(startButton, gbc);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
controller.startGame();
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(0, 0, 0, 192));
g.fillRect(0, 0, getWidth(), getHeight());
}
}
public class GamePane extends JPanel {
private MainPane.Controller controller;
private Timer timer;
private Rectangle box = new Rectangle(0, 0, 50, 50);
public GamePane(MainPane.Controller controller) {
this.controller = controller;
setBackground(Color.RED);
box.y = (800 - box.height) / 2;
timer = new Timer(5, new ActionListener() {
private int xDelta = 1;
@Override
public void actionPerformed(ActionEvent e) {
box.x += xDelta;
box.y = (getHeight() - box.height) / 2;
if (box.x < 0) {
box.x = 0;
xDelta *= -1;
} else if (box.x + box.width > getWidth()) {
box.x = getWidth() - box.width;
xDelta *= -1;
}
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
controller.pauseGame();
}
});
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.BLUE);
g2d.fill(box);
g2d.dispose();
}
}
}
现在,很明显,如果你想控制多个对象,你需要维护这些对象的单独状态,并从List