gpt4 book ai didi

java - Swing 抽绳集中

转载 作者:行者123 更新时间:2023-12-02 05:32:56 25 4
gpt4 key购买 nike

我必须在 GUI 中水平和垂直绘制 n + 1 个圆圈。我已经成功完成了,如下所示。这样,一个二维字符串数组将在它们之间打印,并居中。

目前情况如何

enter image description here

现在我想在点的“正方形”中绘制数字。

我想要的最终结果

enter image description here

for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
canvas.drawCircle( (j + 1) * 125, (i + 1) * 125, 15, Color.white);
}
}

for (int r = 0; r < size; r++) {
for (int c = 0; c < size; c++) {
canvas.drawString(events[r][c], (r + 1) * 150, (c + 1) * 150, Color.green );
}
}

本例中的宽度为 4,因此图片中基本上有 (n-1) 个点/圆圈。

大小为 3,这只是二维数组的长度,因为本例中有 4 个圆圈,每个圆圈之间有 3 个数字

Events 是包含数字的数据的二维数组

drawCircle方法的签名是 (x,y,半径,颜色)

drawString方法的签名是 (文本、x、y 颜色)

我相信问题的一部分也在于圆圈的绘制。基本上我认为这与我用来确定圆圈和文本的 x、y 坐标的垃圾公式有关。感谢任何帮助,谢谢。

最佳答案

提供了一些我相信可以满足您需求的代码。一些常量可能会进行调整以满足您的最终要求。我使用了二维数字数组,并在绘制过程中将其转换为字符串。它还允许执行以下操作:

  • 改变球的直径或球的数量仍然会提供正确的图表(尽管它会改变它在面板)。
  • 绳子的数字随着球的大小而变化
  • 已启用抗锯齿功能以使图形更加平滑。
  • FontMetrics 用于微调数字的位置。

最后一点:因为这不是资源密集型,所以坐标是在 PaintComponent 方法中计算的。更优化的解决方案是采用Flyweight设计模式,并在进入paintComponent之前尽可能地进行预先计算。


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

public class SwingMain extends JPanel {

final static int WIDTH = 700;
final static int HEIGHT = 700;
final static int SEPARATION = 100;
final static int DIAMETER = 25;
final static int NBALLS = 4;
final static int XSTART = WIDTH / (NBALLS + 2);
final static int YSTART = HEIGHT / (NBALLS + 2);
JFrame frame = new JFrame();
int[][] numbers = new int[NBALLS - 1][NBALLS - 1];

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

public void start() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
frame.add(this);
setBackground(Color.RED);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// populate numbers in 2D array.
for (int r = 0; r < NBALLS - 1; r++) {
for (int c = 0; c < NBALLS - 1; c++) {
numbers[r][c] = r * (NBALLS - 1) + c + 1;
}
}
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;

// Allow smoothing of the graphics.
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.white);

// Iterating by the number of balls is more consistent than
// by x and y due to round of errors.

int y = YSTART;
for (int r = 0; r < NBALLS; r++) {
int x = XSTART;
for (int c = 0; c < NBALLS; c++) {
g2d.fillOval(x, y, DIAMETER, DIAMETER);
x += SEPARATION;
}
y += SEPARATION;
}

// This is the same as above except that the start begins
// halfway between the first row and column

// have the size of the font track with the diameter of the balls
g2d.setFont(new Font("ARIAL", Font.BOLD, DIAMETER));
FontMetrics fm = g2d.getFontMetrics();
y = YSTART + SEPARATION / 2;
for (int r = 0; r < NBALLS - 1; r++) {
int x = XSTART + SEPARATION / 2;
for (int c = 0; c < NBALLS - 1; c++) {
String number = Integer.toString(numbers[r][c]);

// Do some final position adjustment with the font metrics to
// center the number

int strWidth = fm.stringWidth(number);
int strHeight = fm.getAscent();
g2d.drawString(number,
x - strWidth / 2 + DIAMETER / 2,
y + strHeight);

x += SEPARATION;
}
y += SEPARATION;
}

}

}

关于java - Swing 抽绳集中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56190227/

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