You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import javax.swing.*;
import java.awt.*;
public class GraphicsPrimitives extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Рисуем круг
g.setColor(Color.RED);
// (50, 50) координаты левого верхнего угла ограничивающего прямоугольника, 100 диаметр
g.drawOval(50, 50, 100, 100);
// Рисуем квадрат
g.setColor(Color.BLUE);
// (200, 50) координаты левого верхнего угла, 100 размер стороны квадрата
g.drawRect(200, 50, 100, 100);
// Линия
g.setColor(Color.GREEN);
g.drawLine(50, 200, 150, 250);
// Заполненный прямоугольник
g.setColor(Color.MAGENTA);
// Метод fillRect() заполняет прямоугольник цветом
g.fillRect(200, 200, 100, 50);
// Эллипс
g.setColor(Color.ORANGE);
g.drawOval(350, 200, 120, 60);
// "звездообразный" узор
g.setColor(Color.BLACK);
int centerX = 450;
int centerY = 300;
int radius = 50;
// Рисуем линии расходящиеся из центра с шагом 30 градусов
for (int angle = 0; angle < 360; angle += 30) {
int xEnd = centerX + (int) (radius * Math.cos(Math.toRadians(angle)));
int yEnd = centerY + (int) (radius * Math.sin(Math.toRadians(angle)));
g.drawLine(centerX, centerY, xEnd, yEnd);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Пример графических примитивов");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.add(new GraphicsPrimitives());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}