MyCanvas.java Source code

Java tutorial

Introduction

Here is the source code for MyCanvas.java

Source

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

class MyCanvas extends JComponent {
    Color[] colors = new Color[24];

    public MyCanvas() {
        int rgb = 0xff;
        for (int i = 0; i < 24; i++) {
            colors[i] = new Color(rgb);
            rgb <<= 1;
            if ((rgb & 0x1000000) != 0) {
                rgb |= 1;
                rgb &= 0xffffff;
            }
        }
    }

    public void paint(Graphics g) {
        int i = 10;
        for (Color c : colors) {
            g.setColor(c);
            i += 10;
            g.drawLine(i, 10, 200, 200);
        }
    }
}

public class DrawLine {
    public static void main(String[] a) {
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setBounds(30, 30, 300, 300);
        window.getContentPane().add(new MyCanvas());
        window.setVisible(true);
    }
}