I get school project to make graph from polynomial function(example. 2x^2+2x+4). That use JFrame with paintComponent and I get into wall with making line for x's from result of polynomial function. That will be in area of xmin,xmax,ymin and ymax and after that mouselistener will show point where my mouse cursor is on the graph with point of it on line x and line y.(Example of done program)
EDIT:
What I need to do to get access to variables from class Wykres. Making Wykres wykres = new Wykres()
; make program stuck. Do I need to variables from actionperformed add to new class Silnik with method that calculate polynomial function and if how I can achieve that?
Help will be appreciated.
package zad8v2;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Collections;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
public class Draw {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Wykres();
}
});
}
}
class Wykres implements ActionListener {
JTextField f1, f2, f3, f4, f5;
double a, b, c, delta, x1, x2, xmin, xmax, ymin, ymax;
public Wykres() {
// tworzenie
JFrame jf = new JFrame();
Rysowanie rys = new Rysowanie();
JPanel jp = new JPanel();
f1 = new JTextField();
f2 = new JTextField();
f3 = new JTextField();
f4 = new JTextField();
f5 = new JTextField();
JPanel pomoc = new JPanel();
pomoc.setLayout(new GridLayout(1, 4));
pomoc.add(f1);
pomoc.add(f2);
pomoc.add(f3);
pomoc.add(f4);
jf.setLayout(new BorderLayout());
jf.add(rys, BorderLayout.NORTH);
jf.add(pomoc, BorderLayout.CENTER);
jf.add(f5, BorderLayout.SOUTH);
f5.addActionListener(this);
jf.pack();
// Podstawa
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public double getXmin() {
return xmin;
}
public void setXmin(double xmin) {
this.xmin = xmin;
}
public double getXmax() {
return xmax;
}
public void setXmax(double xmax) {
this.xmax = xmax;
}
public double getYmin() {
return ymin;
}
public void setYmin(double ymin) {
this.ymin = ymin;
}
public double getYmax() {
return ymax;
}
public void setYmax(double ymax) {
this.ymax = ymax;
}
@Override
public void actionPerformed(ActionEvent e) {
/*
* if (f1.getText() == "" && f2.getText() == "" && f3.getText() == "" &&
* f4.getText() == "") { f1.setText("Wpisz xmin"); f2.setText("Wpisz xmax");
* f3.setText("Wpisz ymin"); f4.setText("Wpisz ymax"); } else {
*/
String f1s = f1.getText();
String f2s = f2.getText();
String f3s = f3.getText();
String f4s = f4.getText();
double xmin = Double.parseDouble(f1s);
double xmax = Double.parseDouble(f2s);
double ymin = Double.parseDouble(f3s);
double ymax = Double.parseDouble(f4s);
String wielomian = f5.getText();
String[] czesci = wielomian.split("x\\^\\d+\\+?");
String as = czesci[0];
String bs = czesci[1];
String cs = czesci[2];
a = Double.parseDouble(as);
b = Double.parseDouble(bs);
c = Double.parseDouble(cs);
System.out.println(f1s);
System.out.println(f2s);
System.out.println(f3s);
System.out.println(f4s);
System.out.println(a);
System.out.println(b);
System.out.println(c);
if (a == 0) {
f5.setText(f5.getText() + "Nie jest to równanie kwadratowe");
f5.setBackground(Color.RED);
} else {
delta = Math.pow(b, 2) - 4 * a * c;
if (delta < 0) {
f5.setText(f5.getText() + "Brak miejsc zerowych");
f5.setBackground(Color.RED);
} else if (delta == 0) {
x1 = (-b) / 2 * a;
String s1 = String.valueOf(x1);
f5.setBackground(Color.WHITE);
f5.setForeground(Color.BLUE);
} else if (delta > 0) {
x1 = (-b - Math.sqrt(delta)) / (2 * a);
String s1 = String.valueOf(x1);
x2 = (-b + Math.sqrt(delta)) / (2 * a);
String s2 = String.valueOf(x2);
f5.setForeground(Color.BLUE);
f5.setBackground(Color.WHITE);
System.out.println(s1 + " " + s2);
}
}
}
}
class Silnik {
public Silnik() {
}
}
class Rysowanie extends JPanel {
int yCross;
int xCross;
public Rysowanie() {
setPreferredSize(new Dimension(400, 400));
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
setSize(new Dimension(400, getHeight()));
super.componentResized(e);
repaint();
}
});
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
Point point = e.getPoint();
Rysowanie.this.xCross = point.x;
Rysowanie.this.yCross = point.y;
Rysowanie.this.repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
}
});
}
public void paintComponent(Graphics g) {
Wykres wykres = new Wykres();
double xmin = wykres.getXmin();
double xmax = wykres.getXmax();
/*
* g.drawLine(Xmin, Ymax, Xmax, Ymax); g.drawLine(Xmin, Ymax, Xmin, Ymin);
*/
g.clearRect(0, 0, 400, 400);
g.setColor(Color.BLUE);
g.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
g.setColor(Color.BLACK);
g.drawLine((int) (getWidth() / 2), 0, (int) (getWidth() / 2), getHeight());
g.setColor(Color.RED);
/*
* g.drawLine((int) (getWidth() / Math.abs(xmin) + Math.abs(xmax), 200, (int)
* (getWidth() / 2), getHeight());
*/
// krzyz
g.setColor(Color.BLACK);
g.drawLine(getWidth() / 2, yCross, xCross, yCross);
g.drawLine(xCross, getHeight() / 2, xCross, yCross);
/*
* //OX for(int i=0;i<=400;i=i+50) if(i!=200) g.drawString(new
* Integer(i-200).toString(),i, 210);
*
* //OY for(int i=0;i<=400;i=i+50) if(i!=200) g.drawString(new
* Integer((i-200)*-1).toString(),200, i);
*/
}
}
/*
* public class RysSilnik {
*
* public static int makePointX(int W, double xmin, double thisX, double
* xmax,String wzor) { int obl = (int) (W / (Math.abs(xmin) + Math.abs(xmax)));
* double thisX = Wykres.getX(wzor,thisY); int pktSt = (int) ((W / 2) + (thisX *
* pktSt)); return pktSt; }
*
* public static int makePointY(int H, double ymin, double thisY, double ymax,
* String wzor) { int obl = (int) (H / (Math.abs(ymin) + Math.abs(ymax)));
* double thisY = Wykres.getY(wzor,thisX); int pktSt = (int) ((H / 2) + (thisY *
* pktSt)); return pktSt; } } }
*/