我正在尝试扩展java fx Canvas:
package com.kavricious.math;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import java.util.Random;
public class LPlotCanvas extends Canvas{
boolean grid = false;
boolean axes = true;
boolean ticks = true;
boolean startDrawn = false;
double xMin;
double xMax;
double yMin;
double yMax;
int density;
short canvasWidth = 800;
short canvasHeight = 800;
byte colorIndex = 0;
private GraphicsContext gc = null;
public LPlotCanvas(double xmin,double xmax, double ymin, double ymax){
xMin = xmin;
xMax = xmax;
yMin = ymin;
yMax = ymax;
density = 100;
setWidth(canvasWidth);
setHeight(canvasHeight);
gc = getGraphicsContext2D();
drawStart();
}
private void drawStart(){
System.out.println("drawing start");
if(gc == null)System.out.println("it null");
gc.setLineWidth(2);
gc.setStroke(Color.BLACK);
if(axes){
if(xMin<0.0&&xMax>0.0){
drawLine(gc,0,yMin-1,0,yMax+1);
}
if(yMin<0.0&&yMax>0.0){
drawLine(gc,xMin-1,0,xMax+1,0);
}
}
if(grid){
for(int i =(int)((xMin>0)?Math.floor(xMin):Math.ceil(xMin));i<xMax+1;i++){
gc.setLineWidth(0.25);
drawLine(gc,i,yMin,i,yMax);
}
for(int i =(int)((yMin>0)?Math.floor(yMin):Math.ceil(yMin));i<yMax+1;i++){
gc.setLineWidth(0.25);
drawLine(gc,xMin,i,xMax,i);
}
}
startDrawn = true;
}
public void showGrid(boolean b){
grid = b;
}
public void showAxes(boolean b){
axes = b;
}
public void showTicks(boolean b){
ticks = b;
}
public void setCanvasWidth(short width){
this.canvasWidth = width;
}
public void setCanvasHeight(short height){
this.canvasHeight = height;
}
private void plotPoint(double x,double y,Color color){
if(!startDrawn)drawStart();
gc.setFill(color);
gc.fillOval(convertX(x),convertY(y),5,5);
}
public void drawPolynomial(LPolynomial function)throws Exception{
if(!startDrawn)drawStart();
gc.setStroke(getNextColor(colorIndex));
double start = xMin;
double step = (xMax-xMin)/density;
for(int i=0;i<density;i++){
drawLine(gc,start+step*i,function.evaluateAt(start+step*i),start+step*(i+1),function.evaluateAt(start+step*(i+1)));
}
colorIndex++;
}
private void drawLine(GraphicsContext gc,double x1,double y1,double x2, double y2){
int nx1 = convertX(x1);
int ny1 = convertY(y1);
int nx2 = convertX(x2);
int ny2 = convertY(y2);
gc.strokeLine(nx1,ny1,nx2,ny2);
System.out.println("line drawn");
}
private int convertX(double in){
double widthOfPixel = (xMax-xMin)/canvasWidth;//how much space on the real number line is put into one pixel
return (int) Math.floor((in-xMin)/widthOfPixel);//how much greater than left side DIVIDED by pixel size -> how many pixels away from left side
}
private int convertY(double in){
double heightOfPixel = (yMax-yMin)/canvasHeight;
return canvasHeight - (int) Math.floor((in-yMin)/heightOfPixel);
}
public Color getNextColor(byte i){
switch(i){
case 0: return Color.rgb(255,0,0);//red
case 1: return Color.rgb(0,200,0);//lime
case 2: return Color.rgb(0,0,235);//blue
case 3: return Color.rgb(255,255,0);//yellow
case 4: return Color.rgb(255,50,255);//magenta/fuchsia
case 5: return Color.rgb(255,165,0);//orange
case 6: return Color.rgb(0,235,235);
case 7: return Color.rgb(107,30,119);
case 8: return Color.rgb(0,100,0);
default: Random rand = new Random();
int r = rand.nextInt(256);
int g = rand.nextInt(256);
int b = rand.nextInt(256);
return Color.rgb(r,g,b);
}
}
}
然后在另一个应用程序中调用它:
package com.kavricious.math;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcType;
import java.util.ArrayList;
public class LPlotter2 extends Application{
private ArrayList<LPolynomial> functions;
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage ps){
this.functions = new ArrayList<LPolynomial>();
LPolynomial[] funcs = new LPolynomial[]{new LPolynomial(2.3,3.4,2.2,1.1),new LPolynomial(0,1,-2.3),new LPolynomial(1,2),new LPolynomial(2,2,2.3,1,0.4)};
for(LPolynomial f: funcs){
this.functions.add(f);
}
ps.setTitle("LPlotter");
StackPane layout = new StackPane();
LPlotCanvas canv = new LPlotCanvas(-10,10,-10,10);
for(LPolynomial function : functions){
try{
canv.drawPolynomial(function);
}catch(Exception e){System.out.println("an exception wad caught");e.printStackTrace();}
}
layout.getChildren().add(canv);
ps.setScene(new Scene(layout,800,800));
ps.show();
}
}
(在上面的代码中,我尝试在初始化LPlotCanvas之后使用for循环在每个位置绘制多项式,并且会出现相同的结果。)