使我的输出 (Canvas) 仅显示 [0,1] (Java) 之间的值
Make my output (Canvas) show ONLY values between [0,1] (Java)
具有以下 GridBagLayout
接口,其中包含 Canvas
类型,以表示在 [0,1] 间隔之间归一化的随机生成数字:
我发现了下一个问题:我的 canvas 显示的值大于 1,所以这意味着我看不到我的标准化点 (x,y);因为它们对于实际 canvas 设置来说太小了。这就是我想要的:
如您所见,积分是使用 Lattice structure 获得的。我该怎么做?
编辑:包含代码:
/**
* Clase auxiliar empleada en interfazSimulacion.java para la representación de
* nuestras simulaciones gráficas en el output. Hereda de la clase Canvas y
* sobreescribe el método paint de dicha clase.
* @author Fco Javier Perez Sanchez
* @version 1.1
* @since February 25, 2020
*/
import java.awt.*;
import java.awt.geom.*;
import java.math.BigDecimal;
public class Grafico extends Canvas{
public static int cantidadPuntos;
public static BigDecimal[] coordenadasX;
public static BigDecimal[] coordenadasY;
/**
* Constructor principal del programa
*/
public Grafico(){
setPreferredSize(new Dimension(600,600));
setBackground(Color.decode("#202421"));
}
/**
* Método que establece la cantidad de puntos y las coordenadas de los puntos en X e Y.
* @param cantidad cantidad de puntos a generar.
* @param X coordenadas en X.
* @param Y coordenadas en Y.
*/
public static void getPuntos(int cantidad, BigDecimal[] X, BigDecimal[] Y){
cantidadPuntos = cantidad;
coordenadasX = new BigDecimal[cantidadPuntos];
coordenadasY = new BigDecimal[cantidadPuntos];
coordenadasX = X;
coordenadasY = Y;
}
/**
* Método que sirve para dibujar las simulaciones deseadas.
* @param graphic objeto de tipo graphics usado para dichas simulaciones.
*/
@Override
public void paint(Graphics graphic){
graphic.setColor(Color.GREEN);
//graphic.fillRect(10,10,10,10);
Graphics2D nuevoOutput = (Graphics2D) graphic;
for(int i = 0; i < cantidadPuntos-1; i = i+2){
Rectangle2D rect;
rect = (new Rectangle.Double(coordenadasX[i].doubleValue()%this.getWidth(), coordenadasY[i+1].doubleValue()%this.getHeight(), 3,3));
nuevoOutput.fill(rect);
}
}
对您的问题没有帮助,但是,Swing 应用程序应该扩展 JPanel
,而不是 Canvas,并通过覆盖 paintComponent(…)
而不是 paint() 来进行自定义绘画。
I can't see my normalized points(x,y); because they are too small with the actual canvas settings.
如果您有 [0, 1] 之间的数字,您不必将这些数字乘以面板的 width/height(而不是使用模数)以获得 x/y 整数点做画?
It's only failing for the max value (1,1)
好吧,如果该点从面板边界开始并延伸 3 个像素,那么该点将绘制在面板边界之外,您将无法看到它。
也许您需要将归一化值乘以(面板大小 - 3)?
具有以下 GridBagLayout
接口,其中包含 Canvas
类型,以表示在 [0,1] 间隔之间归一化的随机生成数字:
我发现了下一个问题:我的 canvas 显示的值大于 1,所以这意味着我看不到我的标准化点 (x,y);因为它们对于实际 canvas 设置来说太小了。这就是我想要的:
如您所见,积分是使用 Lattice structure 获得的。我该怎么做?
编辑:包含代码:
/**
* Clase auxiliar empleada en interfazSimulacion.java para la representación de
* nuestras simulaciones gráficas en el output. Hereda de la clase Canvas y
* sobreescribe el método paint de dicha clase.
* @author Fco Javier Perez Sanchez
* @version 1.1
* @since February 25, 2020
*/
import java.awt.*;
import java.awt.geom.*;
import java.math.BigDecimal;
public class Grafico extends Canvas{
public static int cantidadPuntos;
public static BigDecimal[] coordenadasX;
public static BigDecimal[] coordenadasY;
/**
* Constructor principal del programa
*/
public Grafico(){
setPreferredSize(new Dimension(600,600));
setBackground(Color.decode("#202421"));
}
/**
* Método que establece la cantidad de puntos y las coordenadas de los puntos en X e Y.
* @param cantidad cantidad de puntos a generar.
* @param X coordenadas en X.
* @param Y coordenadas en Y.
*/
public static void getPuntos(int cantidad, BigDecimal[] X, BigDecimal[] Y){
cantidadPuntos = cantidad;
coordenadasX = new BigDecimal[cantidadPuntos];
coordenadasY = new BigDecimal[cantidadPuntos];
coordenadasX = X;
coordenadasY = Y;
}
/**
* Método que sirve para dibujar las simulaciones deseadas.
* @param graphic objeto de tipo graphics usado para dichas simulaciones.
*/
@Override
public void paint(Graphics graphic){
graphic.setColor(Color.GREEN);
//graphic.fillRect(10,10,10,10);
Graphics2D nuevoOutput = (Graphics2D) graphic;
for(int i = 0; i < cantidadPuntos-1; i = i+2){
Rectangle2D rect;
rect = (new Rectangle.Double(coordenadasX[i].doubleValue()%this.getWidth(), coordenadasY[i+1].doubleValue()%this.getHeight(), 3,3));
nuevoOutput.fill(rect);
}
}
对您的问题没有帮助,但是,Swing 应用程序应该扩展 JPanel
,而不是 Canvas,并通过覆盖 paintComponent(…)
而不是 paint() 来进行自定义绘画。
I can't see my normalized points(x,y); because they are too small with the actual canvas settings.
如果您有 [0, 1] 之间的数字,您不必将这些数字乘以面板的 width/height(而不是使用模数)以获得 x/y 整数点做画?
It's only failing for the max value (1,1)
好吧,如果该点从面板边界开始并延伸 3 个像素,那么该点将绘制在面板边界之外,您将无法看到它。
也许您需要将归一化值乘以(面板大小 - 3)?