在java中绘制极坐标图 [英] draw polar graph in java
本文介绍了在java中绘制极坐标图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道我如何开始在java中绘制极坐标图,并在该图上绘制一些点?我的意思是圆圈和线条,我希望用swing之类的东西来做,而不是像Jfreechart那样使用任何类库
谢谢
解决方案
您可能会喜欢看利萨如曲线;下面显示了 a = 5,b = 4(5:4)
的示例。
附录:一次您会看到如何在 xy 坐标中绘制点,那么您应该查看 在极坐标和笛卡尔坐标之间进行转换 。 b
$ b
$ b
public class LissajousPanel extends JPanel {
private static final int SIZE = 400;
private GeneralPath path = new GeneralPath();
@Override
public Dimension getPreferredSize(){
return new Dimension(SIZE,SIZE);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
double dt = Math.PI / 180;
int w = getWidth()/ 2;
int h = getHeight()/ 2;
path.reset();
path.moveTo(w,h); (double t = 0; t <2 * Math.PI; t + = dt){
double x = w * Math.sin(5 * t)+ w;
;
double y = h * Math.sin(4 * t)+ h;
path.lineTo(x,y);
}
g2d.setColor(Color.blue);
g2d.draw(path);
public static void main(String [] args){
EventQueue.invokeLater(new Runnable(){
$ b $ @Override
)public void run(){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new LissajousPanel());
f.pack();
f.setVisible(true);
}
});
}
}
Does anyone know how I can get started to draw a polar graph in java and plot some points on this graph? I mean the circles and lines, I wish to do this with something like swing, and not use any library like Jfreechart Thanks
解决方案
You might like to look at Lissajous curves; an example of a = 5, b = 4 (5:4)
is shown below.
Addendum: Once you see how to plot points in xy coordinates, then you should look at converting between polar and Cartesian coordinates.
public class LissajousPanel extends JPanel {
private static final int SIZE = 400;
private GeneralPath path = new GeneralPath();
@Override
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
double dt = Math.PI / 180;
int w = getWidth() / 2;
int h = getHeight() / 2;
path.reset();
path.moveTo(w, h);
for (double t = 0; t < 2 * Math.PI; t += dt) {
double x = w * Math.sin(5 * t) + w;
double y = h * Math.sin(4 * t) + h;
path.lineTo(x, y);
}
g2d.setColor(Color.blue);
g2d.draw(path);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new LissajousPanel());
f.pack();
f.setVisible(true);
}
});
}
}
这篇关于在java中绘制极坐标图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文