Skip to content

Commit 42d7364

Browse files
committed
Initial Commit
0 parents  commit 42d7364

File tree

13 files changed

+596
-0
lines changed

13 files changed

+596
-0
lines changed

.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.class

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JavaSketchPad</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package paintcomponents;
2+
import java.awt.Graphics;
3+
import java.awt.Rectangle;
4+
5+
public abstract class PaintComponent {
6+
7+
private int x;
8+
private int y;
9+
private boolean selected;
10+
11+
/**
12+
* @return the x
13+
*/
14+
public int getX() {
15+
return x;
16+
}
17+
18+
/**
19+
* @param x the x to set
20+
*/
21+
public void setX(int x) {
22+
this.x = x;
23+
}
24+
25+
/**
26+
* @return the y
27+
*/
28+
public int getY() {
29+
return y;
30+
}
31+
32+
/**
33+
* @param y the y to set
34+
*/
35+
public void setY(int y) {
36+
this.y = y;
37+
}
38+
39+
public PaintComponent(int x, int y) {
40+
super();
41+
this.x = x;
42+
this.y = y;
43+
}
44+
45+
public void paint(Graphics g){
46+
if(selected){
47+
paintSelected(g);
48+
} else {
49+
paintNotSelected(g);
50+
}
51+
52+
}
53+
54+
protected abstract void paintNotSelected(Graphics g) ;
55+
56+
57+
protected abstract void paintSelected(Graphics g) ;
58+
59+
60+
public void select(){
61+
selected = true;
62+
}
63+
public void deselect(){
64+
selected = false;
65+
}
66+
67+
public abstract Rectangle getBounds();
68+
69+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package paintcomponents;
2+
import java.awt.Color;
3+
import java.awt.Graphics;
4+
import java.awt.Rectangle;
5+
6+
import settings.Defaults;
7+
8+
public class SimplePoint extends PaintComponent {
9+
10+
11+
private static final int DEFUALT_RADIUS = 10;
12+
private int radius;
13+
private Color color;
14+
private Color selectedColor;
15+
16+
public SimplePoint(int x, int y){
17+
this(x, y, Defaults.sharedDefaults().defaultSimplePointSize(),
18+
Defaults.sharedDefaults().defaultSimplePointColor(),
19+
Defaults.sharedDefaults().defaultSimplePointSelectedColor());
20+
}
21+
22+
public SimplePoint(int x, int y, int radius, Color color, Color selectedColor) {
23+
super(x, y);
24+
this.radius = radius;
25+
this.color = color;
26+
this.selectedColor = selectedColor;
27+
}
28+
29+
public SimplePoint(SimplePoint p) {
30+
this(p.getX(), p.getY(), p.radius, p.color, p.selectedColor);
31+
}
32+
33+
34+
@Override
35+
protected void paintNotSelected(Graphics g) {
36+
g.setColor(color);
37+
g.fillOval(this.getX()- this.radius/2, this.getY() - this.radius/2, radius, radius);
38+
}
39+
40+
@Override
41+
protected void paintSelected(Graphics g) {
42+
g.setColor(selectedColor);
43+
g.fillOval(this.getX()- this.radius/2, this.getY() - this.radius/2, radius, radius);
44+
45+
}
46+
47+
@Override
48+
public Rectangle getBounds() {
49+
return new Rectangle(this.getX()- this.radius/2, this.getY() - this.radius/2, radius, radius);
50+
}
51+
52+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package painttools.toolbar;
2+
import java.awt.Button;
3+
import java.awt.Component;
4+
import java.awt.Graphics;
5+
import java.awt.event.ActionEvent;
6+
import java.awt.event.ActionListener;
7+
import java.util.ArrayList;
8+
9+
import javax.swing.Icon;
10+
import javax.swing.JButton;
11+
import javax.swing.JPanel;
12+
import javax.tools.Tool;
13+
14+
import painttools.tools.DotTool;
15+
import painttools.tools.PaintTool;
16+
17+
18+
public class ToolBar extends JPanel {
19+
20+
21+
public ArrayList<ToolBarListener> listeners;
22+
23+
public ToolBar(){
24+
listeners = new ArrayList<>();
25+
addTool(new DotTool());
26+
}
27+
28+
private void addTool(PaintTool tool) {
29+
JButton button = tool.getButton();
30+
button.addActionListener(new ActionListener() {
31+
32+
@Override
33+
public void actionPerformed(ActionEvent e) {
34+
select(tool);
35+
}
36+
});
37+
add(button);
38+
}
39+
40+
public void addToolBarListener(ToolBarListener listener){
41+
listeners.add(listener);
42+
}
43+
44+
private void select(PaintTool tool){
45+
for (ToolBarListener toolBarListener : listeners) {
46+
toolBarListener.toolSelected(tool);
47+
}
48+
}
49+
50+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package painttools.toolbar;
2+
3+
import painttools.tools.PaintTool;
4+
5+
public interface ToolBarListener {
6+
7+
public abstract void toolSelected(PaintTool tool);
8+
}

src/painttools/tools/DotTool.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package painttools.tools;
2+
import java.awt.Component;
3+
import java.awt.Graphics;
4+
import java.awt.Point;
5+
import java.awt.event.MouseEvent;
6+
7+
import javax.swing.Icon;
8+
import javax.swing.JButton;
9+
10+
import paintcomponents.SimplePoint;
11+
import ui.PaintPanel;
12+
13+
public class DotTool extends PaintTool<SimplePoint> {
14+
15+
SimplePoint p;
16+
private PaintPanel panel;
17+
18+
@Override
19+
public void start(PaintPanel panel) {
20+
this.panel = panel;
21+
22+
p = new SimplePoint(0, 0);
23+
panel.setTempComponent(p);
24+
25+
}
26+
27+
28+
@Override
29+
public SimplePoint paintedComponent() {
30+
return p;
31+
}
32+
33+
@Override
34+
public JButton getButton() {
35+
JButton button = super.getButton();
36+
button.setIcon(new Icon() {
37+
38+
@Override
39+
public void paintIcon(Component c, Graphics g, int x, int y) {
40+
g.fillOval(10, 10, 20, 20);
41+
42+
}
43+
44+
@Override
45+
public int getIconWidth() {
46+
return 40;
47+
}
48+
49+
@Override
50+
public int getIconHeight() {
51+
// TODO Auto-generated method stub
52+
return 40;
53+
}
54+
});
55+
return button;
56+
}
57+
58+
@Override
59+
public void mouseClicked(MouseEvent e) {
60+
panel.addPaintComponent(new SimplePoint(p));
61+
}
62+
63+
@Override
64+
public void mousePressed(MouseEvent e) {
65+
// TODO Auto-generated method stub
66+
67+
}
68+
69+
@Override
70+
public void mouseReleased(MouseEvent e) {
71+
// TODO Auto-generated method stub
72+
73+
}
74+
75+
@Override
76+
public void mouseEntered(MouseEvent e) {
77+
// TODO Auto-generated method stub
78+
79+
}
80+
81+
@Override
82+
public void mouseExited(MouseEvent e) {
83+
// TODO Auto-generated method stub
84+
85+
}
86+
87+
@Override
88+
public void mouseDragged(MouseEvent e) {
89+
// TODO Auto-generated method stub
90+
91+
}
92+
93+
@Override
94+
public void mouseMoved(MouseEvent e) {
95+
// TODO Auto-generated method stub
96+
p.setX(e.getX());
97+
p.setY(e.getY());
98+
panel.repaint();
99+
}
100+
101+
102+
103+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package painttools.tools;
2+
import java.awt.Panel;
3+
import java.awt.event.MouseListener;
4+
import java.awt.event.MouseMotionListener;
5+
6+
import javax.swing.JButton;
7+
import javax.swing.text.html.HTMLEditorKit.InsertHTMLTextAction;
8+
import javax.xml.stream.events.StartDocument;
9+
10+
import paintcomponents.PaintComponent;
11+
import ui.PaintPanel;
12+
13+
public abstract class PaintTool<T extends PaintComponent> implements MouseListener, MouseMotionListener {
14+
15+
16+
//if this tool has finished
17+
private boolean done;
18+
19+
/**
20+
* Set the start condition of this paint tool
21+
*/
22+
public abstract void start(PaintPanel panel);
23+
24+
25+
/**
26+
* the completed component
27+
* @return
28+
*/
29+
public abstract T paintedComponent();
30+
31+
/**
32+
* @return whether this component has done painting
33+
*/
34+
public boolean isDone() {
35+
return done;
36+
}
37+
38+
/**
39+
* sets the done condition
40+
* @param
41+
*/
42+
public void setDone(boolean done) {
43+
this.done = done;
44+
}
45+
46+
public JButton getButton() {
47+
return new JButton(this.getClass().getName());
48+
}
49+
}

0 commit comments

Comments
 (0)