madhawaseeeee

javam1

Mar 2nd, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.20 KB | None | 0 0
  1.  
  2. So am trying to retrieve the coordinates of a line 2D that i will draw on my JPanel. The getBounds() method seemed to work for a rectangle2D but is not appripriate for a line2D.. How can i get the the coordinates of a line2D and put them in a variable as i did for the rectangle2D below:
  3.  
  4. private Line2D.Float drawLine(int x1, int y1, int x2, int y2) {
  5.  
  6. return new Line2D.Float(x1, y1, x2, y2);
  7. }
  8.  
  9. This is where i want to get the coordinates inside variables.
  10.  
  11. if(var>1){
  12. for(int i=0;i<var-1;i++){
  13. Shape s = shapes.get(i);
  14.  
  15.  
  16.  
  17. double x1 = (s.getBounds2D().getX1());
  18. double y1 = (s.getBounds2D().getY1());
  19. double x2 = (s.getBounds2D().getX2());
  20. double y2 = (s.getBounds2D().getY2());
  21.  
  22. }
  23. }
  24.  
  25. Can anyone help me out ?
  26.  
  27. So am trying to retrieve the coordinates of a line 2D that i will draw on my JPanel. The getBounds() method seemed to work for a rectangle2D but is not appripriate for a line2D.. How can i get the the coordinates of a line2D and put them in a variable as i did for the rectangle2D below: Here is my entire code!
  28.  
  29. import java.awt.AlphaComposite;
  30. import java.awt.BasicStroke;
  31. import java.awt.BorderLayout;
  32. import java.awt.Color;
  33. import java.awt.Dimension;
  34. import java.awt.Graphics;
  35. import java.awt.Graphics2D;
  36. import java.awt.Point;
  37. import java.awt.Rectangle;
  38. import java.awt.RenderingHints;
  39. import java.awt.Shape;
  40. import java.awt.event.ActionEvent;
  41. import java.awt.event.ActionListener;
  42. import java.awt.event.MouseAdapter;
  43. import java.awt.event.MouseEvent;
  44. import java.awt.event.MouseMotionAdapter;
  45. import java.awt.geom.Ellipse2D;
  46. import java.awt.geom.Line2D;
  47. import java.awt.geom.Rectangle2D;
  48. import java.awt.image.BufferedImage;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.io.OutputStream;
  52. import java.io.OutputStreamWriter;
  53. import java.io.Writer;
  54. import java.text.DecimalFormat;
  55. import java.util.ArrayList;
  56. import java.util.Iterator;
  57. import java.util.Scanner;
  58.  
  59. import javax.imageio.ImageIO;
  60. import javax.swing.Box;
  61. import javax.swing.Icon;
  62. import javax.swing.ImageIcon;
  63. import javax.swing.JButton;
  64. import javax.swing.JColorChooser;
  65. import javax.swing.JComponent;
  66. import javax.swing.JFileChooser;
  67. import javax.swing.JFrame;
  68. import javax.swing.JLabel;
  69. import javax.swing.JMenu;
  70. import javax.swing.JMenuBar;
  71. import javax.swing.JMenuItem;
  72. import javax.swing.JOptionPane;
  73. import javax.swing.JPanel;
  74. import javax.swing.JSeparator;
  75. import javax.swing.JSlider;
  76. import javax.swing.JTextField;
  77. import javax.swing.SwingConstants;
  78. import javax.swing.event.ChangeEvent;
  79. import javax.swing.event.ChangeListener;
  80. import javax.swing.filechooser.FileNameExtensionFilter;
  81.  
  82. public class Test extends JFrame {
  83. private int len;
  84.  
  85. // public int getSizes(){
  86. // return len;
  87. //}
  88.  
  89. private static final long serialVersionUID = -140274271716086522L;
  90.  
  91. JMenuBar menubar;
  92. JMenu File, Exit;
  93. JMenuItem New, Open;
  94. JComponent DrawingBoard;
  95.  
  96. JButton lineBut, ellipseBut, rectBut, strokeBut;
  97.  
  98. DecimalFormat dec = new DecimalFormat("#.##");
  99.  
  100. // Contains all of the rules for drawing
  101.  
  102. Graphics2D graphSettings;
  103.  
  104.  
  105. // Going to be used to monitor what shape to draw next
  106.  
  107. int currentAction = 1;
  108.  
  109. // Transparency of the shape
  110.  
  111. // Default stroke and fill colors
  112.  
  113. Color strokeColor = Color.BLACK;
  114.  
  115. public static void main(String[] args) {
  116. new Test();
  117. }
  118.  
  119. public Test() {
  120. // Define the defaults for the JFrame
  121.  
  122. this.setSize(800, 600);
  123. this.setTitle("ERD BUILDER");
  124. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  125. this.setJMenuBar(menubar);
  126.  
  127. JMenuBar menuBar = new JMenuBar();
  128.  
  129. // Add the menubar to the frame
  130. setJMenuBar(menuBar);
  131.  
  132. // Define and add two drop down menu to the menubar
  133. JMenu fileMenu = new JMenu("File");
  134. JMenu editMenu = new JMenu("Edit");
  135. JMenu dbMenu = new JMenu("Database");
  136. JMenu ToolsMenu = new JMenu("Tools");
  137. JMenu HelpMenu = new JMenu("Help");
  138. menuBar.add(fileMenu);
  139. menuBar.add(editMenu);
  140. menuBar.add(dbMenu);
  141. menuBar.add(ToolsMenu);
  142. menuBar.add(HelpMenu);
  143. // Create and add simple menu item to one of the drop down menu
  144. JMenuItem newAction = new JMenuItem("New Project");
  145. JMenuItem openAction = new JMenuItem("Open File");
  146. JMenuItem exitAction = new JMenuItem("Quit");
  147. JMenuItem cutAction = new JMenuItem("Cut");
  148. JMenuItem copyAction = new JMenuItem("Copy");
  149. JMenuItem pasteAction = new JMenuItem("Paste");
  150. JMenuItem UndoAction = new JMenuItem("Undo");
  151. JMenuItem RedoAction = new JMenuItem("Redo");
  152. JMenuItem clearAction = new JMenuItem("Clear");
  153. JMenuItem saveAction = new JMenuItem("Save");
  154. JMenuItem exportAction = new JMenuItem("Export");
  155. JMenuItem printAction = new JMenuItem("Print");
  156. JMenuItem ConvertAction = new JMenuItem("Convert To Tables");
  157. JMenuItem ColorAction = new JMenuItem("Color Picker");
  158. JMenuItem ZoomAction = new JMenuItem("Zoom");
  159. JMenuItem EntityAction = new JMenuItem("Entity & Attributes");
  160. JMenuItem RelationshipAction = new JMenuItem("Relationship Attributes");
  161. JMenuItem HelpAction = new JMenuItem("Help");
  162. JMenuItem AboutAction = new JMenuItem("About");
  163.  
  164. fileMenu.add(newAction);
  165. fileMenu.addSeparator();
  166. fileMenu.add(openAction);
  167. fileMenu.addSeparator();
  168. fileMenu.add(saveAction);
  169. fileMenu.addSeparator();
  170. fileMenu.add(exportAction);
  171. fileMenu.addSeparator();
  172. fileMenu.add(printAction);
  173. fileMenu.addSeparator();
  174. fileMenu.add(exitAction);
  175.  
  176. editMenu.add(UndoAction);
  177. editMenu.addSeparator();
  178. editMenu.add(RedoAction);
  179. editMenu.addSeparator();
  180. editMenu.add(cutAction);
  181. editMenu.addSeparator();
  182. editMenu.add(copyAction);
  183. editMenu.addSeparator();
  184. editMenu.add(pasteAction);
  185. editMenu.addSeparator();
  186. editMenu.add(clearAction);
  187.  
  188. dbMenu.add(ConvertAction);
  189.  
  190. ToolsMenu.add(ColorAction);
  191. ToolsMenu.addSeparator();
  192. ToolsMenu.add(ZoomAction);
  193. ToolsMenu.addSeparator();
  194. ToolsMenu.add(EntityAction);
  195. ToolsMenu.addSeparator();
  196. ToolsMenu.add(RelationshipAction);
  197.  
  198. HelpMenu.add(HelpAction);
  199. HelpMenu.addSeparator();
  200. HelpMenu.add(AboutAction);
  201.  
  202.  
  203. exitAction.addActionListener(new ActionListener() {
  204. public void actionPerformed(ActionEvent arg0) {
  205.  
  206. }}
  207. );
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. ConvertAction.addActionListener(new ActionListener() {
  215. public void actionPerformed(ActionEvent arg0) {
  216.  
  217. new ConvertForm().setVisible(true);
  218.  
  219.  
  220.  
  221. }}
  222. );
  223.  
  224.  
  225.  
  226.  
  227. newAction.addActionListener(new ActionListener() {
  228. public void actionPerformed(ActionEvent arg0) {
  229.  
  230. new Test().setVisible(true);
  231. }
  232. });
  233.  
  234.  
  235.  
  236. JPanel buttonPanel = new JPanel();
  237.  
  238. JPanel FieldPanel = new JPanel();
  239.  
  240. // Swing box that will hold all the buttons
  241.  
  242. Box theBox = Box.createVerticalBox();
  243. Box theBoxs = Box.createVerticalBox();
  244.  
  245. // Make all the buttons in makeMeButtons by passing the
  246. // button icon.
  247.  
  248.  
  249. lineBut = makeMeButtons("./src/line.png", 2);
  250. ellipseBut = makeMeButtons("./src/ellipse.png", 3);
  251. rectBut = makeMeButtons("./src/rectangle.png", 4);
  252.  
  253. // Make all the buttons in makeMeColorButton by passing the
  254. // button icon and true for stroke color or false for fill
  255.  
  256. strokeBut = makeMeColorButton("./src/stroke.png", 5, true);
  257.  
  258.  
  259. // Add the fields to the boxs
  260.  
  261.  
  262.  
  263. //theBox.add(brushBut);
  264. theBox.add(lineBut);
  265. theBox.add(ellipseBut);
  266. theBox.add(rectBut);
  267. theBox.add(strokeBut);
  268.  
  269. buttonPanel.add(theBox);
  270. FieldPanel.add(theBoxs);
  271.  
  272.  
  273. this.add(buttonPanel, BorderLayout.WEST);
  274. this.add(FieldPanel, BorderLayout.EAST);
  275. buttonPanel.setPreferredSize(new Dimension(200,480));
  276. FieldPanel.setPreferredSize(new Dimension(200,480));
  277. // Make the drawing area take up the rest of the frame
  278.  
  279. // this.add(new DrawingBoard(), BorderLayout.CENTER);
  280. final DrawingBoard drawPanel = new DrawingBoard();
  281. this.add(drawPanel, BorderLayout.CENTER);
  282. this.getContentPane().setBackground(Color.WHITE);
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289. exportAction.addActionListener(new ActionListener() {
  290.  
  291. public void actionPerformed(ActionEvent arg0) {
  292. BufferedImage image = new BufferedImage(drawPanel.getWidth(),
  293. drawPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
  294. Graphics2D g = image.createGraphics();
  295. drawPanel.paint(g);
  296. g.dispose();
  297.  
  298. JFileChooser fileChooser = new JFileChooser();
  299. File theDirectory = new File("C:/Users/Wenda/Desktop");
  300. fileChooser.setCurrentDirectory(theDirectory);
  301. FileNameExtensionFilter pngFilter = new FileNameExtensionFilter(
  302. "PNG file (*.png)", "png");
  303. fileChooser.addChoosableFileFilter(pngFilter);
  304. fileChooser.setFileFilter(pngFilter);
  305.  
  306. int status = fileChooser.showSaveDialog(Test.this);
  307.  
  308. if (status == JFileChooser.APPROVE_OPTION) {
  309. try {
  310. ImageIO.write(image, "png",
  311. fileChooser.getSelectedFile());
  312. JOptionPane.showMessageDialog(null, "Image saved to "
  313. + fileChooser.getSelectedFile().getName());
  314. } catch (Exception ex) {
  315. ex.printStackTrace();
  316. }
  317. }
  318. }
  319. });
  320. // Show the frame
  321. this.setVisible(true);
  322. }
  323.  
  324. public JButton makeMeButtons(String iconFile, final int actionNum) {
  325. JButton theBut = new JButton();
  326. Icon butIcon = new ImageIcon(iconFile);
  327. theBut.setIcon(butIcon);
  328.  
  329. theBut.addActionListener(new ActionListener() {
  330.  
  331. public void actionPerformed(ActionEvent e) {
  332. currentAction = actionNum;
  333. }
  334. });
  335. return theBut;
  336. }
  337.  
  338. // Spits out buttons based on the image supplied and
  339. // whether a stroke or fill is to be defined
  340.  
  341. public JButton makeMeColorButton(String iconFile, final int actionNum,
  342. final boolean stroke) {
  343. JButton theBut = new JButton();
  344. Icon butIcon = new ImageIcon(iconFile);
  345. theBut.setIcon(butIcon);
  346.  
  347. theBut.addActionListener(new ActionListener() {
  348.  
  349. public void actionPerformed(ActionEvent e) {
  350.  
  351. if (stroke) {
  352.  
  353. // JColorChooser is a popup that lets you pick a color
  354.  
  355. strokeColor = JColorChooser.showDialog(null,
  356. "Pick a Stroke", Color.BLACK);
  357. } else {
  358. }
  359. }
  360. });
  361.  
  362. return theBut;
  363. }
  364. public class DrawingBoard extends JComponent {
  365.  
  366. private static final long serialVersionUID = -4431176095451940075L;
  367.  
  368. // ArrayLists that contain each shape drawn along with
  369. // that shapes stroke and fill
  370. ArrayList<Shape> shapes = new ArrayList<Shape>();
  371. ArrayList<Color> shapeStroke = new ArrayList<Color>();
  372. ArrayList<Integer> count = new ArrayList<Integer>();
  373.  
  374.  
  375. Point drawStart, drawEnd;
  376. // Monitors events on the drawing area of the frame
  377.  
  378. public DrawingBoard() {
  379.  
  380.  
  381. this.addMouseListener(new MouseAdapter() {
  382.  
  383. public void mousePressed(MouseEvent e) {
  384.  
  385. if (currentAction != 1) {
  386.  
  387. // When the mouse is pressed get x & y position
  388.  
  389. drawStart = new Point(e.getX(), e.getY());
  390. drawEnd = drawStart;
  391. repaint();
  392. }
  393. }
  394.  
  395. public void mouseReleased(MouseEvent e) {
  396.  
  397.  
  398. int counts =0;
  399. if (currentAction != 1) {
  400. Shape aShape = null;
  401.  
  402. if (currentAction == 2) {
  403. aShape = drawLine(drawStart.x, drawStart.y,
  404. e.getX(), e.getY());
  405. shapes.add(aShape);
  406.  
  407. int var = shapes.size();
  408. System.out.println("Array index (LINE)"+var);
  409. if(var>1){
  410. for(int i=0;i<var-1;i++){
  411. Shape st = shapes.get(var-1);
  412. Shape s = shapes.get(i);
  413.  
  414. double x1 = (s.getBounds2D().getX());
  415. double y1 = (s.getBounds2D().getY());
  416. double x2 = (s.getBounds2D().getWidth());
  417. double y2 = (s.getBounds2D().getHeight());
  418.  
  419.  
  420.  
  421.  
  422. }
  423.  
  424. }
  425.  
  426.  
  427.  
  428. shapeStroke.add(strokeColor);
  429. drawStart = null;
  430. drawEnd = null;
  431. repaint();
  432. }
  433.  
  434.  
  435. else
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442. if (currentAction == 4) {
  443. boolean collision = false;
  444.  
  445. // Create a new rectangle using x & y coordinates
  446. aShape = drawRectangle(drawStart.x, drawStart.y,
  447. e.getX(), e.getY());
  448. int var = shapes.size();
  449. System.out.println("Array index"+var);
  450. if(var>1){
  451.  
  452. //look for collisions between shapes
  453. for(int i=0;i<var-1;i++){
  454. Shape st = shapes.get(var-1);
  455. Shape s = shapes.get(i);
  456. double x = (s.getBounds2D().getX());
  457. double y = (s.getBounds2D().getY());
  458. double w = (s.getBounds2D().getWidth());
  459. double h = (s.getBounds2D().getHeight());
  460.  
  461.  
  462. if(st.intersects(x+7, y+7, w+7, h+7)){
  463. collision = true;
  464. System.out.println("it collides with "+i);
  465.  
  466. }
  467. else{
  468. collision = false;
  469. System.out.println("NO COLLISION with " +i);
  470.  
  471. }
  472.  
  473. }
  474. }
  475.  
  476. shapeStroke.add(strokeColor);
  477. drawStart = null;
  478. drawEnd = null;
  479. repaint();
  480.  
  481.  
  482. //new EntityForm().setVisible(true);
  483.  
  484. }
  485.  
  486. }
  487.  
  488. }
  489. });
  490.  
  491.  
  492.  
  493.  
  494. this.addMouseListener(new MouseAdapter() {
  495. @Override
  496. public void mouseClicked(MouseEvent me) {
  497. JTextField name = new JTextField(15);
  498. super.mouseClicked(me);
  499. for (Shape s : shapes) {
  500.  
  501. if (s.contains(me.getPoint())) {//check if mouse is clicked within shape
  502. //we can either just print out the object class name
  503. System.out.println("Clicked a "+s.getClass().getName());
  504.  
  505. //or check the shape class we are dealing with using instance of with nested if
  506. if (s instanceof Rectangle2D) {
  507.  
  508. //create table
  509.  
  510. }
  511. else if (s instanceof Ellipse2D) {
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518. }
  519.  
  520. }
  521. }
  522. }
  523. });
  524.  
  525.  
  526.  
  527. this.addMouseMotionListener(new MouseMotionAdapter() {
  528.  
  529. public void mouseDragged(MouseEvent e) {
  530.  
  531. // If this is a brush have shapes go on the screen quickly
  532.  
  533. if (currentAction == 1) {
  534.  
  535. int x = e.getX();
  536. int y = e.getY();
  537.  
  538. Shape aShape = null;
  539. shapes.add(aShape);
  540.  
  541. shapeStroke.add(strokeColor);
  542.  
  543. // Add the transparency value
  544. }
  545.  
  546. // Get the final x & y position after the mouse is dragged
  547.  
  548. drawEnd = new Point(e.getX(), e.getY());
  549. repaint();
  550. }
  551. });
  552. }
  553.  
  554. public void paint(Graphics g) {
  555. // Class used to define the shapes to be drawn
  556.  
  557. graphSettings = (Graphics2D) g;
  558.  
  559. // Antialiasing cleans up the jagged lines and defines rendering
  560. // rules
  561.  
  562. graphSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  563. RenderingHints.VALUE_ANTIALIAS_ON);
  564.  
  565. // Defines the line width of the stroke
  566.  
  567. graphSettings.setStroke(new BasicStroke(4));
  568.  
  569. // Iterators created to cycle through strokes and fills
  570. Iterator<Color> strokeCounter = shapeStroke.iterator();
  571. for (Shape s : shapes) {
  572.  
  573. // Grabs the next stroke from the color arraylist
  574. graphSettings.setPaint(strokeCounter.next());
  575.  
  576. graphSettings.draw(s);
  577.  
  578. }
  579.  
  580. // Guide shape used for drawing
  581. if (drawStart != null && drawEnd != null) {
  582. // Makes the guide shape transparent
  583.  
  584. graphSettings.setComposite(AlphaComposite.getInstance(
  585. AlphaComposite.SRC_OVER, 0.40f));
  586.  
  587. // Make guide shape gray for professional look
  588.  
  589. graphSettings.setPaint(Color.LIGHT_GRAY);
  590.  
  591. Shape aShape = null;
  592.  
  593. if (currentAction == 2) {
  594. aShape = drawLine(drawStart.x, drawStart.y, drawEnd.x,
  595. drawEnd.y);
  596. } else
  597.  
  598. if (currentAction == 4) {
  599.  
  600. aShape = drawRectangle(drawStart.x, drawStart.y, drawEnd.x,
  601. drawEnd.y);
  602.  
  603.  
  604.  
  605. }
  606.  
  607. graphSettings.draw(aShape);
  608. }
  609. }
  610.  
  611.  
  612. private Rectangle2D.Float drawRectangle(int x1, int y1, int x2, int y2) {
  613. // Get the top left hand corner for the shape
  614. // Math.min returns the points closest to 0
  615.  
  616. int x = Math.min(x1, x2);
  617. int y = Math.min(y1, y2);
  618.  
  619. // Gets the difference between the coordinates and
  620.  
  621. int width = Math.abs(x1 - x2);
  622. int height = Math.abs(y1 - y2);
  623.  
  624. return new Rectangle2D.Float(x, y, width, height);
  625.  
  626.  
  627.  
  628. }
  629.  
  630. private Line2D.Float drawLine(int x1, int y1, int x2, int y2) {
  631.  
  632. return new Line2D.Float(x1, y1, x2, y2);
  633. }
  634. }
  635.  
  636.  
  637.  
  638. }
Advertisement
Add Comment
Please, Sign In to add comment