让我的输出(画布)仅显示介于[0,1]之间的值(Java)

huangapple go评论66阅读模式
英文:

Make my output (Canvas) show ONLY values between [0,1] (Java)

问题

以下是翻译好的部分:

/**
 * Auxiliary class used in interfazSimulacion.java for the graphical representation
 * of our simulations in the output. Inherits from the Canvas class and overrides
 * the paint method of that class.
 * @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;

/**
 * Main constructor of the program
 */

public Grafico(){
    setPreferredSize(new Dimension(600,600));
    setBackground(Color.decode("#202421"));
}

/**
 * Method that sets the number of points and the coordinates of the points in X and Y.
 * @param cantidad  number of points to generate.
 * @param X coordinates in X.
 * @param Y coordinates in 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;
}

/**
 * Method used to draw the desired simulations.
 * @param graphic graphics object used for these simulations.
 */

@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);
    }
}

请注意,由于部分内容包含代码,我已经对代码部分进行了翻译,而对注释内容保留了原文。如有需要,您可以根据您的上下文和要求进行调整。

英文:

Having the following GridBagLayout interface, with a Canvas type inside it, in order to represent random generated numbers normalized between [0,1] intervals:

让我的输出(画布)仅显示介于[0,1]之间的值(Java)

I found the next problem: my canvas is showing values higher than 1, so it means that I can't see my normalized points(x,y); because they are too small with the actual canvas settings. This is what I'd like:

让我的输出(画布)仅显示介于[0,1]之间的值(Java)

As you see the points are obtained using the Lattice structure. How can I do it?

EDIT: Include code:

/**
* Clase auxiliar empleada en interfazSimulacion.java para la representaci&#243;n de
* nuestras simulaciones gr&#225;ficas en el output. Hereda de la clase Canvas y
* sobreescribe el m&#233;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(&quot;#202421&quot;));
}
/**
* M&#233;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&#233;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 &lt; 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);
}
}

答案1

得分: 1

不会解决您的问题,但 Swing 应用程序应该扩展 JPanel,而不是 Canvas,并通过覆盖 paintComponent(…) 来进行自定义绘制,而不是 paint()

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

如果您的数字在 [0,1] 之间,您不是必须将这些数字乘以面板的宽度/高度(而不是使用模运算),以获得 x/y 整数点进行绘制吗?

仅对最大值(1,1)出现问题

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

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

英文:

Won't help with your problem but, Swing applications should extend JPanel, not Canvas and do custom painting by overriding paintComponent(…), not paint().

> I can't see my normalized points(x,y); because they are too small with the actual canvas settings.

If you have numbers between [0, 1] don't you have to multiply (instead of using modulo) those numbers by the width/height of the panel to get an x/y integer point to do the painting?

> It's only failing for the max value (1,1)

Well if the point starts at the bounds of the panel and extends for 3 pixels, then the point is painted outside the bounds of the panel and you won't be able to see it.

Maybe you need to multiply the normalized value by the (panel size - 3)?

huangapple
  • 本文由 发表于 2020年3月15日 10:51:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/60689309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定