如何在随机坐标上绘制一个随机大小的圆,使圆完全可见?
How to paint a circle with a random size on a random coordinate such that the circle is fully visible?
代码:
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
将添加到使用 setSize(FRAME_WIDTH, FRAME_HEIGHT);
设置大小的 JFrame。
但问题是有时候,圆的一部分在 JPanel 的外面一半里面:
我哪里错了?
- 当框架包含可变的高标题栏和框架插图时,您在计算圆的位置时尝试使用框架大小
- 您尝试使用不相关的框架大小,它是您应该使用的组件大小,因为它在您要绘制的
mainPanel
的坐标上下文中
So, how do I fix the issues?
丢弃所有帧 "padding"/"offsets"。 mainPanel
有它自己的坐标上下文(它的左上角将是 0x0
)并且它将在它的 parent 的坐标上下文中,所以你不需要关心框架或其插图。
简化您的计算,例如...
int randomSize = MIN_SIZE + (rand.nextInt(MAX_SIZE) + 1);
int randomPositionX = rand.nextInt(getWidth() - randomSize);
int randomPositionY = rand.nextInt(getHeight() - randomSize);
应该允许您生成不会落在可视区域之外的结果,因为尺寸的最大范围是容器的当前大小减去所需大小,请注意,如果可用大小更小然后 MAX_SIZE
,你会遇到问题 ;)
示例每秒生成一个新圆圈
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();
}
}
}
代码:
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
将添加到使用 setSize(FRAME_WIDTH, FRAME_HEIGHT);
设置大小的 JFrame。
但问题是有时候,圆的一部分在 JPanel 的外面一半里面:
我哪里错了?
- 当框架包含可变的高标题栏和框架插图时,您在计算圆的位置时尝试使用框架大小
- 您尝试使用不相关的框架大小,它是您应该使用的组件大小,因为它在您要绘制的
mainPanel
的坐标上下文中
So, how do I fix the issues?
丢弃所有帧 "padding"/"offsets"。 mainPanel
有它自己的坐标上下文(它的左上角将是 0x0
)并且它将在它的 parent 的坐标上下文中,所以你不需要关心框架或其插图。
简化您的计算,例如...
int randomSize = MIN_SIZE + (rand.nextInt(MAX_SIZE) + 1);
int randomPositionX = rand.nextInt(getWidth() - randomSize);
int randomPositionY = rand.nextInt(getHeight() - randomSize);
应该允许您生成不会落在可视区域之外的结果,因为尺寸的最大范围是容器的当前大小减去所需大小,请注意,如果可用大小更小然后 MAX_SIZE
,你会遇到问题 ;)
示例每秒生成一个新圆圈
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();
}
}
}