Figury – Graphics2D

Kody źródłowe z odcinka

package pl.am.swing.ramka6;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class FiguryComponent extends JComponent {
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D graphics2D = (Graphics2D) g;

        graphics2D.drawString("Hello world", 0, 20);

        Point point1 = new Point(100, 100);
        Point point2 = new Point(300, 300);

        Dimension dimension = new Dimension(200, 200);

        Line2D line2D = new Line2D.Float(point1, point2);
        graphics2D.draw(line2D);

        Rectangle rectangle = new Rectangle(point1, dimension);
        graphics2D.draw(rectangle);

        Ellipse2D ellipse2D = new Ellipse2D.Float();
        ellipse2D.setFrame(rectangle);
        graphics2D.draw(ellipse2D);

    }
}
package pl.am.swing.ramka6;

import javax.swing.*;
import java.awt.*;

public class MojeOkno extends JFrame {
    public MojeOkno() {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension rozmiarEkranu = toolkit.getScreenSize();
        setTitle("Moje pierwsze okienko");
        setBounds(50, 50, rozmiarEkranu.width/2, rozmiarEkranu.height/2);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        add(new FiguryComponent());
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            MojeOkno mojeOkno = new MojeOkno();
            mojeOkno.setVisible(true);
        });
    }
}

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *