java - 如何确保JPanel上绘制的形状不重叠?

标签 java swing graphics 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();
    }

最佳答案

你需要改变

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()));
}

关于java - 如何确保JPanel上绘制的形状不重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8369113/

相关文章:

Java Swing : move JButton on click

java - 在这种情况下使用方法引用有什么好处?

java - 如何使用 JDialog、JPanel 解决图形故障?

opengl - 我如何处理 OpenGL 中的透视投影?

c# - 如何创建可以放置、移动和连接项目的示意图?

java - 使用递归比较字符串以确定哪个按字母顺序排在第一位 Java

java - 如何获取父 View 之外的 View 的一部分?

Java Swing 。前面的 Windows 从 JDialog 启动

java - Swing 中 requestFocusInWindow() 和 grabFocus() 的区别

java - Graphics2D 是后现代的