import javax.swing.*;
import java.awt.*;
import java.util.Vector;
import java.util.logging.Logger;
public class Panel extends JPanel implements Runnable
{
int angle=0;
final int STEP=5;
final int INTERVAL=100;
public Panel()
{
}
@Override
public void run()
{
while (true)
{
try
{
Thread.sleep(INTERVAL);
}
catch (Exception e)
{
e.printStackTrace();
}
this.repaint();
}
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
int r=20;
Vector<Point> points=new Vector<>();
points.add(new Point(-r,-r));
points.add(new Point(r,-r));
points.add(new Point(r,-2*r));
points.add(new Point(-r,-2*r));
Point center=new Point(100,100);
this.drawTankAngle(points, center, g, angle);
angle+=STEP;
}
private double getAngleByPoint(float x, float y)
{
return y>0 ? Math.acos( x / Math.sqrt(x * x + y * y)) : 2 * Math.PI - Math.acos(x / Math.sqrt(x * x + y * y));
}
public void drawTankAngle(Vector<Point> points, Point center, Graphics g, int angle)
{
int iCenterX = (int)center.getX();
int iCenterY = (int)center.getY();
int[] xPoints = new int[points.size()];
int[] yPoints = new int[points.size()];
for(int i = 0; i < points.size(); i++)
{
xPoints[i] = (int)(points.get(i).getX());
yPoints[i] = (int)(points.get(i).getY());
float x = xPoints[i];
float y = yPoints[i];
double d = getAngleByPoint(x, y) + angle / 360.0f * 2 * Math.PI;
xPoints[i] = (int)(Math.abs(Math.sqrt(x * x + y * y)) * Math.cos(d)) + iCenterX;
yPoints[i] = (int)(Math.abs(Math.sqrt(x * x + y * y)) * Math.sin(d)) + iCenterY;
}
g.setColor(Color.yellow);
g.drawString("0", iCenterX, iCenterY);
g.drawPolygon(xPoints, yPoints, xPoints.length);
}
}
import java.awt.*;
import java.util.Vector;
import java.util.logging.Logger;
public class Panel extends JPanel implements Runnable
{
int angle=0;
final int STEP=5;
final int INTERVAL=100;
public Panel()
{
}
@Override
public void run()
{
while (true)
{
try
{
Thread.sleep(INTERVAL);
}
catch (Exception e)
{
e.printStackTrace();
}
this.repaint();
}
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
int r=20;
Vector<Point> points=new Vector<>();
points.add(new Point(-r,-r));
points.add(new Point(r,-r));
points.add(new Point(r,-2*r));
points.add(new Point(-r,-2*r));
Point center=new Point(100,100);
this.drawTankAngle(points, center, g, angle);
angle+=STEP;
}
private double getAngleByPoint(float x, float y)
{
return y>0 ? Math.acos( x / Math.sqrt(x * x + y * y)) : 2 * Math.PI - Math.acos(x / Math.sqrt(x * x + y * y));
}
public void drawTankAngle(Vector<Point> points, Point center, Graphics g, int angle)
{
int iCenterX = (int)center.getX();
int iCenterY = (int)center.getY();
int[] xPoints = new int[points.size()];
int[] yPoints = new int[points.size()];
for(int i = 0; i < points.size(); i++)
{
xPoints[i] = (int)(points.get(i).getX());
yPoints[i] = (int)(points.get(i).getY());
float x = xPoints[i];
float y = yPoints[i];
double d = getAngleByPoint(x, y) + angle / 360.0f * 2 * Math.PI;
xPoints[i] = (int)(Math.abs(Math.sqrt(x * x + y * y)) * Math.cos(d)) + iCenterX;
yPoints[i] = (int)(Math.abs(Math.sqrt(x * x + y * y)) * Math.sin(d)) + iCenterY;
}
g.setColor(Color.yellow);
g.drawString("0", iCenterX, iCenterY);
g.drawPolygon(xPoints, yPoints, xPoints.length);
}
}