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

package mainframe;

import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
import java.net.*;
import java.io.*;

/**
*
* @author eugene & valera
*/


class DrawComponent extends JComponent
{
public DrawComponent(NPanel panel)
{
squares = new ArrayList<Rectangle2D>();
current = null;
pan=panel;
Runnable ob = new DrawOblRunnable(this);
Thread t = new Thread(ob);
t.start();
}

private Line2D buildOsX() //build OsX
{
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;
double width = rad+1;
double height = rad+1;

Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
return rect;
}

private GeneralPath buildTr() //Build triangle
{
GeneralPath tr = new GeneralPath();
tr.moveTo(G_WIDTH/2, G_HEIGHT/2);
tr.lineTo(G_WIDTH/2-rad/2, G_HEIGHT/2);
tr.lineTo(G_WIDTH/2, G_HEIGHT/2+rad/2);
tr.closePath();

return tr;
}

private Arc2D buildSector()
{
Arc2D sector = new Arc2D.Double(G_WIDTH/2-rad,
G_HEIGHT/2-rad,2*rad,2*rad,270, 90,Arc2D.PIE);

return sector;
}

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;

g2.setPaint(this.oblColor);

//draw Rectangle
Rectangle2D rect = this.buildRec();
g2.fill(rect);

//draw Triangle
GeneralPath tr = this.buildTr();
g2.fill(tr);

//draw sector
Arc2D sector = this.buildSector();
g2.fill(sector);

g2.setPaint(Color.BLACK);
g2.setStroke(new BasicStroke(2));//Change line width

//draw OsX
Line2D linex = this.buildOsX();
g2.draw(linex);

area = new Shape[] {sector, tr, rect};

//draw OsY
Line2D liney = this.buildOsY();
g2.draw(liney);

//draw Riski i 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));//Change line width

if (squares.size()>8)
{
squares.remove(0);
}

for (int sqi=0; sqi < (squares.size()); sqi ++)
{
String isin = this.isContain(new Point2D.Double(squares.get(sqi).getX(),squares.get(sqi).getY()));

if (isin.equals("true"))
{
g2.setPaint(Color.RED);
g2.draw(squares.get(sqi));//}
}
else if (isin.equals("false"))
{
g2.setPaint(Color.BLUE);
g2.draw(squares.get(sqi));//}
}
else
{
g2.setPaint(Color.BLACK);
g2.draw(squares.get(sqi));//}

//g2.drawString(this.isContain(new Point2D.Double(squares.get(sqi).getX(),squares.get(sqi).getY())), G_WIDTH/12, (int)(G_HEIGHT/1.5));
}
}
}

String isContain(Point2D p)
{
String boolbuffer="false";


try {
String datax = Double.toString(this.pan.setDecX(p.getX(), this));
String datay = Double.toString(this.pan.setDecY(p.getY(), this));
String datar = Double.toString(pan.getLogicRad());

String urlserv = "http://localhost:8080/FileWork/servlet1";
String urlparam = "param1=" + datax + "&param2=" + datay + "&param3=" + datar;
boolbuffer=sendGetRequest(urlserv, urlparam);
}
catch(Exception e) {
System.out.print("Something wrong with server!\n");

boolbuffer = e.toString();
}
return boolbuffer;
}

public static String sendGetRequest(String endpoint, String requestParameters)
{
String result = null;
if (endpoint.startsWith("http://"))
{
// Send a GET request to the servlet
try
{
// Construct data
StringBuffer data = new StringBuffer();
// Send data
String urlStr = endpoint;
if (requestParameters != null && requestParameters.length() > 0)
{
urlStr += "?" + requestParameters;
}
URL url = new URL(urlStr);

URLConnection conn = url.openConnection ();
conn.setConnectTimeout(10);
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null)
{
sb.append(line);
}
rd.close();
result = sb.toString();
}
catch (Exception e)
{
result = e.toString();
}
}
return result;
}


boolean isContain2(Point2D p)
{
for (Shape s: area)
{
if (s.contains(p)) return true;
}
return false;
}

void add(Point2D p)
{
double x = p.getX();
double y = p.getY();

current = new Rectangle2D.Double(x,y,1,1);
squares.add(current);

this.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();
}
}

public final int G_WIDTH = 800;//DrawFrame.getMonWidth()/16*13;
public final int G_HEIGHT = 500;//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;

public ArrayList<Rectangle2D> squares;
private Rectangle2D current;
private Shape[] area;
private NPanel pan;
public static int co = 0;
Thread []th=new Thread[2];

Color bcol;
Color rcol;
Color bkcol;
Color oblColor;
}

class DrawOblRunnable implements Runnable
{
public DrawOblRunnable(DrawComponent aComponent)
{
component = aComponent;
}

public void run()
{
try
{
int jbk1=0;
int jbk2=0xFF;
int jbk3=0xFF;

component.oblColor=new Color(jbk1,jbk2,jbk3);
component.repaint();

while (true)
{
for (int i = 0; i<STEPS-1; i++)
{
Thread.sleep(DELAY);
component.repaint();
component.oblColor=new Color(jbk1,jbk2=jbk2-1,jbk3);

}
for (int i = 0; i<STEPS-1; i++)
{
Thread.sleep(DELAY);
component.repaint();
component.oblColor=new Color(jbk1,jbk2=jbk2+1,jbk3=jbk3-1);
}
for (int i = 0; i<STEPS-1; i++)
{
Thread.sleep(DELAY);
component.repaint();
component.oblColor=new Color(jbk1,jbk2,jbk3=jbk3+1);
}
}

}
catch (InterruptedException e)
{
}
}
private DrawComponent component;
public static final int STEPS = 180;
public static final int DELAY = 15;
}


Соседние файлы в папке mainframe