使我的输出(Canvas)仅显示[0,1](Java)之间的值

问题描述 投票:0回答:1

具有以下GridBagLayout接口,并且内部带有Canvas类型,以便表示在[0,1]间隔之间归一化的随机生成的数字:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9LTDVFcy5wbmcifQ==” alt =“与画布的界面”>

我发现了下一个问题:我的画布显示的值大于1,所以这意味着我看不到标准化点(x,y);因为它们对于实际的画布设置来说太小了。这就是我想要的:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9Gand3by5wbmcifQ==” alt =“所需结果”>

如您所见,这些点是使用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);
    }
}
java swing canvas awt intervals
1个回答
1
投票
将无法解决您的问题,但是Swing应用程序应扩展JPanel而不是Canvas并通过覆盖paintComponent(…)而不是paint()进行自定义绘制。

我看不到我的归一化点(x,y);因为它们对于实际的画布设置来说太小了。

如果您的数字介于[0,1]之间,是否不必将这些数字乘以面板的宽度/高度(而不是使用模数)来获得x / y整数点来进行绘画?

仅失败于最大值(1,1)

如果该点从面板的边界开始并延伸3个像素,则该点将在面板的边界之外绘制,并且您将无法看到它。

也许您需要将标准化值乘以(面板大小-3)?

© www.soinside.com 2019 - 2024. All rights reserved.