Скачиваний:
65
Добавлен:
09.05.2014
Размер:
3.92 Кб
Скачать
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.applet.Applet;
import java.awt.*;

public class NewApplet1 extends Applet {

    private final int WINDX = 500;
    private final int WINDY = 500;

    private final int centerX = WINDX / 2;
    private final int centerY = WINDY / 2;

    private final int size = 5;
    private final int hR = 25;

    private int xPoints[] = {centerX, centerX, centerX + hR};
    private int yPoints[] = {centerY, centerY - hR, centerY};

    public double R;
    public double x;
    public double y;

    private String[] letters={"-2R","","-R","-R/2","0","R/2","R","","2R"};
    private Boolean Col;

    private int pointx;
    private int pointy;
    private GraphicsCanvas gc;

    private String param1;
    private String param2;
    private String param3;
    private String param4;

    @Override
    public void init() {
        param1 = new String();
        param2 = new String();
        param3 = new String();
        param4 = new String();
        param1 = getParameter("X");
        param2 = getParameter("Y");
        param3 = getParameter("R");
        param4 = getParameter("in");
        try
        {
            if ((param1 != null)&&(param2 != null)&&(param3 != null))
            {
                x = Double.parseDouble(param1);
                y = Double.parseDouble(param2);
                R = Double.parseDouble(param3);
                Col = Boolean.parseBoolean(param4);
            }
            else
            {
                x = 0;
                y = 0;
                R = 1;
                Col = true;
            }
        } catch (NumberFormatException e) {}

        setLayout(new BorderLayout());
        gc = new GraphicsCanvas();
        add(gc, BorderLayout.CENTER);
        setSize(WINDX, WINDY);
    }

    @Override
    public void paint(Graphics g) {
        if (gc.getGraphics() != null) {
            gc.repaint();
        }
    }

    /*
     * Метод для вывода графика и точек на экран
     */
    private void drawArea(Graphics g) {

        g.setColor(Color.blue);
        g.fillArc(centerX-2*hR, centerY-2*hR, 4*hR, 4*hR, 90, 90);
        g.fillRect(centerX, centerY, hR, 2*hR);
        g.fillPolygon(xPoints, yPoints, xPoints.length);
        String str = new String();
        str = "("+param1+"; "+param2+")" ;
        g.drawString(str,5,20);
        g.setColor(Color.black);
        g.drawLine(centerX - 150, centerY, centerX + 150, centerY);
        g.drawLine(centerX, centerY - 150, centerX, centerY + 150);

        for (int i = -2; i <= 2; i++) {
            g.drawLine(centerX - size / 2, centerY + i * hR, centerX + size / 2, centerY + i * hR);
            g.drawLine(centerX + i * hR, centerY - size / 2, centerX + i * hR, centerY + size / 2);
            if (i != 0) {
                g.drawString(letters[i+4], centerX + i * hR - size, centerY - size);
                g.drawString(letters[i+4], centerX + size, centerY - i * hR + size / 2);
            }
        }

        double p = 2 * hR/R;
        pointx = centerX + (int) Math.round(p * x);
        pointy = centerY - (int) Math.round(p * y);
        if (Col) {
            g.setColor(Color.GREEN);
        }
        else {
            g.setColor(Color.RED);
        }
        g.fillRect(pointx - 2, pointy - 2, 4, 4);
        g.setColor(Color.black);
    }

    class GraphicsCanvas extends Canvas {
        public GraphicsCanvas() {
            setSize(WINDX, WINDY);
            setBackground(Color.WHITE);
        }
        @Override
        public void paint(Graphics g) {
            drawArea(g);
        }
    }
}