I need Help about JAVA GUI #146721
Unanswered
hwangheechan
asked this question in
Programming Help
Replies: 1 comment
-
class DrawingPanel extends JPanel {
private Vector<MyBox> rectList;
private Vector<MyBox> circleList;
private Vector<Vector<MyPoint>> pencilPaths;
private Color fillColor = null;
MyPoint start = null;
MyPoint end = null;
Vector<MyPoint> currentPencilPath = null;
DrawingPanel() {
rectList = new Vector<>();
circleList = new Vector<>();
pencilPaths = new Vector<>();
start = new MyPoint();
end = new MyPoint();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
start.x = e.getX();
start.y = e.getY();
if (EditorEx.this.selectedShape == 1) { // Pencil
currentPencilPath = new Vector<>();
currentPencilPath.add(new MyPoint(e.getX(), e.getY()));
pencilPaths.add(currentPencilPath);
}
}
public void mouseReleased(MouseEvent e) {
end.x = e.getX();
end.y = e.getY();
if (EditorEx.this.selectedShape == 3) { // Rectangle
MyBox rect = new MyBox(new MyPoint(start), new MyPoint(end));
rect.setColor(EditorEx.this.currentColor);
rectList.add(rect);
} else if (EditorEx.this.selectedShape == 4) { // Circle
MyBox circle = new MyBox(new MyPoint(start), new MyPoint(end));
circle.setColor(EditorEx.this.currentColor);
circleList.add(circle);
}
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (EditorEx.this.selectedShape == 1 && currentPencilPath != null) { // Pencil
currentPencilPath.add(new MyPoint(e.getX(), e.getY()));
repaint();
}
}
});
}
public void setFillColor(Color color) {
this.fillColor = color;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw Rectangles
for (MyBox rect : rectList) {
g.setColor(rect.rectColor);
if (fillColor != null) {
g.fillRect(rect.sPos.x, rect.sPos.y, rect.width, rect.height);
} else {
g.drawRect(rect.sPos.x, rect.sPos.y, rect.width, rect.height);
}
}
// Draw Circles
for (MyBox circle : circleList) {
g.setColor(circle.rectColor);
if (fillColor != null) {
g.fillOval(circle.sPos.x, circle.sPos.y, circle.width, circle.height);
} else {
g.drawOval(circle.sPos.x, circle.sPos.y, circle.width, circle.height);
}
}
// Draw Pencil Paths
for (Vector<MyPoint> path : pencilPaths) {
g.setColor(EditorEx.this.currentColor != null ? EditorEx.this.currentColor : Color.BLACK);
for (int i = 0; i < path.size() - 1; i++) {
MyPoint p1 = path.get(i);
MyPoint p2 = path.get(i + 1);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
}
} Explanation of Changes
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Select Topic Area
Question
Body
package SwingTest;
import javax.swing.;
import java.awt.;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.event.*;
import java.util.*;
import SwingTest.MyPoint;
import SwingTest.MyBox;
import java.awt.Color;
public class EditorEx extends JFrame {
}
I want to add pencil, eraser, rotateShape, fillcolor in Shape base on this code. Can you help me how to write code?
Beta Was this translation helpful? Give feedback.
All reactions