Skip to content

Commit 87988b2

Browse files
committed
Complete Select Tool and Polygon Generator
1 parent c8c5297 commit 87988b2

File tree

11 files changed

+472
-27
lines changed

11 files changed

+472
-27
lines changed

src/actions/ActionsMenuBar.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package actions;
2+
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
import javax.swing.JMenu;
7+
import javax.swing.JMenuBar;
8+
import javax.swing.JMenuItem;
9+
10+
import painttools.tools.SelectionToolListener;
11+
import ui.PaintPanel;
12+
13+
public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{
14+
15+
public ActionsMenuBar(PaintPanel panel){
16+
addAction(new GeneratePolygonSourceJava(panel));
17+
18+
}
19+
20+
private void addAction(PaintAction action) {
21+
String[] strings = action.locationString().split("/");
22+
JMenu insertionMenu = null;
23+
//look for existing j menus
24+
for (int i = 0; i < getMenuCount();i++) {
25+
JMenu menu = getMenu(i);
26+
if(menu.getText().equals(strings[0])){
27+
insertionMenu = menu;
28+
break;
29+
}
30+
}
31+
//create a new if not found
32+
if(insertionMenu == null){
33+
insertionMenu = new JMenu(strings[0]);
34+
this.add(insertionMenu);
35+
}
36+
37+
//assume 2 level depth
38+
PaintActionMenuItem item = new PaintActionMenuItem(action);
39+
item.setEnabled(action.canPerformAction());
40+
item.setText(strings[1]);
41+
item.addActionListener(new ActionListener() {
42+
43+
@Override
44+
public void actionPerformed(ActionEvent e) {
45+
action.performAction();
46+
47+
}
48+
});
49+
50+
insertionMenu.add(item);
51+
52+
}
53+
54+
@Override
55+
public void selectionChanged() {
56+
for (int i = 0; i < getMenuCount(); i++) {
57+
JMenu menu = getMenu(i);
58+
for(int j = 0; j < menu.getItemCount(); j++){
59+
PaintActionMenuItem item = (PaintActionMenuItem) menu.getItem(i);
60+
item.setEnabled(item.getAssociatedAction().canPerformAction());
61+
}
62+
63+
}
64+
65+
}
66+
67+
68+
69+
70+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

src/actions/PaintAction.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package actions;
2+
3+
import ui.PaintPanel;
4+
5+
public abstract class PaintAction {
6+
7+
protected PaintPanel panel;
8+
9+
public PaintAction(PaintPanel panel){
10+
this.panel = panel;
11+
}
12+
13+
public abstract boolean canPerformAction();
14+
public abstract void performAction();
15+
public abstract String locationString();
16+
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package actions;
2+
3+
import javax.swing.JMenuItem;
4+
5+
public class PaintActionMenuItem extends JMenuItem{
6+
7+
8+
private PaintAction associatedAction;
9+
10+
public PaintActionMenuItem(PaintAction associatedAction) {
11+
this.setAssociatedAction(associatedAction);
12+
13+
}
14+
15+
public PaintAction getAssociatedAction() {
16+
return associatedAction;
17+
}
18+
19+
public void setAssociatedAction(PaintAction associatedAction) {
20+
this.associatedAction = associatedAction;
21+
}
22+
}

src/icons/LeftArrow.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package icons;
2+
3+
import java.awt.Color;
4+
import java.awt.Component;
5+
import java.awt.Graphics;
6+
import java.awt.Polygon;
7+
import java.awt.Rectangle;
8+
9+
import javax.swing.Icon;
10+
11+
import paintcomponents.PaintComponent;
12+
public class LeftArrow{
13+
14+
15+
/**
16+
* Constructs a PaintComponent from a specific polygon
17+
* @param poly
18+
* @param color
19+
* @return
20+
*/
21+
public static PaintComponent paintComponentFromPolygon(Polygon poly, Color color)
22+
{
23+
return new PaintComponent(0, 0) {
24+
25+
@Override
26+
protected void paintSelected(Graphics g) {
27+
paintNotSelected(g);
28+
}
29+
30+
@Override
31+
protected void paintNotSelected(Graphics g) {
32+
g.setColor(color);
33+
34+
//translates y to the correct position
35+
int dx = (int) (getX() - poly.getBounds().getX());
36+
int dy = (int) (getY() - poly.getBounds().getY());
37+
//center arrow, by shifting y up by half of the bounds.height
38+
dy -= poly.getBounds().getHeight() / 2;
39+
//shift the arrow to the right a little bit
40+
dx += poly.getBounds().getWidth() / 5;
41+
poly.translate(dx, dy);
42+
g.fillPolygon(poly);
43+
}
44+
45+
@Override
46+
public Rectangle getBounds() {
47+
return poly.getBounds();
48+
}
49+
};
50+
51+
}
52+
53+
/**
54+
* Generate an Icon from polygon
55+
* it will fill polygon with 10 default offset
56+
* @param poly the polygon object
57+
* @param color the color of the icon
58+
* @return
59+
*/
60+
public static Icon iconFromPolygon(Polygon poly, Color color){
61+
return new Icon() {
62+
63+
@Override
64+
public void paintIcon(Component c, Graphics g, int x, int y) {
65+
//translates y to the correct position
66+
int dx = (int) (x - poly.getBounds().getX());
67+
int dy = (int) (y - poly.getBounds().getY());
68+
//center arrow, by shifting y up by half of the bounds.height
69+
dy += poly.getBounds().getHeight() / 2;
70+
//shift the arrow to the right a little bit
71+
dx += poly.getBounds().getWidth() / 4;
72+
g.setColor(color);
73+
g.fillPolygon(poly);
74+
75+
}
76+
77+
@Override
78+
public int getIconWidth() {
79+
return (int) poly.getBounds().getWidth();
80+
}
81+
82+
@Override
83+
public int getIconHeight() {
84+
return (int) poly.getBounds().getHeight();
85+
}
86+
};
87+
88+
}
89+
90+
91+
public static Polygon getPolygon(){
92+
93+
Polygon p = new Polygon();
94+
95+
p.addPoint(8, 0);
96+
p.addPoint(0, 9);
97+
p.addPoint(8, 20);
98+
p.addPoint(8, 17);
99+
p.addPoint(8, 14);
100+
p.addPoint(40, 15);
101+
p.addPoint(40, 8);
102+
p.addPoint(8, 7);
103+
p.addPoint(8, 1);
104+
105+
106+
return p;
107+
}
108+
109+
}

src/painttools/toolbar/ToolBar.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
public class ToolBar extends JPanel {
2121

2222
public ArrayList<ToolBarListener> listeners;
23+
private SelectTool selectTool;
2324

2425
/**
2526
* Creates a default toolbar and add necessary tools
@@ -30,8 +31,9 @@ public ToolBar() {
3031
//sets the box layout
3132
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
3233

34+
selectTool = new SelectTool();
3335
addTool(new DotTool());
34-
addTool(new SelectTool());
36+
addTool(selectTool);
3537
}
3638

3739
/**
@@ -70,4 +72,10 @@ private void select(PaintTool tool) {
7072
}
7173
}
7274

75+
public SelectTool getSelectTool() {
76+
return selectTool;
77+
}
78+
79+
80+
7381
}

0 commit comments

Comments
 (0)