Search code examples
javajframedrawingjava-2d

Filling the frame with squares in different colors


I had this strange problem with filling the frame by 30px squares in different colors. Here is my try but dont work (and also cant figure out how to set size of squares)

import java.awt.Component;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;

public class Pixl extends Component 
{
public void paint(Graphics g)
{
    Graphics2D g2d = (Graphics2D)g;
    g.setColor(Color.CYAN);

    int x = 25;
    int y = 32;
    g.drawLine(x,y,x,y);
}

public static void main(String[] args)
{

    int frameWidth = 300;
    int frameHeight = 300;

    javax.swing.JFrame frame = new javax.swing.JFrame();
    frame.setSize(frameWidth,frameHeight);
    frame.setVisible(true);
    frame.getContentPane().add(new DrawingComponent());
}
}

Thanks


Solution

  • In your code you are drawing a line that starts and ends on the same point, this yields a single pixel set to cyan color.

    To draw a rect use fillRect, it's quite self-explanatory if you take time to read the documentation:

    int size = 30;
    g.fillRect(x, y, size, size);