gpt4 book ai didi

java - 如何使用paintComponent()进行多线程?

转载 作者:行者123 更新时间:2023-12-01 21:09:12 24 4
gpt4 key购买 nike

我创建了一个应用程序,其中包含一个正方形,每次接触框架边缘时都会弹跳。我在运行该应用程序时没有遇到问题,问题是我不知道如何创建各种线程以便框架内有多个正方形。我尝试了多种方法,但我不知道应该在哪里创建线程。我还注意到,只有当我将其直接添加到框架内时,该正方形才可见,而当我将其放入 JPanel 内时,该正方形才可见。

方形.java

public class Square extends JComponent implements ActionListener {

int width = 20;
int height = 20;
double y = Math.random() * 360;
double x = Math.random() * 360;
boolean xMax = false;
boolean yMax = false;
boolean xMin = true;
boolean yMin = true;
Rectangle2D.Double square = new Rectangle2D.Double(x, y, width, height);

public Square() {
Timer t = new Timer(2, this);
t.start();
}

public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g);
g2.setColor(Color.BLUE);
g2.fill(square);

x_y_rules();


}
public void x_y_rules() {
if (xMax == true) {
x = x - 0.5;
if (x <= 0) {
xMax = false;
}
} else {
x = x + 0.5;
if (x >= this.getWidth()) {
xMax = true;
}
}
if (yMax == true) {
y = y - 0.5;
if (y <= 0) {
yMax = false;
}
} else {
y = y + 0.5;
if (y >= this.getHeight()) {
yMax = true;
}
}
square.setFrame(x, y, width, height);
}

@Override
public void actionPerformed(ActionEvent arg0) {
repaint();
}
}

应用程序.java

public class App extends JFrame {

public static void main(String[] args) {
JFrame jf = new JFrame();
Square sqr = new Square();
jf.setSize(400, 400);
jf.setVisible(true);
jf.add(sqr);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
}
}

尽管我在计时器中设置了时间2,但方 block 移动得很慢,这正常吗?

最佳答案

问题:

  1. 你已经有了程序逻辑,x_y_rules()方法调用,在 PaintComponent 方法内部。将其取出,因为它不属于那里,而是将其放入 Timer 的 ActionListener 代码中它所属的位置。
  2. 如果需要,您可以为每个 Square 提供自己的 Swing Timer。这实际上并不是一个线程问题,因为每个计时器的 ActionListener 将在 EDT 上运行。
  3. 在 Swing 计时器中使用两毫秒是不切实际的时间片,而且没有计时器会运行得那么快。 11 点到 13 点大概是预期或希望的最快时间。
  4. 如果您希望 Sprite 移动得更快,请在移动代码中为 delta-x 和 delta-y 指定更大的值。
  5. 您的 JComponent 没有定义首选大小,这可能是它没有显示在 JPanel 中的原因,因为默认的 FlowLayout 会将其大小设置为 [0, 0]。覆盖其 getPreferredSize()并让它返回一个合理的 Dimension 值。
  6. 您正在调用 setVisible(true)在添加所有组件之前先在 JFrame 上添加,这是禁忌。
<小时/>

Ok,i put a getPrefferedSize() inside the square class but i've encountered a problem: the squares are not "together",it's like they're bouncing on separate panels

那么你的程序结构就被破坏了。您确实不想创建单独的 Swing 组件,事实上您的 Square 类不应该扩展 JComponent 或 JPanel。而是

  • Square 应该是一个逻辑类,一个从无扩展而来的类(除了默认的 Object)。
  • 给它一个绘图方法,比如 public void draw(Graphics g) {....}
  • 创建一个扩展 JPanel 的类,例如 DrawingPanel ,并重写其paintComponent方法。
  • 为 DrawingPanel 类指定 ArrayList<Square>这样它就可以容纳多个 Square 对象。
  • 为 DrawingPanel 类提供一个 Swing Timer
  • 在DrawingPanel类的Timer中,让它更新ArrayList中所有Square的位置,然后调用repaint()
  • 在paintComponent方法中,使用for循环迭代列表中的所有Square,并调用每个Square的draw方法。

例如:

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

@SuppressWarnings("serial")
public class DrawingPanel extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private static final int TIMER_DELAY = 20;
private static final Color[] SQUARE_COLOR = { Color.BLUE, Color.CYAN, Color.DARK_GRAY,
Color.BLACK, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
Color.PINK, Color.RED, Color.YELLOW };
List<Square> squareList = new ArrayList<>();

public DrawingPanel() {
// create a bunch of squares
for (int i = 0; i < SQUARE_COLOR.length; i++) {
squareList.add(new Square(SQUARE_COLOR[i], PREF_W, PREF_H));
}

setBackground(Color.WHITE);

// create and start the timer
new Timer(TIMER_DELAY, new TimerListener()).start();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// simply draw all the squares in the list
for (Square square : squareList) {
square.draw(g);
}
}

// set size of JPanel
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// simply iterate through list and move all squares
for (Square square : squareList) {
square.move();
}
repaint(); // then repaint the GUI
}
}

private static void createAndShowGui() {
DrawingPanel mainPanel = new DrawingPanel();

JFrame frame = new JFrame("Drawing Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

// this class does *not* extend JPanel or JComponent
class Square {
public static final int WIDTH = 20;

// location of Square
private double sqrX;
private double sqrY;

// X and Y speed
private double deltaX;
private double deltaY;

// width and height of DrawingPanel JPanel
private int dpWidth;
private int dpHeight;

// image to draw
private Image image;

public Square(Color color, int dpWidth, int dpHeight) {
this.dpWidth = dpWidth;
this.dpHeight = dpHeight;

// create square at random location with random speed
sqrX = Math.random() * (dpWidth - WIDTH);
sqrY = Math.random() * (dpHeight - WIDTH);
deltaX = Math.random() * 10 - 5;
deltaY = Math.random() * 10 - 5;

// one way to draw it is to create an image and draw it
image = new BufferedImage(WIDTH, WIDTH, BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.setColor(color);
g.fillRect(0, 0, WIDTH, WIDTH);
g.dispose();
}

public void move() {

// check that we're not hitting boundaries
if (sqrX + deltaX < 0) {
deltaX = Math.abs(deltaX);
}
if (sqrX + deltaX + WIDTH >= dpWidth) {
deltaX = -Math.abs(deltaX);
}
sqrX += deltaX;

// check that we're not hitting boundaries
if (sqrY + deltaY < 0) {
deltaY = Math.abs(deltaY);
}
if (sqrY + deltaY + WIDTH >= dpHeight) {
deltaY = -Math.abs(deltaY);
}
sqrY += deltaY;

}

public void draw(Graphics g) {
int x = (int) sqrX;
int y = (int) sqrY;
g.drawImage(image, x, y, null);
}
}

关于java - 如何使用paintComponent()进行多线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41527293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com