public class Frame1 extends JFrame {
Frame2 jFrame2 = new Frame2();
JPanel contentPane;
JPanel jPanel1 = new JPanel();
int xanf, yanf, xalt, yalt;
Color zeichenfarbe;
Stroke nStroke;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JPanel jPanel2 = new JPanel();
JComboBox jComboBox1 = new JComboBox();
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
JSlider jSlider1 = new JSlider();
JLabel jLabel2 = new JLabel();
JButton jButton1 = new JButton();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Initialisierung der Komponenten.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
this.setJMenuBar(jMenuBar1);
setSize(new Dimension(600, 500));
setTitle("Frame-Titel");
jPanel1.setBackground(Color.white);
jPanel1.setBounds(new Rectangle(4, 56, 480, 439));
jPanel1.addMouseListener(new Frame1_jPanel1_mouseAdapter(this));
jPanel1.addMouseMotionListener(new Frame1_jPanel1_mouseMotionAdapter(this));
jMenu1.setText("File");
jMenuItem1.setText("Clear");
jMenuItem1.addActionListener(new Frame1_jMenuItem1_actionAdapter(this));
jPanel2.setBounds(new Rectangle(486, 0, 114, 501));
jPanel2.setLayout(xYLayout1);
jComboBox1.addItem("Line");
jComboBox1.addItem("Rectangle");
jComboBox1.addItem("Oval");
jComboBox1.addItem("Fan");
jLabel1.setText("Tools:");
jSlider1.setValue(1);
jSlider1.setMinimum(1);
jSlider1.setMaximum(10);
jSlider1.setPaintLabels(true);
jSlider1.setPaintTicks(true);
jSlider1.addAncestorListener(new Frame1_jSlider1_ancestorAdapter(this));
jLabel2.setToolTipText("");
jLabel2.setText("Stroke:");
jButton1.setText("Change Line Color");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jMenuBar1.add(jMenu1);
jMenu1.add(jMenuItem1);
contentPane.add(jPanel1, null);
contentPane.add(jPanel2);
jPanel2.add(jComboBox1, new XYConstraints(2, 58, 102, 19));
jPanel2.add(jLabel1, new XYConstraints(2, 40, 100, 14));
jPanel2.add(jSlider1, new XYConstraints( -4, 116, 118, -1));
jPanel2.add(jLabel2, new XYConstraints(6, 100, 100, 12));
jPanel2.add(jButton1, new XYConstraints( -10, 152, 127, -1));
nStroke = new BasicStroke(5);
}
public void jPanel1_mouseDragged(MouseEvent e) {
nStroke = new BasicStroke(jSlider1.getValue());
Graphics2D g = (Graphics2D) jPanel1.getGraphics();
g.setColor(zeichenfarbe);
g.setStroke(nStroke);
if (jComboBox1.getSelectedIndex() == 0) {
g.drawLine(xanf, yanf, e.getX(), e.getY());
xanf = e.getX();
yanf = e.getY();
}
if (jComboBox1.getSelectedIndex() == 1 || jComboBox1.getSelectedIndex() == 2) {
g.setXORMode(Color.white);
if (xanf - xalt < 0 && yanf - yalt < 0) { //vorheriges löschen
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xanf, yanf, xalt - xanf, yalt - yanf);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xanf, yanf, xalt - xanf, yalt - yanf);}
}
if (xanf - xalt > 0 && yanf - yalt < 0) {
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xalt, yanf, xanf - xalt, yalt - yanf);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xalt, yanf, xanf - xalt, yalt - yanf);}
}
if (xanf - xalt < 0 && yanf - yalt > 0) {
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xanf, yalt, xalt - xanf, yanf - yalt);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xanf, yalt, xalt - xanf, yanf - yalt);}
}
if (xanf - xalt > 0 && yanf - yalt > 0) {
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xalt, yalt, xanf - xalt, yanf - yalt);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xalt, yalt, xanf - xalt, yanf - yalt);}
}
xanf = e.getX();
yanf = e.getY();
if (xanf - xalt < 0 && yanf - yalt < 0) { //aktuelles zeigen
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xanf, yanf, xalt - xanf, yalt - yanf);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xanf, yanf, xalt - xanf, yalt - yanf);}
}
if (xanf - xalt > 0 && yanf - yalt < 0) {
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xalt, yanf, xanf - xalt, yalt - yanf);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xalt, yanf, xanf - xalt, yalt - yanf);}
}
if (xanf - xalt < 0 && yanf - yalt > 0) {
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xanf, yalt, xalt - xanf, yanf - yalt);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xanf, yalt, xalt - xanf, yanf - yalt);}
}
if (xanf - xalt > 0 && yanf - yalt > 0) {
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xalt, yalt, xanf - xalt, yanf - yalt);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xalt, yalt, xanf - xalt, yanf - yalt);}
}
if (jComboBox1.getSelectedIndex() == 3) {
g.setXORMode(Color.white);
g.drawLine(xanf, yanf, e.getX(), e.getY());
}
}
}
public void jPanel1_mousePressed(MouseEvent e) {
xanf = e.getX();
yanf = e.getY();
xalt = xanf;
yalt = yanf;
}
public void jPanel1_mouseReleased(MouseEvent e) {
Graphics2D g = (Graphics2D) jPanel1.getGraphics();
g.setPaintMode();
g.setStroke(nStroke);
g.setColor(zeichenfarbe);
if (jComboBox1.getSelectedIndex() == 1 || jComboBox1.getSelectedIndex() == 2) {
xanf = e.getX();
yanf = e.getY();
if (xanf - xalt < 0 && yanf - yalt < 0) { //endgültiges zeihnen
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xanf, yanf, xalt - xanf, yalt - yanf);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xanf, yanf, xalt - xanf, yalt - yanf);}
}
if (xanf - xalt > 0 && yanf - yalt < 0) {
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xalt, yanf, xanf - xalt, yalt - yanf);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xalt, yanf, xanf - xalt, yalt - yanf);}
}
if (xanf - xalt < 0 && yanf - yalt > 0) {
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xanf, yalt, xalt - xanf, yanf - yalt);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xanf, yalt, xalt - xanf, yanf - yalt);}
}
if (xanf - xalt > 0 && yanf - yalt > 0) {
if (jComboBox1.getSelectedIndex() == 1) {g.drawRect(xalt, yalt, xanf - xalt, yanf - yalt);}
if (jComboBox1.getSelectedIndex() == 2) {g.drawOval(xalt, yalt, xanf - xalt, yanf - yalt);}
}
}
}
public void update(Graphics g)
{
super.paint(g);
}