MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.MemoryImageSource;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JPanel {
    public void paint(Graphics gg) {
        int h = 150;
        int w = 150;

        int pixels[] = new int[w * h];
        int i = 0;
        int r = 0, g = 0, b = 0;

        for (int y = 0; y < h; y++) {
            for (int x = 0; x < h; x++) {
                r = (x ^ y) & 0xff;
                g = (x * 2 ^ y * 2) & 0xff;
                b = (x * 4 ^ y * 4) & 0xff;
                pixels[i++] = (255 << 24) | (r << 16) | (g << 8) | b;
            }
        }

        Image art = createImage(new MemoryImageSource(w, h, pixels, 0, w));

        gg.drawImage(art, 0, 0, this);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new MainClass());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
    }
}