如何将不同颜色的随机点放在由对角线分隔的矩形中 (JAVA)

How to put different color random dots in a rectangle separated by a diagonal (JAVA)

我的问题是我无法正确调整对角线的大小。如果我 运行 代码对角线位于错误的一侧,并且当我垂直扩展它时,它不会保持从一个角到另一个角的完美对角线。

下面是程序和驱动程序的代码。 导入 javax.swing.JFrame;

public class Points
{

public static void main(String[] args)
{
  JFrame frame = new JFrame("Points");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  PointsPanel panel = new PointsPanel();

  frame.getContentPane().add(panel);
  frame.pack();
  frame.setVisible(true);
  }
  }

这是主程序

import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import java.awt.Color;
import java.lang.Math.*;

public class PointsPanel extends JPanel
{
 private final int MAX_POINTS = 20000;
 private final int LENGTH = 1;
 private int x,x1,y,y1;
 private Random random;
 double slope,b,c,g;


 public PointsPanel(){
  random = new Random();
  setBackground(Color.black);
  setPreferredSize(new Dimension(300,300));

 }


 public void paintComponent(Graphics page)
 {
  super.paintComponent(page);
 for(int count = 0; count < MAX_POINTS; count++)
 {

  x = random.nextInt(getWidth()-1) + 1;

  y = random.nextInt(getHeight()-1) + 1;

  x1= x + LENGTH;
  y1= y + LENGTH;

  slope = x1/y1;
    c = slope*x;
    b = ((-1)*(y))-c;
    g = (-1)*y1;

  if (b <= g)

     page.setColor(Color.red);
  else
     page.setColor(Color.green);
     page.drawLine(x,y,x1,y1);
 }
  }
 }

我对您的 PointsPanel class 做了一点改动,解决了您的问题。看看 PointsPanel2 class。 首先,对角线是 y = aX + b 形式的线性方程,由于它经过坐标点 (0,0),我们可以将其简化为 y = aX,因此 a = Y/X 对于作为对角线一部分的任何 (X,Y) 点。

现在虽然绘图方法使用整数坐标我们仍然必须将 a 系数计算为浮点数(如双精度),否则由于舍入问题将无法工作.

然后一旦您的代码的另一部分运行并且我们获得 (x,y) 然后 x1 和 y1,我所要做的就是计算 x1 的相应对角线 y 并将其与 y1 进行比较,并根据获取值,将颜色更改为红色或绿色

使用这个 PointsPanel2 class 而不是 PointsPanel 来测试你的其他点 class

import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import java.awt.Color;
import java.lang.Math.*;

public class PointsPanel2 extends JPanel
{
     private final int MAX_POINTS = 20000;
     private final int LENGTH = 1;
     private int x,x1,y,y1;
     private Random random;
     double slope,b,c,g;


     public PointsPanel2(){
     random = new Random();
     setBackground(Color.black);
     setPreferredSize(new Dimension(300,300));

 }


 public void paintComponent(Graphics page)
 {
      super.paintComponent(page);
      // Diagonal equation
      // X0, Y0 = (0,0)
      // Xl, Yl = width, height
      // Equation y = aX + b
      // 0 = 0 + b ==> b = 0
      // Yl = aXl ==> a = Yl/Xl

      // Computes the diagonal a coefficient
      double aCoeff = (getHeight()*1.0)/(getWidth()*1.0);
      System.out.println("Xl,Yl, a = " + getWidth() + " " + getHeight() + "   " + aCoeff);

      for(int count = 0; count < MAX_POINTS; count++)
      {

          x = random.nextInt(getWidth()-1) + 1;

          y = random.nextInt(getHeight()-1) + 1;

          x1= x + LENGTH;
          y1= y + LENGTH;

          // Computes the diagonal image of x in order to compare it with y1
          double diagImage = aCoeff * x1;

          slope = x1/y1;
          c = slope*x;
          b = ((-1)*(y))-c;
          g = (-1)*y1;

          //if (b <= g)
          if (diagImage <= y1)
              page.setColor(Color.red);
          else
              page.setColor(Color.green);
          page.drawLine(x,y,x1,y1);
        }
     }
 }