Mam napisać program, który przekształca współrzędne kartezjańskie na współrzędne ekranowe, po to by móc narysować wykres funkcji na ekranie.
Mam coś takiego, ale wyświetla mi pustą ramkę, nie wiem co mogę poprawić . Czy ktoś mógłby zerknąć czy rzutowanie współrzędnych jest prawidłowe?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Konwersja extends JFrame {
private Konwersja2 contentPane;
private class Konwersja2 extends JPanel {
private double xmin;
private double xmax;
private double ymin;
private double ymax;
private int W;
private int H;
public Konwersja2() {
xmin = 0;
xmax = 400;
ymin = 0;
ymax = 400;
W = 500;
H = 500;
}
public Konwersja2(double xmin, double xmax, double ymin, double ymax, int W, int H) {
this.xmin = xmin;
this.xmax = xmax;
this.ymin = ymin;
this.ymax = ymax;
this.W = W;
this.H = H;
}
public int obliczXE(double x) {
int xe = W * (int) ((x - xmin) / (xmax - xmin));
return xe;
}
public int obliczYE(double y) {
int ye = H - (H * (int) ((y - ymin) / (ymax - ymin)));
return ye;
}
public int getW() {
return W;
}
public void setW(int w) {
W = w;
}
public int getH() {
return H;
}
public void setH(int h) {
H = h;
}
public void draw(Graphics g2, Konwersja2 okno) {
okno.setW(getWidth());
okno.setH(getHeight());
g2.setColor(Color.red);
for (double x = 0; x <= (2 * Math.PI); x += 0.001) {
double y = Math.sin(x);
int xe = okno.obliczXE(x);
int ye = okno.obliczYE(y);
g2.drawLine(xe, ye, xe, ye);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//Graphics2D g2 = (Graphics2D) g;
draw(g, contentPane);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { // tworzenie nowego wątku
public void run() {
try {
Konwersja frame = new Konwersja();
frame.setVisible(true); // ustawianie widoczności
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Konwersja() { // konstruktor
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, 0, 500, 500);
contentPane = new Konwersja2();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}