如何确保JPanel上绘制的形状不重叠?
考虑到任何坐标都不应相互重叠,为第二个形状定义适当坐标的最佳方法是什么?这是计时器触发时执行的操作的代码:
@Override
public void actionPerformed(ActionEvent e) {
if(allowedToDrawRing || allowedToDrawSquare){
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
isOverlaped = xSquare>=xRing && xSquare<=(ringSize+xRing) && ySquare>=yRing && ySquare<=(yRing+ringSize);
while (isOverlaped) {
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
isOverlaped = xSquare>=xRing && xSquare<=(ringSize+xRing) && ySquare>=yRing && ySquare<=(yRing+ringSize);
}
setParamsRing(xRing, yRing, ringSize);
setParamsSquare(xSquare,ySquare, squareSize);
repaint();
}
}
源代码:
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.io.*;
import javax.swing.Timer;
public class mull extends JPanel implements ActionListener {
private static JPanel p;
Timer timer;
ActionListener updateProBar;
private double esize;
private double maxSize = 0;
private boolean initialize = true;
private static int squareX = 50;
private static int squareY = 50;
private static int squareSize = 200;
private static int ringX = 50;
private static int ringY = 50;
private static int ringSize = 100;
private static int frameWidth = 500;
private static int frameHeight = 500;
private int jPanelHeight;
private int jPanelWidth;
private int i = 0;
public mull() {
timer = new Timer(500, this);
timer.setInitialDelay(500);
timer.start();
}
int xRing;
int yRing;
int xSquare;
int ySquare;
public static boolean allowedToDrawRing = true;
public static boolean allowedToDrawSquare = true;
public static boolean isOverlapedOnX = true;
public static boolean isOverlapedOnY = true;
public static boolean isOverlaped = true;
@Override
public void actionPerformed(ActionEvent e) {
if (allowedToDrawRing || allowedToDrawSquare) {
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
// isOverlaped = (xSquare + squareSize * 2) < (xRing)
// || (xSquare) > (xRing + ringSize * 2)
// || (ySquare + squareSize * 2) < (yRing)
// || (ySquare) > (yRing + ringSize * 2);
//
while (xSquare >= xRing && xSquare <= (2.0 * ringSize + xRing)
&& ySquare >= yRing && ySquare <= (yRing + 2.0 * ringSize)) {
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
}
setParamsRing(xRing, yRing, ringSize);
setParamsSquare(xSquare, ySquare, squareSize);
}
repaint();
}
public void setParamsRing(int xpos, int ypos, int size) {
mull.ringX = xpos;
mull.ringY = ypos;
mull.ringSize = size;
}
public void setParamsSquare(int xpos, int ypos, int size) {
mull.squareX = xpos;
mull.squareY = ypos;
mull.squareSize = size;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (allowedToDrawRing) {
g.setColor(Color.BLACK);
g.drawOval(ringX, ringY, ringSize, ringSize);
g.setColor(Color.BLUE);
g.fillOval(ringX, ringY, ringSize, ringSize);
}
if (allowedToDrawSquare) {
g.setColor(Color.BLACK);
g.drawRect(squareX, squareY, ringSize, ringSize);
g.setColor(Color.RED);
g.fillRect(squareX, squareY, ringSize, ringSize);
}
}
public static void main(String[] args) throws InterruptedException {
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
f.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
&& (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
allowedToDrawRing = false;
}
if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
&& (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
allowedToDrawSquare = false;
}
}
});
f.add(p);
f.add(new mull());
f.setSize(frameWidth, frameHeight);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
好的,有解决方案:
@Override
public void actionPerformed(ActionEvent e) {
if (allowedToDrawRing || allowedToDrawSquare) {
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
while( !(
(xSquare + squareSize) < (xRing)
|| (xSquare) > (xRing + ringSize )
|| (ySquare + squareSize) < (yRing)
|| (ySquare) > (yRing + ringSize)
)
){
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
}
setParamsRing(xRing, yRing, ringSize);
setParamsSquare(xSquare, ySquare, squareSize);
}
repaint();
}
最后一个问题:如果计时器设置为低值,为什么我的应用程序在计时器的一些迭代后会被冻结??? (本例中为 50):
public mull() {
timer = new Timer(50, this);
timer.setInitialDelay(500);
timer.start();
}
What is the best approach to define appropriate coordinates for second shape, taking into account that none should overlap each other? Here is the code for action performed when timer fires:
@Override
public void actionPerformed(ActionEvent e) {
if(allowedToDrawRing || allowedToDrawSquare){
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
isOverlaped = xSquare>=xRing && xSquare<=(ringSize+xRing) && ySquare>=yRing && ySquare<=(yRing+ringSize);
while (isOverlaped) {
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
isOverlaped = xSquare>=xRing && xSquare<=(ringSize+xRing) && ySquare>=yRing && ySquare<=(yRing+ringSize);
}
setParamsRing(xRing, yRing, ringSize);
setParamsSquare(xSquare,ySquare, squareSize);
repaint();
}
}
Source code:
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.io.*;
import javax.swing.Timer;
public class mull extends JPanel implements ActionListener {
private static JPanel p;
Timer timer;
ActionListener updateProBar;
private double esize;
private double maxSize = 0;
private boolean initialize = true;
private static int squareX = 50;
private static int squareY = 50;
private static int squareSize = 200;
private static int ringX = 50;
private static int ringY = 50;
private static int ringSize = 100;
private static int frameWidth = 500;
private static int frameHeight = 500;
private int jPanelHeight;
private int jPanelWidth;
private int i = 0;
public mull() {
timer = new Timer(500, this);
timer.setInitialDelay(500);
timer.start();
}
int xRing;
int yRing;
int xSquare;
int ySquare;
public static boolean allowedToDrawRing = true;
public static boolean allowedToDrawSquare = true;
public static boolean isOverlapedOnX = true;
public static boolean isOverlapedOnY = true;
public static boolean isOverlaped = true;
@Override
public void actionPerformed(ActionEvent e) {
if (allowedToDrawRing || allowedToDrawSquare) {
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
// isOverlaped = (xSquare + squareSize * 2) < (xRing)
// || (xSquare) > (xRing + ringSize * 2)
// || (ySquare + squareSize * 2) < (yRing)
// || (ySquare) > (yRing + ringSize * 2);
//
while (xSquare >= xRing && xSquare <= (2.0 * ringSize + xRing)
&& ySquare >= yRing && ySquare <= (yRing + 2.0 * ringSize)) {
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
}
setParamsRing(xRing, yRing, ringSize);
setParamsSquare(xSquare, ySquare, squareSize);
}
repaint();
}
public void setParamsRing(int xpos, int ypos, int size) {
mull.ringX = xpos;
mull.ringY = ypos;
mull.ringSize = size;
}
public void setParamsSquare(int xpos, int ypos, int size) {
mull.squareX = xpos;
mull.squareY = ypos;
mull.squareSize = size;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (allowedToDrawRing) {
g.setColor(Color.BLACK);
g.drawOval(ringX, ringY, ringSize, ringSize);
g.setColor(Color.BLUE);
g.fillOval(ringX, ringY, ringSize, ringSize);
}
if (allowedToDrawSquare) {
g.setColor(Color.BLACK);
g.drawRect(squareX, squareY, ringSize, ringSize);
g.setColor(Color.RED);
g.fillRect(squareX, squareY, ringSize, ringSize);
}
}
public static void main(String[] args) throws InterruptedException {
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
f.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
&& (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
allowedToDrawRing = false;
}
if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
&& (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
allowedToDrawSquare = false;
}
}
});
f.add(p);
f.add(new mull());
f.setSize(frameWidth, frameHeight);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
OK, There is the solution:
@Override
public void actionPerformed(ActionEvent e) {
if (allowedToDrawRing || allowedToDrawSquare) {
xRing = (int) ((getWidth() - ringSize) * (Math.random()));
yRing = (int) ((getHeight() - ringSize) * (Math.random()));
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
while( !(
(xSquare + squareSize) < (xRing)
|| (xSquare) > (xRing + ringSize )
|| (ySquare + squareSize) < (yRing)
|| (ySquare) > (yRing + ringSize)
)
){
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
}
setParamsRing(xRing, yRing, ringSize);
setParamsSquare(xSquare, ySquare, squareSize);
}
repaint();
}
Last question: why my application becomes frozen after some iterations of timer if timer was set to low values ??? (in this example 50):
public mull() {
timer = new Timer(50, this);
timer.setInitialDelay(500);
timer.start();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要改变
You'll need to change