private class MouseMotionHandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
currentX = event.getX();
currentY = event.getY();
repaint();
}
public void mouseDragged(MouseEvent event)
{
int tempX, tempY;
ProgrammZustand temp;
tempX = event.getX();
tempY = event.getY();
currentX = tempX;
currentY = tempY;
int isLeft = event.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK;
int isRight = event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK;
//wozu bin ich da :-)
// wenn left mouse gehaltenden gedrueckt und bewegt
// und wenn paintEnabled , dann soll ein Figur gezeichnet werden
if((isLeft != 0) && paintEnabled)
{
/*
for(int i = 0; i<polygon.size(); i++)
{
temp = (ProgrammZustand)polygon.get(i);
if(temp.isContain(tempX, tempY)) //&& temp.isStored)
{
temp = (ProgrammZustand)polygon.remove(i);
temp.setEnd(tempX, tempY);
polygon.add(temp);
}
}
*/
int polygonSize = polygon.size();
temp = (ProgrammZustand)polygon.remove(polygonSize-1);
//if(temp.isStored)
temp.setEnd(tempX, tempY);
polygon.add(temp);
repaint();
}
// und wenn left mouse gehaltenden gedrueckt und bewegt
// und move_or_eraseButtonActived dann soll die selected Figur
// bewegt werden
else if((isLeft != 0) && move_or_eraseButtonActived)
{
for(int i=0; i<polygon.size(); i++)
{
temp = (ProgrammZustand)polygon.get(i);
if(temp.isContain(tempX, tempY))
{
// temp.isSelected = true;
temp = (ProgrammZustand)polygon.remove(i);
int x1 = (int)temp.getStart().getX() + (tempX - moveX);
int y1 = (int)temp.getStart().getY() + (tempY - moveY);
int x2 = (int)temp.getEnd().getX() + (tempX - moveX);
int y2 = (int)temp.getEnd().getY() + (tempY - moveY);
temp.setStart(x1, y1);
temp.setEnd(x2, y2);
moveX = tempX;
moveY = tempY;
polygon.add(polygon.size(), temp);
repaint();
}
}
repaint();
}
repaint();
}
}
class MyKeyListener extends KeyAdapter
{
public void keyPressed(KeyEvent ke)
{
System.out.println(\"test\");
ProgrammZustand temp;
if(textButtonActived)
{
int polygonSize = polygon.size();
temp = (ProgrammZustand)polygon.remove(polygonSize-1);
if(ke.getKeyCode() == KeyEvent.VK_ENTER)
textButtonActived = false; // Texteingabe beenden
else
{
if(ke.getKeyCode() != KeyEvent.VK_BACK_SPACE)
temp.text += ke.getKeyChar(); // Taste an String h鋘gen
else
if (temp.text.length()>0) //Backspace
temp.text = temp.text.substring(0,temp.text.length()-1); //letzten Charakter l鰏chen
}
polygon.add(temp);
repaint();
}
}
}
class KeyHandler extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
ProgrammZustand temp;
if(textButtonActived)
{
int polygonSize = polygon.size();
temp = (ProgrammZustand)polygon.remove(polygonSize-1);
if(e.getKeyCode() == KeyEvent.VK_ENTER)
textButtonActived = false; // Texteingabe beenden
else
{
if(e.getKeyCode() != KeyEvent.VK_BACK_SPACE)
temp.text += e.getKeyChar(); // Taste an String h鋘gen
else
if (temp.text.length()>0) //Backspace
temp.text = temp.text.substring(0,temp.text.length()-1); //letzten Charakter l鰏chen
}
polygon.add(temp);
repaint();
}
}
}
}
//------------ Class PaintFrame ------------
class PaintFrame extends JFrame
{
public PaintFrame()
{
setTitle(\"Paint Board\");
setBounds(100, 100, 800, 400);
JMenuBar menuBar = makeMenuBar();
setJMenuBar(menuBar);
PaintPanel panel = new PaintPanel();
getContentPane().add(panel);
}
private JMenuBar makeMenuBar()
{
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu(\"File\");
fileMenu.setMnemonic('F');
menuBar.add(fileMenu);
JMenuItem newItem = new JMenuItem(\"New\", 'N');
newItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
newItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
PaintPanel.polygon.clear();
PaintPanel.currentBackColor = Color.WHITE;
PaintPanel.currentColor = Color.BLACK;
PaintPanel.currentFill = false;
PaintPanel.paintEnabled = false;
PaintPanel.move_or_eraseButtonActived = true;
}
});
fileMenu.add(newItem);
JMenuItem openItem = new JMenuItem(\"Open\", 'O');
openItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
openItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(\".\"));
chooser.showOpenDialog(PaintFrame.this);
}
});
fileMenu.add(openItem);
JMenuItem saveItem = new JMenuItem(\"Save\", 'O');
saveItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
saveItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(\".\"));
chooser.showSaveDialog(PaintFrame.this);
}
});
fileMenu.add(saveItem);
fileMenu.addSeparator();
JMenuItem exitItem = new JMenuItem(\"Exit\", 'x');
exitItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
exitItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
fileMenu.add(exitItem);
JMenu editMenu = new JMenu(\"Edit\");
editMenu.setMnemonic('E');
menuBar.add(editMenu);
JMenuItem undoItem = new JMenuItem(\"Undo\", new ImageIcon(\"undo.gif\"));
undoItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK));
editMenu.add(undoItem);
JMenuItem cutItem = new JMenuItem(\"Cut\", new ImageIcon(\"cut.gif\"));
cutItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
editMenu.add(cutItem);
JMenuItem copyItem = new JMenuItem(\"Copy\", new ImageIcon(\"copy.gif\"));
copyItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK));
editMenu.add(cutItem);
JMenuItem pasteItem = new JMenuItem(\"Paste\", new ImageIcon(\"paste.gif\"));
pasteItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK));
editMenu.add(pasteItem);
JMenu setMenu = new JMenu(\"Settings\");
setMenu.setMnemonic('S');
menuBar.add(setMenu);
JMenuItem backItem = new JMenuItem(\"Background\", 'B');
backItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Color defaultColor = Color.BLACK;
Color selected =
JColorChooser.showDialog(PaintFrame.this, \"Select Color\", defaultColor);
PaintPanel.currentBackColor = selected;
}
});
setMenu.add(backItem);
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem draw = new JRadioButtonMenuItem(\"Draw\", true);
draw.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
PaintPanel.currentFill = false;
}
});
JRadioButtonMenuItem fill = new JRadioButtonMenuItem(\"Fill\", false);
fill.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
PaintPanel.currentFill = true;
}
});
group.add(draw);
group.add(fill);
JMenu style = new JMenu(\"Style\");
style.add(draw);
style.add(fill);
setMenu.add(style);
JMenu helpMenu = new JMenu(\"Help\");
helpMenu.setMnemonic('H');
menuBar.add(helpMenu);
JMenuItem helpItem = new JMenuItem(\"Help\", 'H');
helpItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));
helpMenu.add(helpItem);
JMenuItem aboutItem = new JMenuItem(\"About\", 'A');
aboutItem.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
aboutItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(
PaintFrame.this, \" JPaint Version 0.1\n\n\" +
\"Author : jellen chang \nE-mail : jellen@126.com\" +
\"\n Author : kaihua wu \nE-mail: kaihua1@yahoo.com\");
}
});
helpMenu.add(aboutItem);
return menuBar;
}
}
//------------ Public Class ------------
public class Zeichnen
{
public static void main(String[] args) throws Exception
{
PaintFrame frame = new PaintFrame();
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}