Sunday, 15 February 2015

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


My problem is that I can not get the diagonal to resize properly if I run the code then the diagonal wrong side And when I make it vertical it does not keep the right diagonal from one corner to the other.

Below is my code for my program and driver. I import javax.swing.JFrame;

  public class digits {public static zero main (string [] args) {JFrame frame = new JFrame ("digits"); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Point panel panel = new pointsPanel (); . Frame.getContentPane () add (panel); Frame.pack (); Frame.setVisible (true); }}  

Here is the main program

  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 PointsPanelJeynell Extended {Private Final X 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); Setpriffer Size (new dimension (300,300)); } Public Zero Paint Component (Graphics Page) {super.paintComponent (Page); (Integer number = 0; calculation & lt; MAX_POINTS; calculation ++) {x = random.nextInt (getWidth () - 1) + 1; Y = random.exe (getHeight () - 1) + 1; X1 = x + LENGTH; Y1 = y + LENGTH; Slope = x1 / y1; C = slope * x; B = ((-1) * (Y)) - C; G = (-1) * y1; If (B & lt; = g) page.Set collar (color.red); Second page. Color.green; Page.drawLine (X, Y, x1, y1); }}}}  

I resolved your problem by making a slight change to your point-of-panel class Have done Take a look at PointPanel 2 Class. First of all the diagonal form is a linear equation of Y = Ax + B and since it passes through the coordinate (0,0) digits, then we can decrease it by y = ax so a = y / x any (X, y) point which is part of diagonal.

Event now though the drawing method uses integer coordinates We still have to calculate a coefficient as a temporary number (like a double), otherwise this round issues Will not work because of .

Then once the second part of your code runs and we get (x, y) then x1 and y1, all I have to do is calculate the diagonal y for x 1 and Comparing it with y1 and changing the color to red or green, depending on the value obtained

Use Point Points instead of PointPanel to test with your second Points 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 PointsPanel 2 enhances JPN {Private Final X MAX_POINTS = 20000; Private Final Int LENGTH = 1; Private int X, X1, Y, y1; Private random random; Double slope, B, C, G; Public PointPanel 2 () {Random = New Random (); SetBackground (Color.black); Setpriffer Size (new dimension (300,300)); } Public Zero Paint Component (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 // diagonal a coefficient double aCoff = (getHyight () * 1.0) / (getWidth () * 1.0); System.out.println ("XL, YL, A =" + getWidth () + "" + getHeight () + "" + aCoff); (Integer number = 0; calculation & lt; MAX_POINTS; calculation ++) {x = random.nextInt (getWidth () - 1) + 1; Y = random.exe (getHeight () - 1) + 1; X1 = x + LENGTH; Y1 = y + LENGTH; // Compare the diagonal image of X y1 to double diagram image = a coof * x1; Slope = x1 / y1; C = slope * x; B = ((-1) * (Y)) - C; G = (-1) * y1; // if (b & gt; = g) if (diagram image & lt; = y1) page.set color (color.red); Second page. Color.green; Page.drawLine (X, Y, x1, y1); }}}

No comments:

Post a Comment