>我让学校项目从多项式函数制作图(例如 2x^2+2x+4(。使用带有paintComponent的JFrame,我从多项式函数的结果中为x制作线条。这将在xmin,xmax,ymin和ymax的区域中,之后鼠标侦听器将显示我的鼠标光标在图形上的位置,其点位于x行和y行上。
编辑: 我需要做什么才能从类 Wykres 访问变量。制作Wykres wykres = new Wykres()
;使程序卡住。我是否需要使用计算多项式函数的方法将动作中的变量添加到新类 Silnik 中,以及我该如何实现?
帮助将不胜感激。
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; } } }
*/
做了这样的事情:当鼠标保存值时,向绘图组件添加mmouse动作,然后在油漆中重复使用它来绘制2条线
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
class Rysowanie extends JPanel {
protected int yCross;
protected int xCross;
public Rysowanie() {
setPreferredSize(new Dimension(400, 400));
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) {
// nothing to do here
}
});
}
public void paintComponent(Graphics g) {
/*
* 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() / 2), 200, (int) (getWidth() / 2), getHeight());
// draw the cross
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; } } }
*/