|
Graph
Описание и работа:
Программа, позволяющая строить графики линейных и квадратичных функций.
В левом верхнем углу Вы видите 5 полей ввода, и кнопку.
График строится по формуле < A/K *X*X + B/K *X + C/K> .
В первом поле (слева) Вы указываете A, во втором B, в третем C, в четвертом K,
а в пятом поле указываете степень увеличения (от 1 до бесконечности). Нажимаете кнопку... График построен.
Вы можете указать начало координат мышкой, кликнув в нужную Вам точку.
Download:
[Graph.class]
Программа:
import java.applet.*;
import java.awt.*;
public class Graph extends Applet {
double x;
double y;
double a, b, c, m;
Dimension d;
int x1, y1, si = 2;
private Button enter; // Enter Button
private TextField tf1, tf2, tf3, tf4, tf5;
public void init() {
d = size();
x1 = d.width/2;
y1 = d.height/2+15;
setLayout( new FlowLayout(FlowLayout.LEFT, 2, 2 ));
tf1 = new TextField("A", 0);
tf2 = new TextField("B", 0);
tf3 = new TextField("C", 0);
Label l = new Label(" ");
l.setBackground(Color.black);
tf4 = new TextField("", 1);
tf5 = new TextField("K", 1);
tf5.setForeground(Color.black);
tf5.setBackground(Color.gray);
enter = new Button("))>");
enter.setForeground(Color.black);
enter.setBackground(Color.lightGray);
this.add(tf1);
this.add(tf2);
this.add(tf3);
this.add(tf5);
this.add(tf4);
this.add(l);
this.add(enter);
}
public boolean mouseDown(Event evt, int px, int py) {
try {
int m1=Integer.parseInt(tf5.getText());
m = (double) (m1);
int a1=Integer.parseInt(tf1.getText());
a = (double) (a1/m);
int b1=Integer.parseInt(tf2.getText());
b = (double) (b1/m);
int c1=Integer.parseInt(tf3.getText());
c = (double) (c1/m);
si=Integer.parseInt(tf4.getText());
} catch(NumberFormatException e) {
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
}
x1 = px;
y1 = py;
repaint();
return true;
}
public boolean mouseUp(Event evt, int px, int py) {
return true;
}
public boolean action(Event event, Object obj) {
if (event.target instanceof Button) {
String label = (String) obj;
if (label.equals("))>")) {
try {
int m1=Integer.parseInt(tf5.getText());
m = (double) (m1);
int a1=Integer.parseInt(tf1.getText());
a = (double) (a1/m);
int b1=Integer.parseInt(tf2.getText());
b = (double) (b1/m);
int c1=Integer.parseInt(tf3.getText());
c = (double) (c1/m);
si=Integer.parseInt(tf4.getText());
} catch(NumberFormatException e) {
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
}
repaint();
}
}
return false;
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, d.width+1, 30);
g.setColor(Color.red);
g.drawString("0", x1 + 5, y1-5);
g.drawLine(x1, 30, x1, d.height);
g.drawLine(0, y1, d.width, y1);
for (int i = x1; i<=d.width; i=i+si) {
g.drawLine(i, y1-2, i, y1+2);
}
for (int i = x1; i>=-d.width; i=i-si) {
g.drawLine(i, y1-2, i, y1+2);
}
for (int i = y1; i<=d.height; i=i+si) {
g.drawLine(x1-2, i, x1+2, i);
}
for (int i = y1; i>=-d.height; i=i-si) {
g.drawLine(x1-2, i, x1+2, i);
}
g.setColor(Color.black);
g.fillRect(0, 0, d.width+1, 30);
for (x=-d.width/2; x<=d.width/2; x=x+0.01) {
y = a*x*x/si+ b*x +c*si;
int xx = (int)x;
int yy = (int)y;
g.fillOval(xx+x1, -yy+y1, 1, 1);
}
}
}
| |
|