Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Rozrah_KG.docx
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
210 Кб
Скачать
☆

Інструкція користувачу

Спочатку для користувача відкривається вікно, де йому потрібно вказати якого типу буде багатокутник, тобто скільки кутів буде мати.

По замовчуванню вказано чотирикутник, але користувач може змінювати фігуру.

Рис 2.1.1 Вибір геометричної фігури

Після того як користувач вибрав кількість кутів і натиснув кнопку “OK” програма будує вибрану геометричну фігуру.

Рис 2.1.2 Побудова прямокутника

Рис 2.1.2 Побудова кола

Текст програми:

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Polygon;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class Rozrah {

private JFrame mainMap;

private Polygon poly;

static int [] x ;

static int [] y ;

static int k ;

public Rozrah() {

}

private void initComponents(int[]x, int[]y) {

mainMap = new JFrame();

mainMap.setResizable(false);

mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

int xPoly[] = x;

int yPoly[] = y;

poly = new Polygon(xPoly, yPoly, xPoly.length);

JPanel p = new JPanel() {

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.setColor(Color.BLUE);

g.drawPolygon(poly);

}

@Override

public Dimension getPreferredSize() {

return new Dimension(800, 600);

}

};

MouseAdapter ma = new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent me) {

super.mouseClicked(me);

if (poly.contains(me.getPoint())) {

System.out.println("Clicked polygon");

}

}

};

p.addMouseListener(ma);

mainMap.add(p);

mainMap.pack();

mainMap.setVisible(true);

}

public static void main(String[] args) {

JFrame mainFrame = new JFrame("Line Clipping");

mainFrame.setSize(800, 600);

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String response = JOptionPane.showInputDialog(mainFrame, "Виберіть фігуру:1-прямокутник,2-коло",

"1");

if (response == null) System.exit(0);

try {

k = Integer.parseInt(response);

} catch (NumberFormatException ne) {

JOptionPane.showMessageDialog(mainFrame, "All values must be integers");

} finally {

}

switch (k){

case 1: x = new int[] {150, 150,400, 400}; y = new int[]{150, 300, 300, 150};break;

case 2: JFrame window = new JFrame("Brez");

window.setSize(400, 400);

window.setContentPane(new Brez());

window.setVisible(true);

break;

}

Rozrah j = new Rozrah();

j.initComponents(x,y);

}

}

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JPanel;

public class Brez extends JPanel {

public void paintComponent(Graphics g) {

super.paintComponents(g);

g.setColor(Color.BLACK);

int x = -150;

int y = 170;

if(x<0){

x = x*(-1);

}

if(y<0){

y = y*(-1);

}

int radius = 110;

if (radius>0){

Dr_Circle(x, y, radius, g);

}else{

System.out.println("радіус від'ємний");

}

}

private void Dr_Circle(int center_x, int center_y, int radius, Graphics g){

int x = 0, y = radius, sigma = 0, delta = 2 - 2 * radius;

while (y >= 0){

g.drawLine(center_x + x, center_y - y, center_x + x, center_y - y);

g.drawLine(center_x - x, center_y - y, center_x - x, center_y - y);

g.drawLine(center_x - x, center_y + y, center_x - x, center_y + y);

g.drawLine(center_x + x, center_y + y, center_x + x, center_y + y);

sigma = 2 * (delta + y) - 1;

if (delta < 0 && sigma <= 0) {

x++;

delta += x + 1;

} else if (delta > 0 && sigma > 0) {

y--;

delta -= y + 1;

} else {

x++;

delta += x - y;

y--;

}

}

}

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]