java编写一个程序,在JPanel组件上绘制函数f(x)=x*x的曲线图. 10
2个回答
展开全部
是要这种效果的么?
package example;
import java.awt.Font;
//import stdlib.StdDraw;
public class TestDraw {
/**
* 按照比例增加,每增加一个log10,增加指定的add
* @param d
* @param val
* @param add
* @return
*/
public static double scale(double d,double val,double add){
if(d<10){
return val ;
}
double temp = Math.log10(d);
val = add*temp+val;
return val;
}
/**
* 输出坐标轴
* @param maxX
* @param maxY
*/
public static void printXY(double maxX,double maxY){
double x0= maxX/2; //X轴中心
maxY = maxY*0.9; //Y轴显示比例为110%
double textY = 0.004 * maxY; //文字描述Y轴位置 偏移千分之4
textY = textY * Math.log10(maxY); //按比例修正
double tesxtX = 0.004 * maxX; //文字描述X轴位置 偏移千分之4
tesxtX = tesxtX * Math.log10(maxX);
double tesxtXs =(maxX*0.001);;//增长比例
tesxtXs = tesxtXs * Math.log10(maxX);
double lineLenX = 0.0025 * maxY ;//X轴分割线长度
lineLenX = lineLenX * Math.log10(maxY);
double lineLenY = 0.0025 * maxX ;//Y轴分割线长度
lineLenY = lineLenY * Math.log10(maxX);
ToolDraw.setXscale(0, maxX);
ToolDraw.setYscale(0, maxY);
ToolDraw.setPenColor(ToolDraw.BLACK);
ToolDraw.setPenRadius(0.0025);
//输出X轴线
ToolDraw.line(0, 0, maxX, -0);
//输出Y轴线
ToolDraw.line(x0, 0, x0, maxY);
//输出分割线段
ToolDraw.setPenColor(ToolDraw.RED);
ToolDraw.setPenRadius(0.0025);
ToolDraw.setFont(new Font("SansSerif", Font.PLAIN, 12));
ToolDraw.textLeft(x0, -textY, ""+0) ; //原点描述
//打印X轴分割线
for(int i=1;i<x0;i++){
ToolDraw.line((i+x0), 0, (i+x0), lineLenX);
ToolDraw.line(x0-i, 0, x0-i, lineLenX);
ToolDraw.textLeft((i+x0), -textY, ""+(i)) ;
ToolDraw.textLeft(x0-i, -textY, ""+(-i)) ;
}
//打印Y轴分割线
for(int i=1;i<maxY/0.9;i++){
ToolDraw.line(x0, i*0.9, x0+lineLenY, i*0.9);
ToolDraw.textLeft(x0-scale(i,tesxtX,tesxtXs), i*0.9, ""+i);
}
ToolDraw.setPenColor(ToolDraw.RED);
ToolDraw.setPenRadius(0.005);
ToolDraw.point(x0,0); // 输出原点
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ToolDraw.setCanvasSize(2048,2048); //设置画布尺寸(单位像素)
printXY(100,100); //输出坐标轴
double x1 =100/2;
double x2 = 100/2 ;
double y1 = 0 ;
double y2 = 0;
for(int i=1;i*i<100;i++){ //输出点
ToolDraw.setPenColor(ToolDraw.RED); //设置画笔颜色为红色
ToolDraw.setPenRadius(0.005); //设置画笔尺寸
ToolDraw.line(x1, y1, (i+100/2), (i*i)*0.9 ); //输出线
ToolDraw.line(x2, y2, 100/2-i, (i*i)*0.9 );
x1 =i+100/2;
x2 = 100/2-i ;
y1 = (i*i)*0.9 ;
y2 = (i*i)*0.9;
ToolDraw.setPenColor(ToolDraw.BLUE);
ToolDraw.setPenRadius(0.01);
//把点换颜色和尺寸标出
ToolDraw.point(x1,y1);
ToolDraw.point(x2,y2);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-07
展开全部
public class JPanel_aXa extends JFrame {
public JPanel_aXa(){
super("n x n");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1024, 600);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.RED);
int lastX=0, lastY=0;
for(int x=0; x<40; x+=1){
int y=getHeight()-x*x;
g.drawLine(lastX, lastY, x+1, y+1);
lastX=x;
lastY=y;
}
}
public static void main(String[] args) {
new JPanel_aXa();
}
}
曲线画不了多少,因为x *x 挺大的
public JPanel_aXa(){
super("n x n");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1024, 600);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.RED);
int lastX=0, lastY=0;
for(int x=0; x<40; x+=1){
int y=getHeight()-x*x;
g.drawLine(lastX, lastY, x+1, y+1);
lastX=x;
lastY=y;
}
}
public static void main(String[] args) {
new JPanel_aXa();
}
}
曲线画不了多少,因为x *x 挺大的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询