简体   繁体   English

为什么该程序无法绘制正方形?

[英]Why is this program not drawing the square?

I am currently learning out of Computing Concepts with java 2 essentials by Cay Hortmans, And i copied this program straight from the book, yet it is not working. 目前,我正在学习Cay Hortmans的Java 2 Essentials中的Computing Concepts,并且我直接从书中复制了该程序,但没有用。 Im not looking for an entirely separate way of doing this, but just why this code, straight from the book, is not drawing the square correctly. 我不是在寻找一种完全独立的方式来执行此操作,而是为什么这段代码(直接来自本书)无法正确绘制正方形。

import java.applet.*;
import java.awt.*;
import javax.swing.JOptionPane;

public class ColorSelect extends Applet{

    private static final long serialVersionUID = -7954365679431207534L;

    public void init(){
        String input; //ask the user for red, green, blue values

        input = JOptionPane.showInputDialog("Red:");
        float red = Float.parseFloat(input);

        input = JOptionPane.showInputDialog("Green:");
        float green = Float.parseFloat(input);

        input = JOptionPane.showInputDialog("Blue:");
        float blue = Float.parseFloat(input);
        fillColor = new Color(red, green, blue);
    }

    public void paint(Graphics g){
        final int SQUARE_LENGTH = 100;

        Graphics2D g2 = (Graphics2D)g;

        //select color into graphics content

        g2.setColor(fillColor);

        //construct and fill a square whose center is the center of the window

        Rectangle square = new Rectangle(
                (getWidth() - SQUARE_LENGTH) / 2,
                (getHeight() -  SQUARE_LENGTH) / 2,
                SQUARE_LENGTH,
                SQUARE_LENGTH);

        g2.fill(square);
    }
    private Color fillColor;
}

Because your r , g , b values are all float s, you are invoking the Color constructor that has 3 float parameters: http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#Color(float , float, float) 由于您的rgb值均为float ,因此您正在调用具有3个float参数的Color构造函数: http : //docs.oracle.com/javase/7/docs/api/java/awt/Color.html #Color(float ,float,float)

Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0). 创建具有指定范围(0.0-1.0)的红色,绿色和蓝色值的不透明sRGB颜色。 Alpha is defaulted to 1.0. Alpha默认为1.0。 The actual color used in rendering depends on finding the best match given the color space available for a particular output device. 渲染中使用的实际颜色取决于给定特定输出设备可用的颜色空间,找到最佳匹配。

Either change your r, g, b types to int or cast to int r, g, b类型更改为int或将其intint

抱歉,伙计们,我意识到在估算时我使用的是int值而不是float,这很糟糕。

Rectangle specifies bounds of two opposite corners just as fill() wants. 矩形指定两个相对角的边界,就像fill()想要的那样。 You need 你需要

Rectangle square = new Rectangle(
                                  (getWidth()/2 - SQUARE_LENGTH/2),
                                  (getHeight()/2 -  SQUARE_LENGTH/2),
                                  (getWidth()/2 + SQUARE_LENGTH/2),
                                  (getHeight()/2 +  SQUARE_LENGTH/2)
                                );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM