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();
}
You’ll need to change