在 JavaFX 中,我有很多数据要绘制,目前正在使用 ViewPort 类来保存各种参数,以便在屏幕上向用户显示部分数据。我的项目中的多个类需要访问此数据。我目前传递了一个 ViewPort 对象的本地副本,但我也在考虑是静态类还是单例会更好。我相信任何方法都行得通,但我正在寻找一种符合最佳实践的方法——或者如果存在的话,可能是更好的方法。
这是我的 ViewPort 类的副本,我实例化并传递。注意我
package graphics;
public class ViewPort {
private static int _step;
private static int _xStart;
private static int _yStart;
private static int _noOfPoints;
private static int _xSize;
private static int _ySize;
public ViewPort( int step, int xStart, int noOfPts,
int yStart, int xSize, int ySize) {
_step = step;
_xStart = xStart;
_noOfPoints = noOfPts;
_yStart = yStart;
_xSize = xSize;
_ySize = ySize;
}
public int getStep(){
return _step;
}
public void setStep(int step){
_step = step;
}
public int getXStart(){
return _xStart;
}
public void setXStart(int xStart){
_xStart = xStart;
}
public int getYStart(){
return _yStart;
}
public void setYStart(int yStart){
_yStart = yStart;
}
public int getNoOfPts(){
return _noOfPoints;
}
public void setNoOfPts(int noOfPts){
_noOfPoints = noOfPts;
}
public int getXSize(){
return _xSize;
}
public void setXSize(int xSize){
_xSize = xSize;
}
public int getYSize(){
return _ySize;
}
public void setYSize(int ySize){
_ySize = ySize;
}
public double displayValue(double x){
return (double) _xStart + (x * _step);
}
public int intValue(double x){
return (int) ((int) _xStart + (x * _step));
}
public double dblValue(double x){
return ( _xStart + (x * _step));
}
public int getIntX(String pos){
return (Integer.parseInt(pos) - _xStart)/_step;
}
public double getDblX(String pos){
return (Integer.parseInt(pos) - _xStart)/_step;
}
public double getX(double pos){
return (pos - _xStart)/_step;
public void setXSize(int xSize){
_xSize = xSize;
}
public int getYSize(){
return _ySize;
}
public void setYSize(int ySize){
_ySize = ySize;
}
public double displayValue(double x){
return (double) _xStart + (x * _step);
}
public int intValue(double x){
return (int) ((int) _xStart + (x * _step));
}
public double dblValue(double x){
return ( _xStart + (x * _step));
}
public int getIntX(String pos){
return (Integer.parseInt(pos) - _xStart)/_step;
}
public double getDblX(String pos){
return (Integer.parseInt(pos) - _xStart)/_step;
}
public double getX(double pos){
return (pos - _xStart)/_step;
}
public boolean inRange(double pos) {
return ((getX(pos) >= _xStart*_step) && (getX(pos) < _xSize*_step));
}
public double getWidth(double w){
return w/_step;
}
}