import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
/**
*
* @author AvrDragon
*/
public class RectEditor extends JPanel {
public static float rot = 0;
List<Rect> rects = new ArrayList<>();
List<Rect> selected = new ArrayList<>();
public RectEditor() {
rects.add(new Rect(200, 200, 200, 100, (float)Math.toRadians(45)));
// rects.add(new Rect(400, 100, 60, 44, (float)Math.toRadians(0)));
// rects.add(new Rect(300, 400, 120, 80, (float)Math.toRadians(133)));
setFocusable(true);
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_Q) {
rot += 0.04;
repaint();
}
if(e.getKeyCode() == KeyEvent.VK_E) {
rot -= 0.04;
repaint();
}
}
});
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
for(Rect r : rects) {
if(r.contains(p)) {
r.clr = Settings.MOUSE_OVER_COLOR;
} else {
r.clr = Color.BLACK;
}
}
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
});
addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
if(!e.isControlDown()) {
selected.clear();
}
Point p = e.getPoint();
for(Rect r : rects) {
if(r.contains(p)) {
if(!selected.contains(r)) {
selected.add(r);
} else if(e.isControlDown()) {
selected.remove(r);
}
}
}
repaint();
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.GRAY);
g2.fill(g2.getClip());
if(Settings.paintAxes) {
g2.setColor(Settings.AXES_COLOR);
for(int i = 0; i < g2.getClipBounds().width; i += 100) {
g2.draw(new Line2D.Float(i, 0, i, g2.getClipBounds().height));
}
for(int i = 0; i < g2.getClipBounds().height; i += 100) {
g2.draw(new Line2D.Float(0, i, g2.getClipBounds().width, i));
}
}
for(Rect r : rects) {
AffineTransform oldTransform = g2.getTransform();
g2.setColor(r.clr);
Rectangle2D rect = new Rectangle2D.Float(r.x, r.y, r.w, r.h);
g2.translate(-r.w / 2, -r.h / 2);
g2.rotate(r.r, r.x + r.w / 2, r.y + r.h / 2);
g2.draw(rect);
if(selected.contains(r)) {
Rectangle2D.Float selectionBorder = (Rectangle2D.Float) rect.clone();
selectionBorder.x -= Settings.SELECTION_BORDER_INSET;
selectionBorder.y -= Settings.SELECTION_BORDER_INSET;
selectionBorder.width += Settings.SELECTION_BORDER_INSET * 2;
selectionBorder.height += Settings.SELECTION_BORDER_INSET * 2;
g2.setColor(Settings.SELECTION_COLOR);
g2.draw(selectionBorder);
}
g2.setTransform(oldTransform);
if(Settings.paintCenter) {
g2.setColor(Settings.CENTER_COLOR);
g2.fillOval((int)r.getCenter().x - 1, (int)r.getCenter().y - 1, 3, 3);
}
if(Settings.paintEdgePoints) {
g2.setColor(Settings.EDGE_POINTS_COLOR);
g2.fillOval((int)r.getPointNW().x - 1, (int)r.getPointNW().y - 1, 3, 3);
g2.fillOval((int)r.getPointNE().x - 1, (int)r.getPointNE().y - 1, 3, 3);
g2.fillOval((int)r.getPointSW().x - 1, (int)r.getPointSW().y - 1, 3, 3);
g2.fillOval((int)r.getPointSE().x - 1, (int)r.getPointSE().y - 1, 3, 3);
g2.fillOval((int)r.getPointN().x - 1, (int)r.getPointN().y - 1, 3, 3);
g2.fillOval((int)r.getPointW().x - 1, (int)r.getPointW().y - 1, 3, 3);
g2.fillOval((int)r.getPointE().x - 1, (int)r.getPointE().y - 1, 3, 3);
g2.fillOval((int)r.getPointS().x - 1, (int)r.getPointS().y - 1, 3, 3);
}
if(Settings.paintEdges) {
g2.setColor(Settings.EDGES_COLOR);
g2.draw(r.getEdgeN());
g2.draw(r.getEdgeW());
g2.draw(r.getEdgeS());
g2.draw(r.getEdgeE());
}
if(Settings.paintAABB) {
g2.setColor(Settings.AABB_COLOR);
g2.draw(r.getAABB());
}
if(Settings.paintResizeBounds) {
List<Rect> resizeBorders = new ArrayList<>();
resizeBorders.add(r.getRectN(Settings.RESIZE_BORDER_INSET));
resizeBorders.add(r.getRectW(Settings.RESIZE_BORDER_INSET));
resizeBorders.add(r.getRectE(Settings.RESIZE_BORDER_INSET));
resizeBorders.add(r.getRectS(Settings.RESIZE_BORDER_INSET));
for(Rect rr : resizeBorders) {
oldTransform = g2.getTransform();
g2.setColor(Settings.RESIZE_BOUNDS_COLOR);
rect = new Rectangle2D.Float(rr.x, rr.y, rr.w, rr.h);
g2.translate(-rr.w / 2, -rr.h / 2);
g2.rotate(rr.r, rr.x + rr.w / 2, rr.y + rr.h / 2);
g2.draw(rect);
g2.setTransform(oldTransform);
}
}
}
}
}