|
| 1 | +package actions; |
| 2 | + |
| 3 | +import java.awt.Polygon; |
| 4 | +import java.awt.Rectangle; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileNotFoundException; |
| 7 | +import java.io.FileOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.ArrayList; |
| 10 | + |
| 11 | +import javax.swing.JOptionPane; |
| 12 | + |
| 13 | +import paintcomponents.PaintComponent; |
| 14 | +import ui.PaintPanel; |
| 15 | + |
| 16 | +public class GeneratePolygonSourceJava extends PaintAction { |
| 17 | + |
| 18 | + public GeneratePolygonSourceJava(PaintPanel panel) { |
| 19 | + super(panel); |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public boolean canPerformAction() { |
| 24 | + return panel.getSelectTool().getSelectedComponents().size()>1; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void performAction() { |
| 29 | + try{ |
| 30 | + String str = generatePolygon(panel.getSelectTool().getSelectedComponents()); |
| 31 | + |
| 32 | + |
| 33 | + JOptionPane.showMessageDialog(panel, "Already Saved To Downloads: \n\n" + str); |
| 34 | + |
| 35 | + } catch(Exception e){ |
| 36 | + JOptionPane.showMessageDialog(panel, e); |
| 37 | + |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private String generatePolygon( |
| 42 | + ArrayList<PaintComponent> selectedComponents) { |
| 43 | + |
| 44 | + |
| 45 | + //get width and height for target |
| 46 | + String widthStr = JOptionPane.showInputDialog("Enter generated WIDTH for this polygon"); |
| 47 | + String heightStr = JOptionPane.showInputDialog("Enter the HEIGHT for this polygon"); |
| 48 | + int width = Integer.parseInt(widthStr); |
| 49 | + int height = Integer.parseInt(heightStr); |
| 50 | + String nameOfPolygon = JOptionPane.showInputDialog("Enter the NAME for this polygon"); |
| 51 | + |
| 52 | + StringBuilder builder = new StringBuilder(); |
| 53 | + builder.append("import java.awt.Polygon;\npublic class " + nameOfPolygon + "{\n\n\n"); |
| 54 | + builder.append("\tpublic static Polygon getPolygon(){\n\n\t\tPolygon p = new Polygon();\n\n"); |
| 55 | + |
| 56 | + //get a bounds for current polygon |
| 57 | + Polygon poly = new Polygon(); |
| 58 | + for (PaintComponent paintComponent : selectedComponents) { |
| 59 | + poly.addPoint(paintComponent.getX(), paintComponent .getY()); |
| 60 | + |
| 61 | + } |
| 62 | + Rectangle actual = poly.getBounds(); |
| 63 | + |
| 64 | + double xfactor = (double)actual.getWidth() / width; |
| 65 | + double yfactor = (double)actual.getHeight() / height; |
| 66 | + |
| 67 | + for (PaintComponent paintComponent : selectedComponents) { |
| 68 | + int x = (int)((paintComponent.getX() - actual.getX()) / xfactor); |
| 69 | + int y = (int)((paintComponent.getY() - actual.getY()) / yfactor); |
| 70 | + builder.append("\t\tp.addPoint(" + x + ", " + y + ");\n"); |
| 71 | + } |
| 72 | + |
| 73 | + builder.append("\n\n\t\treturn p;\n\t}\n\n}"); |
| 74 | + |
| 75 | + String home = System.getProperty("user.home"); |
| 76 | + File file = new File(home+"/Downloads/" + nameOfPolygon + ".java"); |
| 77 | + try { |
| 78 | + FileOutputStream fileOutputStream = new FileOutputStream(file); |
| 79 | + fileOutputStream.write(builder.toString().getBytes()); |
| 80 | + fileOutputStream.close(); |
| 81 | + } catch (FileNotFoundException e) { |
| 82 | + // TODO Auto-generated catch block |
| 83 | + e.printStackTrace(); |
| 84 | + } catch (IOException e) { |
| 85 | + // TODO Auto-generated catch block |
| 86 | + e.printStackTrace(); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + return builder.toString(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public String locationString() { |
| 95 | + return "Generate/Java Source File from Selection..."; |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments