[英]How to create a rectangle in Java Swing
Am very lost on how to make a rectangle or object that can be seen in the panel.我对如何制作可以在面板中看到的矩形或对象非常迷茫。 Currently have the below code but is only giving a background color.
目前有以下代码,但只给出背景颜色。 I am unsure how to call the paintComponent method or the repaint in this scenario.
我不确定在这种情况下如何调用paintComponent 方法或重绘。
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();
}
}
I am unsure how to call the paintComponent method
我不确定如何调用paintComponent 方法
You don't.你没有。 The paint sub system will call your component's "paint" methods when it decides something needs to be update, this is a passive update workflow, as a paint pass may be triggered for any number of reasons at any time.
绘制子系统将在决定需要更新某些内容时调用组件的“绘制”方法,这是一个被动更新工作流程,因为任何时候都可能出于多种原因触发绘制过程。
You can request a paint pass by calling repaint
on your component, but it's still up to the paint sub system to decide what and when a paint pass will take place.您可以通过在组件上调用
repaint
来请求绘制传递,但仍然由绘制子系统决定绘制传递的内容和时间。 repaint(Graphics g)
is badly named, however, you'd have to call it from within the paintComponent
method. repaint(Graphics g)
的名字很糟糕,但是,您必须从paintComponent
方法中调用它。
I would recommend looking at Painting in AWT and Swing and Performing Custom Painting for more details about how paint works and how you should work with.我建议查看AWT 中的绘画和 Swing和执行自定义绘画,以了解有关绘画如何工作以及应该如何使用的更多详细信息。
You might also like to have a look at How to Use CardLayout for switching between views.您可能还想看看如何使用 CardLayout在视图之间切换。
The following example uses a Overlayout
so that I can "overlay" the menu over the top the game, and basically acts as a "pause" like state.下面的示例使用了一个
Overlayout
,以便我可以将菜单“覆盖”在游戏顶部,并且基本上充当类似“暂停”的状态。
It makes use of a "delegate" to allow the menu/game change the state accordingly, but without actually needing to know about each other.它使用“委托”来允许菜单/游戏相应地更改状态,但实际上不需要相互了解。
It makes use ofSwing Timer
to animate the position of the box across the screen.它利用Swing
Timer
来为盒子在屏幕上的位置设置动画。 The nice thing about this is that it can be easily started and stopped这样做的好处是可以轻松启动和停止
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();
}
}
}
Now, obviously, if you want to control more then one object, you will need to maintain the individual states of those objects and access them from something like List
现在,显然,如果您想控制多个对象,则需要维护这些对象的各个状态并从
List
之类的东西中访问它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.