Search code examples
javaprintingdrawpixel

Java-How to draw pixels on a screen


What is the best way to simply print pixels onto a screen in Java, either through a simple loop or an array?


Solution

  • You can use a BufferedImage and display it on a JLabel. Something like:

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.util.List;
    import javax.swing.*;
    
    public class SSCCE extends JPanel
    {
        public SSCCE()
        {
            int size = 300;
            BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
            ImageIcon icon = new ImageIcon( bi );
            add( new JLabel(icon) );
    
            for (int y = 0; y < size; y += 5)
            {
                for (int x = 0; x < size; x++)
                {
                    Color color = (y % 2 == 0) ? Color.RED : Color.GREEN;
                    int colorValue = color.getRGB();
                    bi.setRGB(x, y, colorValue);
                    bi.setRGB(x, y + 1, colorValue);
                    bi.setRGB(x, y + 2, colorValue);
                    bi.setRGB(x, y + 3, colorValue);
                    bi.setRGB(x, y + 4, colorValue);
                }
            }
        }
    
        private static void createAndShowGUI()
        {
            JFrame frame = new JFrame("SSCCE");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add( new SSCCE() );
            frame.setLocationByPlatform( true );
            frame.pack();
            frame.setVisible( true );
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowGUI();
                }
            });
        }
    }
    

    Or you can create a custom component and implement the paintComponent(...) method using methods from the Graphics class:

    Graphics.fillRect(...);
    Graphics.fillOval(...);
    etc..
    

    Read the section from the Swing tutorial on Custom Painting for more information and examples to get your started. Don't forget to read the Graphics API for other Graphics methods.