class der udregner ligningen for en graf udfra 2 koordinatsæt
Jeg har lavet følgende kode i "LineEquation":import javax.swing.JFrame;
public class LineEquation
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Entry-angle equals exit-angle");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
LineEquationPanel panel = new LineEquationPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
Derudover har jeg følgende i "LineEquationPanel":
import javax.swing.JPanel;
import java.awt.*;
public class LineEquationPanel extends JPanel
{
private int ln1x1, ln1y1, ln1x2, ln1y2;
private double denominator, denominatorTest, lineOneCalc_1, lineOneCalc_2, lineOneCalc_3, lineOneCalc_4;
private String plusMinus;
public LineEquationPanel ()
{
setBackground (Color.blue);
setPreferredSize (new Dimension(400, 400));
setFont (new Font("Arial", Font.BOLD, 14));
ln1x1 = 10;
ln1y1 = 250;
ln1x2 = 350;
ln1y2 = 100;
// Figures out the formula of the line
lineOneCalc_1 = ln1x2-ln1x1; // how big an x-value
lineOneCalc_2 = ln1y2-ln1y1; // how big an y-value
denominatorTest = lineOneCalc_1;
denominator = 1;
// Finds the easiest form of the formula f.eks x+4=0
if (denominatorTest >=1)
{
while (denominatorTest != 1)
{
denominatorTest--;
denominator++;
}
}
lineOneCalc_3 = lineOneCalc_2/denominator; //Which nr to put in front of the x in the formula
lineOneCalc_4 = ln1y1+(-ln1x1*lineOneCalc_3); //Where does the line cross the y-axis
// Tjecks if the line meets y-axis at a positive or negative value, and adds a minus if necessary
if(lineOneCalc_4 < 0)
{
plusMinus = "";
}
else
{
plusMinus = "+";
}
}
public void paintComponent (Graphics page)
{
super.paintComponent (page);
page.setColor (Color.yellow);
page.drawLine (ln1x1, ln1y1, ln1x2, ln1y2);
page.drawString ("The Formula is: " +lineOneCalc_3 +"x " + plusMinus +lineOneCalc_4+" = 0", 50, 50);
}
}
Jeg vil gerne have lavet en class som laver x antal linjer udfra denne kode?
Hvordan gør jeg det.