Добавил:
Eatmore
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:отчеты по лабораторным работам / лабораторная работа 3 / MainFrame / src / mainframe / DrawComponent
.java package mainframe;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
class DrawComponent extends JComponent {
public final int G_WIDTH = DrawFrame.getMonWidth() / 16 * 13;
public final int G_HEIGHT = DrawFrame.getMonHeight() / 16 * 15;
public final int X_BOUND = G_WIDTH / 10;
public final int Y_BOUND = G_HEIGHT / 12;
public final double rad = G_HEIGHT * 3 / 8;
private ArrayList<Rectangle2D> squares;
private ArrayList<States> states;
private Rectangle2D current;
private Shape[] area;
public DrawComponent() {
squares = new ArrayList<Rectangle2D>();
states = new ArrayList<States>();
current = null;
}
// Ось ОХ
private Line2D buildOsX() {
double x0 = X_BOUND;
double y0 = G_HEIGHT / 2;
double xk = G_WIDTH - X_BOUND;
double yk = G_HEIGHT / 2;
Line2D osx = new Line2D.Double(x0, y0, xk, yk);
return osx;
}
// Ось ОУ
private Line2D buildOsY() {
double x0 = G_WIDTH / 2;
double y0 = Y_BOUND;
double xk = G_WIDTH / 2;
double yk = G_HEIGHT - Y_BOUND;
Line2D osy = new Line2D.Double(x0, y0, xk, yk);
return osy;
}
// Деления на оси ОХ
private Line2D buildRiskaX(double x0) {
Line2D risx = new Line2D.Double(x0,
(double) (G_HEIGHT / 2 - G_HEIGHT / 100),
x0,
(double) (G_HEIGHT / 2 + G_HEIGHT / 100));
return risx;
}
// Деления на оси ОУ
private Line2D buildRiskaY(double y0) {
Line2D risy = new Line2D.Double(G_WIDTH / 2 - G_HEIGHT / 100,
y0,
G_WIDTH / 2 + G_HEIGHT / 100,
y0);
return risy;
}
// Прямоугольник
private Rectangle2D buildRec() {
double leftX = G_WIDTH / 2;
double topY = G_HEIGHT / 2 - rad / 2;
double width = rad + 1;
double height = rad / 2 + 1;
Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
return rect;
}
// Треугольник
private GeneralPath buildTr() {
GeneralPath tr = new GeneralPath();
tr.moveTo(G_WIDTH / 2 + 1, G_HEIGHT / 2 + 1);
tr.lineTo(G_WIDTH / 2, G_HEIGHT / 2 + rad);
tr.lineTo(G_WIDTH / 2 - rad - 1, G_HEIGHT / 2);
tr.closePath();
return tr;
}
// Сектор
private Arc2D buildSector() {
Arc2D sector = new Arc2D.Double(G_WIDTH / 2 - rad / 2,
G_HEIGHT / 2 - rad / 2, rad + 1, rad + 1, 90, 90, Arc2D.PIE);
return sector;
}
// Панель, на которой отображаются наша область
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.green);
// Рисуем прямоугольник
Rectangle2D rect = this.buildRec();
g2.fill(rect);
// Рисуем треугольник
GeneralPath tr = this.buildTr();
g2.fill(tr);
// Рисуем сектор
Arc2D sector = this.buildSector();
g2.fill(sector);
g2.setPaint(Color.BLACK); // Заполнение черным
g2.setStroke(new BasicStroke(2)); // Изменение ширины линии
// Рисуем ось ОХ
Line2D linex = this.buildOsX();
g2.draw(linex);
area = new Shape[]{sector, tr, rect};
// Рисуем ось ОУ
Line2D liney = this.buildOsY();
g2.draw(liney);
// Рисуем деления и R
Line2D[] riska = new Line2D[5];
for (int i = 0; i < 5; i++) {
riska[i] = this.buildRiskaX(G_WIDTH / 2 - rad + i * (rad / 2));
g2.draw(riska[i]);
riska[i] = this.buildRiskaY(G_HEIGHT / 2 - rad + i * (rad / 2));
g2.draw(riska[i]);
}
g2.drawString("R", G_WIDTH / 2 + G_WIDTH / 160, (int) (G_HEIGHT / 2 - rad - G_HEIGHT / 80));
g2.drawString("R", (int) (G_WIDTH / 2 + G_WIDTH / 160 + rad), (int) (G_HEIGHT / 2 - G_HEIGHT / 80));
g2.drawString("R/2", G_WIDTH / 2 + G_WIDTH / 160, (int) (G_HEIGHT / 2 - rad / 2 - G_HEIGHT / 80));
g2.drawString("R/2", (int) (G_WIDTH / 2 + G_WIDTH / 160 + rad / 2), (int) (G_HEIGHT / 2 - G_HEIGHT / 80));
g2.drawString("0", (int) (G_WIDTH / 2 + G_WIDTH / 160), (int) (G_HEIGHT / 2 - G_HEIGHT / 80));
g2.setStroke(new BasicStroke(3)); // Изменение ширины линии
if (!squares.isEmpty()) {
ListIterator sqIter = squares.listIterator();
ListIterator stIter = states.listIterator();
for ( ; sqIter.hasNext() && stIter.hasNext(); ) {
switch ((States)stIter.next()) {
case InArea:
g2.setPaint(Color.RED);
break;
case NotInArea:
g2.setPaint(Color.BLUE);
break;
case NoResponse:
g2.setPaint(Color.BLACK);
break;
}
g2.draw((Shape) sqIter.next());
}
}
}
void add(Point2D p, States s) {
double x = p.getX();
double y = p.getY();
current = new Rectangle2D.Double(x, y, 1, 1);
squares.add(current); // Добавляем компонент current в контейнер
states.add(s);
repaint();
}
void repaintPoints(NPanel panel) {
Rectangle2D bufrec = new Rectangle2D.Double();
for (int i = 0; i < squares.size(); i++) {
bufrec = squares.get(i);
double nx = panel.setPixelX(((panel.setDecX(bufrec.getCenterX(), this))
* panel.getOldLogicRad() / panel.getLogicRad()), this) - 0.5;
double ny = panel.setPixelY(((panel.setDecY(bufrec.getCenterY(), this))
* panel.getOldLogicRad() / panel.getLogicRad()), this) - 0.5;
bufrec = new Rectangle2D.Double(nx, ny, 1, 1);
squares.set(i, bufrec);
repaint();
}
}
}
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
class DrawComponent extends JComponent {
public final int G_WIDTH = DrawFrame.getMonWidth() / 16 * 13;
public final int G_HEIGHT = DrawFrame.getMonHeight() / 16 * 15;
public final int X_BOUND = G_WIDTH / 10;
public final int Y_BOUND = G_HEIGHT / 12;
public final double rad = G_HEIGHT * 3 / 8;
private ArrayList<Rectangle2D> squares;
private ArrayList<States> states;
private Rectangle2D current;
private Shape[] area;
public DrawComponent() {
squares = new ArrayList<Rectangle2D>();
states = new ArrayList<States>();
current = null;
}
// Ось ОХ
private Line2D buildOsX() {
double x0 = X_BOUND;
double y0 = G_HEIGHT / 2;
double xk = G_WIDTH - X_BOUND;
double yk = G_HEIGHT / 2;
Line2D osx = new Line2D.Double(x0, y0, xk, yk);
return osx;
}
// Ось ОУ
private Line2D buildOsY() {
double x0 = G_WIDTH / 2;
double y0 = Y_BOUND;
double xk = G_WIDTH / 2;
double yk = G_HEIGHT - Y_BOUND;
Line2D osy = new Line2D.Double(x0, y0, xk, yk);
return osy;
}
// Деления на оси ОХ
private Line2D buildRiskaX(double x0) {
Line2D risx = new Line2D.Double(x0,
(double) (G_HEIGHT / 2 - G_HEIGHT / 100),
x0,
(double) (G_HEIGHT / 2 + G_HEIGHT / 100));
return risx;
}
// Деления на оси ОУ
private Line2D buildRiskaY(double y0) {
Line2D risy = new Line2D.Double(G_WIDTH / 2 - G_HEIGHT / 100,
y0,
G_WIDTH / 2 + G_HEIGHT / 100,
y0);
return risy;
}
// Прямоугольник
private Rectangle2D buildRec() {
double leftX = G_WIDTH / 2;
double topY = G_HEIGHT / 2 - rad / 2;
double width = rad + 1;
double height = rad / 2 + 1;
Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
return rect;
}
// Треугольник
private GeneralPath buildTr() {
GeneralPath tr = new GeneralPath();
tr.moveTo(G_WIDTH / 2 + 1, G_HEIGHT / 2 + 1);
tr.lineTo(G_WIDTH / 2, G_HEIGHT / 2 + rad);
tr.lineTo(G_WIDTH / 2 - rad - 1, G_HEIGHT / 2);
tr.closePath();
return tr;
}
// Сектор
private Arc2D buildSector() {
Arc2D sector = new Arc2D.Double(G_WIDTH / 2 - rad / 2,
G_HEIGHT / 2 - rad / 2, rad + 1, rad + 1, 90, 90, Arc2D.PIE);
return sector;
}
// Панель, на которой отображаются наша область
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.green);
// Рисуем прямоугольник
Rectangle2D rect = this.buildRec();
g2.fill(rect);
// Рисуем треугольник
GeneralPath tr = this.buildTr();
g2.fill(tr);
// Рисуем сектор
Arc2D sector = this.buildSector();
g2.fill(sector);
g2.setPaint(Color.BLACK); // Заполнение черным
g2.setStroke(new BasicStroke(2)); // Изменение ширины линии
// Рисуем ось ОХ
Line2D linex = this.buildOsX();
g2.draw(linex);
area = new Shape[]{sector, tr, rect};
// Рисуем ось ОУ
Line2D liney = this.buildOsY();
g2.draw(liney);
// Рисуем деления и R
Line2D[] riska = new Line2D[5];
for (int i = 0; i < 5; i++) {
riska[i] = this.buildRiskaX(G_WIDTH / 2 - rad + i * (rad / 2));
g2.draw(riska[i]);
riska[i] = this.buildRiskaY(G_HEIGHT / 2 - rad + i * (rad / 2));
g2.draw(riska[i]);
}
g2.drawString("R", G_WIDTH / 2 + G_WIDTH / 160, (int) (G_HEIGHT / 2 - rad - G_HEIGHT / 80));
g2.drawString("R", (int) (G_WIDTH / 2 + G_WIDTH / 160 + rad), (int) (G_HEIGHT / 2 - G_HEIGHT / 80));
g2.drawString("R/2", G_WIDTH / 2 + G_WIDTH / 160, (int) (G_HEIGHT / 2 - rad / 2 - G_HEIGHT / 80));
g2.drawString("R/2", (int) (G_WIDTH / 2 + G_WIDTH / 160 + rad / 2), (int) (G_HEIGHT / 2 - G_HEIGHT / 80));
g2.drawString("0", (int) (G_WIDTH / 2 + G_WIDTH / 160), (int) (G_HEIGHT / 2 - G_HEIGHT / 80));
g2.setStroke(new BasicStroke(3)); // Изменение ширины линии
if (!squares.isEmpty()) {
ListIterator sqIter = squares.listIterator();
ListIterator stIter = states.listIterator();
for ( ; sqIter.hasNext() && stIter.hasNext(); ) {
switch ((States)stIter.next()) {
case InArea:
g2.setPaint(Color.RED);
break;
case NotInArea:
g2.setPaint(Color.BLUE);
break;
case NoResponse:
g2.setPaint(Color.BLACK);
break;
}
g2.draw((Shape) sqIter.next());
}
}
}
void add(Point2D p, States s) {
double x = p.getX();
double y = p.getY();
current = new Rectangle2D.Double(x, y, 1, 1);
squares.add(current); // Добавляем компонент current в контейнер
states.add(s);
repaint();
}
void repaintPoints(NPanel panel) {
Rectangle2D bufrec = new Rectangle2D.Double();
for (int i = 0; i < squares.size(); i++) {
bufrec = squares.get(i);
double nx = panel.setPixelX(((panel.setDecX(bufrec.getCenterX(), this))
* panel.getOldLogicRad() / panel.getLogicRad()), this) - 0.5;
double ny = panel.setPixelY(((panel.setDecY(bufrec.getCenterY(), this))
* panel.getOldLogicRad() / panel.getLogicRad()), this) - 0.5;
bufrec = new Rectangle2D.Double(nx, ny, 1, 1);
squares.set(i, bufrec);
repaint();
}
}
}
Соседние файлы в папке mainframe