Fu-Schnickens
Grünschnabel
Hallo,
vieleicht weiss einer von euch weiter, wie ich die line löschen und die default ansicht behalten kann.
Gruss
Fu
vieleicht weiss einer von euch weiter, wie ich die line löschen und die default ansicht behalten kann.
Gruss
Fu
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import javax.swing.JPanel;
import javax.swing.JButton;
public class DrawAndDelLine extends JPanel {
private Point a;
private Point b;
private boolean isMouseClicked = false;
private JButton jButton = null;
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new java.awt.Rectangle(52,36,168,24));
jButton.setText("clear last line");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
repaint();
}
});
}
return jButton;
}
public DrawAndDelLine() {
super();
initialize();
}
private void initialize() {
this.setLayout(null);
this.setSize(500, 400);
this.add(getJButton(), null);
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
if (isMouseClicked){
b = e.getPoint();
repaint();
}
else{
a = e.getPoint();
isMouseClicked = true;
}
}
});
}
public void paint(Graphics g) {
Color oColor = this.getBackground();
if (a != null && b != null){
if (isMouseClicked) {
isMouseClicked = false;
g.setColor(Color.BLUE);
g.drawLine(a.x, a.y, b.x, b.y);
}else{
g.setColor(this.getBackground());
g.drawLine(a.x, a.y, b.x, b.y);
}
}
//g.clearRect(0,0,100,100);
//this.setBackground(oColor);
this.paintChildren(g);
//this.paintComponents(g);
}