简体   繁体   中英

Applet doesn't show anything

For some reason, this code will not render anything. Can someone explain to me why it doesn't do anything?

import java.awt.*;   
import java.applet.*; 

public class TEST extends Applet {
    public void paint(Graphics g) {
        int xSize = 255;
        int ySize = 255;
        byte R, G, B;

        for(int x = 0; x < xSize; x++) {
            for(int y = 0; y < ySize; y++) {
                R = (byte) (x % y);
                G = (byte) (y % x);
                B = (byte) (y);
                Color pixel = new Color(R, G, B);
                g.setColor(pixel);
                g.fillRect(x, y, 1, 1);
            }
        }
    }
}

Its not possible to get the modulus of a number relative to 0 eg here

R = (byte)(x%y);

in the first iteration. You're probably seeing an ArithmeticException occur at this point. You could simply start from 1 :

red = (byte) (x % (y + 1));
green = (byte) (y % (x + 1));

I figured it out, it is trying to put RG and B values above 255. I just added %255 at the end of each one to fix it. Thanks for your help guys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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