BasicDraw.java Source code

Java tutorial

Introduction

Here is the source code for BasicDraw.java

Source

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

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

public class BasicDraw {
    public static void main(String[] args) {
        new BasicDraw();
    }

    BasicDraw() {
        JFrame frame = new JFrame();

        frame.add(new MyComponent());

        frame.setSize(300, 300);
        frame.setVisible(true);
    }

}

class MyComponent extends JComponent {
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.red);

        g2d.fill(new Rectangle(20, 20, 200, 200));
        int red = 230;
        int green = 45;
        int blue = 67;
        g2d.setColor(new Color(red, green, blue));
        g2d.fill(new Rectangle(40, 40, 200, 200));
    }

}