用java语言绘制三角函数图像
要求;函数是sinx或者cosx输入区间(比方说100-1200)图形界面上显示x=100-120的sin图像(成比例缩放在界面上);能直接给我程序最好;不能的话能解释一...
要求;函数是sinx或者cosx 输入区间(比方说100-1200) 图形界面上显示x=100-120的sin图像(成比例缩放在界面上);能直接给我程序最好;不能的话能解释一下大致思路和关键代码也成!求给力~~~
展开
1个回答
展开全部
package com.graphics;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
public Test(){
getContentPane().add(new GJpanel());
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test();
}
}
class GJpanel extends JPanel {
private int w;
private int h;
public GJpanel(){
}
public void paintComponent(final Graphics g){
w = getWidth();
h = getHeight();
g.setColor(Color.green);
g.drawLine(0, 0, 0,getHeight());
g.setColor(Color.red);
g.drawLine(0,h/2,w,h/2); //x
g.drawLine(w, h/2, w-10, h/2-10);
g.drawLine(w, h/2, w-10, h/2+10);
g.drawLine(w/2, 0,w/2, h); //y
g.drawLine(w/2, 0, w/2-10, 10);
g.drawLine(w/2, 0, w/2+10, 10);
g.drawString("Y", w/2-20, 20);
g.drawString("X", w-20, h/2+20);
for(int x =0;x<w; x++){
int y =(int) (Math.cos (x*Math. PI/180)*h/3);
g.drawString("·", x, h/2-y);
}
}
}
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
public Test(){
getContentPane().add(new GJpanel());
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test();
}
}
class GJpanel extends JPanel {
private int w;
private int h;
public GJpanel(){
}
public void paintComponent(final Graphics g){
w = getWidth();
h = getHeight();
g.setColor(Color.green);
g.drawLine(0, 0, 0,getHeight());
g.setColor(Color.red);
g.drawLine(0,h/2,w,h/2); //x
g.drawLine(w, h/2, w-10, h/2-10);
g.drawLine(w, h/2, w-10, h/2+10);
g.drawLine(w/2, 0,w/2, h); //y
g.drawLine(w/2, 0, w/2-10, 10);
g.drawLine(w/2, 0, w/2+10, 10);
g.drawString("Y", w/2-20, 20);
g.drawString("X", w-20, h/2+20);
for(int x =0;x<w; x++){
int y =(int) (Math.cos (x*Math. PI/180)*h/3);
g.drawString("·", x, h/2-y);
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询