Kolory

Kody źródłowe z odcinka

package pl.am.swing.ramka7;

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

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);

        graphics2D.setPaint(Color.PINK);
        Rectangle rectangle = new Rectangle(point1, dimension);
        graphics2D.fill(rectangle);
        graphics2D.draw(rectangle);

        graphics2D.setPaint(new Color(189, 63, 21));

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

        graphics2D.setPaint(Color.BLACK);
        graphics2D.draw(ellipse2D);


        Line2D line2D = new Line2D.Float(point1, point2);
        graphics2D.setPaint(Color.YELLOW);
        //graphics2D.draw(line2D);

        Shape shape;
    }
}
package pl.am.swing.ramka7;

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);

        getContentPane().setBackground(Color.GRAY);
        getContentPane().setForeground(Color.BLUE);

        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 *