|
-
April 30th, 2007, 04:15 PM
#3
Re: Help with graphics
Perhaps there are some mistakes but it isn't important, I can fix it myself later. I only don't know how:
1. Send variables a, b, c from the first class to the second class
2. Create a command on the button bt that will draw the graph from the second class in the new window
Oh, and I said that i need graph a*x*x + b*x + c, and in the program is a* sin(b*x + c), but nevermind that
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class mmm extends JFrame implements ActionListener
{
private final int length = 600;
private final int height = 400;
private JTextArea ta;
private JButton bt;
private Container c1;
public mmm()
{
setSize (length, height);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Test");
c1 = getContentPane();
c1.setLayout(null);
ta = new JTextArea();
ta.setSize(sirina - 100, visina - 100);
ta.setLocation(1,1);
c1.add(ta);
bt = new JButton();
bt.setLocation(sirina - 80, 50);
bt.setSize(60, 20);
bt.addActionListener(this);
bt.setText("U redu");
c1.add(bt);
setVisible(true);
}
public void get_a_b_c()
{
String tmp = ta.getText();
String a1 = "";
String b1 = "";
String c1 = "";
String g = "";
boolean u1 = true;
boolean u2 = true;
boolean bo = true;
for (int i = 0; i < tmp.length(); i++)
{
String z = tmp.substring(i, i+1);
if (((u1 == true) && (u2 == true)) && ((z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9"))))
a1 = a1 + z;
if ((z.equals(",")) && (u1 == false))
u2 = false;
if ((z.equals(",")) && (u1 == true))
u1 = false;
if (((u1 == false) && (u2 == true)) && ((z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9"))))
b1 = b1 + z;
if (((u1 == false) && (u2 == false)) && ((z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9"))))
c1 = c1 + z;
bo = ((z.equals(",")) || (z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9")));
if (bo == false)
g = "greska!!";
}
if (g.equals("greska!!"))
{
JOptionPane.showMessageDialog (this, "Error!", "Error", JOptionPane.ERROR_MESSAGE);
}
int a = Integer.parseInt(a1);
int b = Integer.parseInt(b1);
int c = Integer.parseInt(c1);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(bt))
// Here should be the command that draws graph
}
public static void main(String[] s)
{
mmm m = new mmm();
}
}
Code:
import javax.swing.*;
import java.awt.*;
public class test2 extends JFrame
{
private final int length = 640, height = 480;
public test2()
{
setTitle ("Graph of function f (x) = " + a + " sin (" + b + " x + " + c + ")");
setSize (length, height);
setDefaultCloseOperation (EXIT_ON_CLOSE);
setBackground (Color.white);
setVisible (true);
}
private void xos (Graphics g)
{
g.setColor (Color.blue);
g.drawLine (0, height / 2, length, height / 2);
for (int i = 1; i < length/ 20; i++)
g.drawLine (i * 20, height / 2 - 2, i * 20, height / 2 + 2);
}
private void yos (Graphics g)
{
g.setColor (Color.blue);
g.drawLine (length / 2, 0, length / 2, height);
for (int i = 1; i < height / 20; i++)
g.drawLine (length / 2 - 2, i * 20, length / 2 + 2, i * 20);
}
public void crtaj (Graphics g)
{
double x = - length / 40.0, xs = length / 2.0, ys = height / 2.0, y;
int xp, yp;
g.setColor (Color.red);
while (x < length / 40)
{
y = a * x*x + b * x + c;
xp = (int)(xs + 20 * x);
yp = (int)(ys - 20 * y);
g.drawOval (xp, yp, 1, 1);
x += 0.05;
}
}
public void paint (Graphics g)
{
xos (g);
yos (g);
crtaj (g);
}
public static void main (String[] s)
{
test2 t2 = new test2 (3,3,3);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|