PaintingAndStroking.java Source code

Java tutorial

Introduction

Here is the source code for PaintingAndStroking.java

Source

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

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

public class PaintingAndStroking extends JPanel {
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        double x = 15, y = 50, w = 70, h = 70;
        Ellipse2D e = new Ellipse2D.Double(x, y, w, h);
        GradientPaint gp = new GradientPaint(75, 75, Color.white, 95, 95, Color.gray, true);
        // Fill with a gradient.
        g2.setPaint(gp);
        g2.fill(e);
        // Stroke with a solid color.
        e.setFrame(x + 100, y, w, h);
        g2.setPaint(Color.black);
        g2.setStroke(new BasicStroke(8));
        g2.draw(e);
        // Stroke with a gradient.
        e.setFrame(x + 200, y, w, h);
        g2.setPaint(gp);
        g2.draw(e);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.getContentPane().add(new PaintingAndStroking());
        f.setSize(350, 250);
        f.show();
    }
}