gpt4 book ai didi

java - 如何在随机坐标上绘制一个随机大小的圆,使圆完全可见?

转载 作者:行者123 更新时间:2023-11-29 05:03:31 25 4
gpt4 key购买 nike

代码:

Random rand = new Random();
JPanel mainPanel;

int randomSize = 0;
int randomPositionX = 0;
int randomPositionY = 0;

final static int FRAME_HEIGHT = 500;
final static int FRAME_WIDTH = 500;

final static int TITLE_BAR = 30 ;

final static int MAX_SIZE = 100;
final static int MIN_SIZE = 10 ;

/* All the below code is put into a method */

mainPanel = new JPanel(){
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(randomPositionY, randomPositionX, randomSize, randomSize);
}
};

do{
randomSize = rand.nextInt(MAX_SIZE) + 1;
}while(randomSize < MIN_SIZE);

do{
randomPositionX = rand.nextInt(FRAME_WIDTH);
randomPositionY = rand.nextInt(FRAME_HEIGHT);
}while((randomPositionX + randomSize > FRAME_WIDTH) || (randomPositionY + randomSize > FRAME_HEIGHT - TITLE_BAR));

repaint();

我想要的是圆的大小是随机的,最小为 10,最大为 100。圆也应该绘制在随机坐标中,这样圆 完全在 JPanel mainPanel 中可见

请注意,mainPanel 将添加到 JFrame,其大小使用 setSize(FRAME_WIDTH, FRAME_HEIGHT); 设置。

但问题是有时候,圆的一部分在 JPanel 的外面一半在里面:

SCREENSHOT

我哪里出错了?

最佳答案

  1. 当框架包含可变的高标题栏和框架插图时,您在计算圆的位置时尝试使用框架大小
  2. 您尝试使用无关紧要的框架大小,它是您应该使用的组件大小,因为它在您要绘制的 mainPanel 的坐标上下文中

So, how do I fix the issues?

丢弃所有帧“填充”/“偏移”。 mainPanel 有它自己的坐标上下文(它的左上角将是 0x0)并且它将在它父级的坐标上下文中,所以你不需要关心框架或其插图。

简化您的计算,例如...

int randomSize = MIN_SIZE + (rand.nextInt(MAX_SIZE) + 1);

int randomPositionX = rand.nextInt(getWidth() - randomSize);
int randomPositionY = rand.nextInt(getHeight() - randomSize);

应该允许您生成不会超出可视区域的结果,因为尺寸的最大范围是容器的当前大小减去所需大小,请注意,如果可用大小小于 MAX_SIZE,你会遇到问题 ;)

该示例每秒生成一个新圆

Random Circles

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public static class TestPane extends JPanel {

public final static int MAX_SIZE = 100;
public final static int MIN_SIZE = 10;
private Rectangle bounds;
private Random rand;

public TestPane() {
rand = new Random();
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

int randomSize = MIN_SIZE + (rand.nextInt(MAX_SIZE) + 1);

int randomPositionX = rand.nextInt(getWidth() - randomSize);
int randomPositionY = rand.nextInt(getHeight() - randomSize);

bounds = new Rectangle(randomPositionX, randomPositionY, randomSize, randomSize);
repaint();
}
});
timer.start();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (bounds != null) {
g2d.setColor(Color.RED);
g2d.fillOval(bounds.x, bounds.y, bounds.width, bounds.height);
}
g2d.dispose();
}

}

}

关于java - 如何在随机坐标上绘制一个随机大小的圆,使圆完全可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31199315/

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