import javax.swing.*; import java.awt.geom.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import javax.imageio.*; import java.io.*; import java.util.Scanner; import java.util.Random; public class Geometrix extends JFrame { public static Geometrix window; public Canvas c; public wObject s1, s2; public Geometrix() { c = new Canvas(); // BEGIN INITIAL UNIVERSE //c.addObj(100, 100, 50, 20, wObject.RECT, new Color(0xffa800)); c.addObj(200, 100, 20, 50, wObject.CIRCLE, new Color(0x7dbdff)); // END INITIAL UNIVERSE c.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.getClickCount() == 2) { c.duplicate = true; } else { c.duplicate = false; if (e.getButton() == e.BUTTON3) { c.resize = true; } else { c.resize = false; } } c.mDown(new Point(e.getX(), e.getY())); } }); c.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { c.mUp(); } }); c.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { c.mDrag(new Point(e.getX(), e.getY())); } }); add(c); pack(); } public static void main(String args[]) { window = new Geometrix(); window.setLocation(500, 400); window.setSize(500, 400); window.setTitle("Geometrix 1.0"); window.setDefaultCloseOperation(window.EXIT_ON_CLOSE); window.setVisible(true); } private class Canvas extends JComponent { private Graphics2D g2; private BufferedImage timg; private wObject wObjects[]; private wObject active; public boolean resize, duplicate, delete, consoleMode; public Dimension lastknownsize; private Point downat, locat, sizeat; public Canvas() { if (wObjects == null) { wObjects = new wObject[0]; } } public void addObj(int x, int y, int w, int h, int type, Color c) { wObject temp[] = new wObject[wObjects.length + 1]; System.arraycopy(wObjects, 0, temp, 0, wObjects.length); wObjects = new wObject[temp.length]; System.arraycopy(temp, 0, wObjects, 0, temp.length); wObjects[wObjects.length - 1] = new wObject(wObjects.length, x, y, w, h, type, c); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setBackground(new Color(0x232323)); g2.clearRect(0, 0, getWidth(), getHeight()); g2.setColor(Color.white); g2.draw(new Rectangle2D.Double(5, 5, getWidth() - 10, getHeight() - 30)); //****************************************************************** /*BufferedImage img = null; try { img = ImageIO.read(new File("space.jpg")); } catch (IOException e) { } g2.drawImage(img, 6, 6, null);*/ //****************************************************************** for ( wObject thisobject : wObjects ) { if (thisobject != null) { g2.setColor(thisobject.c); g2.drawPolygon(thisobject.p); } } g2.setColor(Color.gray); g2.drawString("Total entities: " + Integer.toString(wObjects.length), 5, getHeight() - 8); if (active != null) { if (resize) { g2.drawString("Active entity size: (" + active.w + ", " + active.h + ") | area: " + active.area, 100, getHeight() - 8); } else { g2.drawString("Active entity location: (" + active.x + ", " + active.y + ")", 100, getHeight() - 8); } } g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } public void mDown(Point e) { wObject lastActive = active; for ( wObject thisobject : wObjects ) { if (thisobject != null) { if (thisobject.p.contains(e.x, e.y)) { active = thisobject; downat = new Point(e.x, e.y); locat = new Point(thisobject.x, thisobject.y); sizeat = new Point(thisobject.w, thisobject.h); } } } if (active == lastActive) { active = null; } if (duplicate) { addObj(active.x + (int) (active.w / 2), active.y + (int) (active.h / 2), active.w, active.h, active.t, active.c); } pInfoRegion(); pDrawRegion(); } public void mDrag(Point e) { if (active != null) { if (resize) { active.w = sizeat.x + (e.x - downat.x); active.h = sizeat.y + (e.y - downat.y); } else { active.x = e.x - (downat.x - locat.x); active.y = e.y - (downat.y - locat.y); } active.update(); pDrawRegion(); pInfoRegion(); } } public void mUp() { if (active != null) { active = null; pInfoRegion(); } } public void pDrawRegion() { repaint(5, 5, getWidth() - 10, getHeight() - 30); } public void pInfoRegion() { repaint(5, getHeight() - 20, getWidth() - 10, 20); } public void doConsole() { String command = new String(); Scanner in = new Scanner(System.in); while (command.compareTo("release") != 0) { command = in.nextLine(); } } } public class wObject extends JComponent { public int id; public int x, y; public int w, h; public Polygon p; public Color c; public int t; public int area; public int a() { return (int) Math.PI * ((w) * (h)); } public static final int RECT = 0, CIRCLE = 1; public wObject(int idid, int xx, int yy, int ww, int hh, int type, Color cc) { id = idid; x = xx; y = yy; w = ww; h = hh; c = cc; t = type; update(); } public String getType() { switch (t) { case 0: if (w == h) { return "Square"; } else { return "Rectange"; } case 1: if (w == h) { return "Circle"; } else { return "Ellipse"; } default: return "Custom Polygon"; } } public void update() { p = new Polygon(); if (t == RECT) { p.addPoint(x, y); p.addPoint(x + w, y); p.addPoint(x + w, y + h); p.addPoint(x, y + h); area = w * h; } if (t == CIRCLE) { for (double i = 0; i < 2 * Math.PI; i = i + 0.1) { p.addPoint(x + (int) (w * Math.cos(i)), y + (int) (h * Math.sin(i))); } area = (int) Math.PI * ((w) * (h)); } System.out.println("Polygon " + id + " (" + getType() + ") modified to: [ X:" + x + " | Y:" + y + " | Width:" + w + " | Height:" + h + " ]"); } } }