package study;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;public class DrawShape extends JFrame implements ActionListener {
private static final int line = 0;
private static final int circle = 1;
private int curType;
private Graphics g;
private JPanel mainPanel = new JPanel(new BorderLayout());
private JPanel shapePanel = new ShapePanel();
private JScrollPane scrollPane = new JScrollPane();
private JToolBar controlBar = new JToolBar(JToolBar.VERTICAL);
private JFrame thisFrame; private static final Object[][] CommAndData = {
{"Line", "Circle", "Black", "Red", "Yellow", "Blue"},
{new Integer(line), new Integer(circle), Color.black, Color.red, Color.yellow, Color.blue}
};
private JToggleButton[] shapeArra = {
new JToggleButton("Line"), new JToggleButton("Circle")
};
private JToggleButton[] colorArra = {
new JToggleButton("Black"), new JToggleButton("Red"), new JToggleButton("Yellow"), new JToggleButton("Blue")
}; private Point startPoint = new Point(), endPoint = new Point();
private Color curColor = shapePanel.getForeground();
private Vector actionBuf = new Vector(100);
private boolean flag = true; public DrawShape() {
thisFrame = this;
this.setResizable(true
ButtonGroup groupShape = new ButtonGroup();
ButtonGroup groupColor = new ButtonGroup();
for (int i = 0; i < shapeArra.length; i++) {
groupShape.add(shapeArra[i]);
shapeArra[i].addActionListener(this);
}
for (int i = 0; i < colorArra.length; i++) {
groupColor.add(colorArra[i]);
colorArra[i].addActionListener(this);
} controlBar.setFloatable(false);
for (int i = 0; i < shapeArra.length; i++) {
if (i == 0) {
shapeArra[i].setSelected(true);
curType = ((Integer) CommAndData[1][0]).intValue();
//study tooltip delay time
shapeArra[i].addMouseListener(new MouseAdapter() {
ToolTipManager tip = ToolTipManager.sharedInstance();
int oldIniDelay = 750, oldDisDelay = 4000; public void mouseEntered(MouseEvent e) {
oldIniDelay = tip.getInitialDelay();
oldDisDelay = tip.getDismissDelay();
tip.setInitialDelay(750);
tip.setDismissDelay(50000);
} public void mouseExited(MouseEvent e) {
tip.setInitialDelay(oldIniDelay);
tip.setDismissDelay(oldDisDelay);
}
});
}
shapeArra[i].setToolTipText("tooltip");
controlBar.add(shapeArra[i]);
shapeArra[i].setFocusPainted(false);
shapeArra[i].setActionCommand((String) CommAndData[0][i]);
}
controlBar.addSeparator();
for (int i = 0; i < colorArra.length; i++) {
controlBar.add(colorArra[i]);
colorArra[i].setFocusPainted(false);
colorArra[i].setActionCommand((String) CommAndData[0][i + shapeArra.length]);
} scrollPane.setViewportView(shapePanel); mainPanel.add(controlBar, BorderLayout.WEST);
mainPanel.add(scrollPane, BorderLayout.CENTER); this.getContentPane().add(mainPanel); shapePanel.setBackground(Color.white);
shapePanel.addMouseListener(new MyMouseListener());
shapePanel.addMouseMotionListener(new MyMouseMotion()); this.addWindowListener(new WindowAdapter() {
public void windowDeiconified(WindowEvent e) {
System.out.println("Deiconified");
if (g == null) {
g = shapePanel.getGraphics();
}
// refresh(g);
}
public void windowIconified(WindowEvent e) {
System.out.println("Iconified");
thisFrame.toFront();
}
}); this.setSize(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
} public void actionPerformed(ActionEvent e) {
for (int i = 0; i < CommAndData[0].length; i++) {
if (((String) CommAndData[0][i]).equals(e.getActionCommand())) {
if (Integer.class.isInstance(CommAndData[1][i])) {
curType = ((Integer) CommAndData[1][i]).intValue();
} else if (Color.class.isInstance(CommAndData[1][i])) {
curColor = (Color) CommAndData[1][i];
}
break;
}
}
} private void drawAction(Graphics g, Color color, int type, Point sP, Point eP) {
g.setColor(color);
switch (type) {
case line :
g.drawLine(sP.x, sP.y, eP.x, eP.y);
break;
case circle :
drawCircle(sP, eP, g);
break;
}
} public static Point getCenterPoint(Point p1, Point p2) {
int x = p1.x + (p2.x - p1.x) / 2;
int y = p1.y + (p2.y - p1.y) / 2; return new Point(x, y);
}
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;public class DrawShape extends JFrame implements ActionListener {
private static final int line = 0;
private static final int circle = 1;
private int curType;
private Graphics g;
private JPanel mainPanel = new JPanel(new BorderLayout());
private JPanel shapePanel = new ShapePanel();
private JScrollPane scrollPane = new JScrollPane();
private JToolBar controlBar = new JToolBar(JToolBar.VERTICAL);
private JFrame thisFrame; private static final Object[][] CommAndData = {
{"Line", "Circle", "Black", "Red", "Yellow", "Blue"},
{new Integer(line), new Integer(circle), Color.black, Color.red, Color.yellow, Color.blue}
};
private JToggleButton[] shapeArra = {
new JToggleButton("Line"), new JToggleButton("Circle")
};
private JToggleButton[] colorArra = {
new JToggleButton("Black"), new JToggleButton("Red"), new JToggleButton("Yellow"), new JToggleButton("Blue")
}; private Point startPoint = new Point(), endPoint = new Point();
private Color curColor = shapePanel.getForeground();
private Vector actionBuf = new Vector(100);
private boolean flag = true; public DrawShape() {
thisFrame = this;
this.setResizable(true
ButtonGroup groupShape = new ButtonGroup();
ButtonGroup groupColor = new ButtonGroup();
for (int i = 0; i < shapeArra.length; i++) {
groupShape.add(shapeArra[i]);
shapeArra[i].addActionListener(this);
}
for (int i = 0; i < colorArra.length; i++) {
groupColor.add(colorArra[i]);
colorArra[i].addActionListener(this);
} controlBar.setFloatable(false);
for (int i = 0; i < shapeArra.length; i++) {
if (i == 0) {
shapeArra[i].setSelected(true);
curType = ((Integer) CommAndData[1][0]).intValue();
//study tooltip delay time
shapeArra[i].addMouseListener(new MouseAdapter() {
ToolTipManager tip = ToolTipManager.sharedInstance();
int oldIniDelay = 750, oldDisDelay = 4000; public void mouseEntered(MouseEvent e) {
oldIniDelay = tip.getInitialDelay();
oldDisDelay = tip.getDismissDelay();
tip.setInitialDelay(750);
tip.setDismissDelay(50000);
} public void mouseExited(MouseEvent e) {
tip.setInitialDelay(oldIniDelay);
tip.setDismissDelay(oldDisDelay);
}
});
}
shapeArra[i].setToolTipText("tooltip");
controlBar.add(shapeArra[i]);
shapeArra[i].setFocusPainted(false);
shapeArra[i].setActionCommand((String) CommAndData[0][i]);
}
controlBar.addSeparator();
for (int i = 0; i < colorArra.length; i++) {
controlBar.add(colorArra[i]);
colorArra[i].setFocusPainted(false);
colorArra[i].setActionCommand((String) CommAndData[0][i + shapeArra.length]);
} scrollPane.setViewportView(shapePanel); mainPanel.add(controlBar, BorderLayout.WEST);
mainPanel.add(scrollPane, BorderLayout.CENTER); this.getContentPane().add(mainPanel); shapePanel.setBackground(Color.white);
shapePanel.addMouseListener(new MyMouseListener());
shapePanel.addMouseMotionListener(new MyMouseMotion()); this.addWindowListener(new WindowAdapter() {
public void windowDeiconified(WindowEvent e) {
System.out.println("Deiconified");
if (g == null) {
g = shapePanel.getGraphics();
}
// refresh(g);
}
public void windowIconified(WindowEvent e) {
System.out.println("Iconified");
thisFrame.toFront();
}
}); this.setSize(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
} public void actionPerformed(ActionEvent e) {
for (int i = 0; i < CommAndData[0].length; i++) {
if (((String) CommAndData[0][i]).equals(e.getActionCommand())) {
if (Integer.class.isInstance(CommAndData[1][i])) {
curType = ((Integer) CommAndData[1][i]).intValue();
} else if (Color.class.isInstance(CommAndData[1][i])) {
curColor = (Color) CommAndData[1][i];
}
break;
}
}
} private void drawAction(Graphics g, Color color, int type, Point sP, Point eP) {
g.setColor(color);
switch (type) {
case line :
g.drawLine(sP.x, sP.y, eP.x, eP.y);
break;
case circle :
drawCircle(sP, eP, g);
break;
}
} public static Point getCenterPoint(Point p1, Point p2) {
int x = p1.x + (p2.x - p1.x) / 2;
int y = p1.y + (p2.y - p1.y) / 2; return new Point(x, y);
}
return (int) Math.sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
} public static void drawCircle(Point startPoint, Point endPoint, Graphics g) {
int diameter = getTwoPointsLength(startPoint, endPoint);
int radius = diameter/2;
Point centerPoint = getCenterPoint(startPoint, endPoint);
Point reviseStartPoint =
new Point(centerPoint.x - radius, centerPoint.y - radius); g.drawOval(reviseStartPoint.x, reviseStartPoint.y, diameter, diameter);
} private void refresh(Graphics g) {
if (g == null) return;
java.util.Iterator ite = actionBuf.iterator();
ActionProperty p; while (ite.hasNext()) {
p = (ActionProperty) ite.next();
drawAction(g, p.getColor(), p.getType(), p.getStartPoint(), p.getEndPoint());
}
} class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) {
flag = true;
actionBuf.add(new ActionProperty(
new Color(curColor.getRGB()),
curType, new Point(startPoint), new Point(endPoint)));// java.util.Iterator ite = vector.iterator();
// ActionProperty p;
// int maxX = 0, maxY = 0;
// while (ite.hasNext()) {
// p = (ActionProperty) ite.next();
// if (p.getStartPoint().x > maxX) {
// maxX = p.getStartPoint().x;
// }
// if (p.getEndPoint().x > maxX) {
// maxX = p.getEndPoint().x;
// }
// if (p.getStartPoint().y > maxY) {
// maxY = p.getStartPoint().y;
// }
// if (p.getEndPoint().y > maxY) {
// maxY = p.getEndPoint().y;
// }
// }
// shapeCanvas.setPreferredSize(new Dimension(maxX, maxY));
} public void mouseEntered(MouseEvent e) {
shapePanel.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
} public void mouseExited(MouseEvent e) { }
} class MyMouseMotion extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
if (flag) {
startPoint = e.getPoint();
endPoint = e.getPoint();
flag = false;
(new Monitor()).start();
}
if (g == null) {
g = shapePanel.getGraphics();
}
drawAction(g, shapePanel.getBackground(), curType, startPoint, endPoint);
refresh(g);
g.setColor(curColor);
endPoint = e.getPoint();
drawAction(g, curColor, curType, startPoint, endPoint);
}
} private class ActionProperty {
private Color color;
private int type;
private Point sP, eP; ActionProperty(){};
ActionProperty(Color color, int type, Point startPoint, Point endPoint) {
this.color = color;
this.type = type;
this.sP = startPoint;
this.eP = endPoint;
} void setColor(Color color) {
this.color = color;
} void setType(int type) {
this.type = type;
} void setStartPoint(Point startPoint) {
this.sP = startPoint;
} void setEndPoint(Point endPoint) {
this.eP = endPoint;
} Color getColor() {
return this.color;
} int getType() {
return this.type;
} Point getStartPoint() {
return this.sP;
} Point getEndPoint() {
return this.eP;
}
} private class ShapePanel extends JPanel {
} private class Monitor extends Thread {
public synchronized void run() {
while (true) {
refresh(g);
try {
wait(50);
} catch (InterruptedException ex) {}
}
}
}
public static void main(String[] args) {
new DrawShape();
// System.out.println(getTwoPointsLength(new Point(1, 1), new Point(4, 5)));
}
}