为第二个形状定义合适的坐标的最佳方法是什么,考虑到没有一个应该相互重叠?以下是计时器触发时执行的操作代码:
@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();
}
答案 0 :(得分:1)
您需要更改
while ((xSquare + squareSize) >= xRing && xSquare <= (ringSize + xRing) && (ySquare + squareSize) >= yRing && ySquare <= (ringSize + yRing)) {
xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
}