JButton in der linken oberen ecke nach drawLine

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

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);
	}
 
danke, ich habs hinbekommen :-)
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JButton;
import javax.swing.JFrame;

	/**
	 * @author Darimont
	 * 
	 * TODO Explain me
	 */
	public class RectanglePaintingExample extends JFrame {
		private Point a;
		private Point b;
		private Point aOld;
		private Point bOld;
		private Color BackColor;
		private JButton jButton = null;
		/**
		 * This method initializes jButton	
		 * 	
		 * @return javax.swing.JButton	
		 */
		private JButton getJButton() {
			if (jButton == null) {
				jButton = new JButton();
				jButton.setBounds(new java.awt.Rectangle(52,36,168,24));
				jButton.setText("clear last line");
				
			}
			return jButton;
		}
		
		public RectanglePaintingExample() {
			super("RectanglePaintingExample");
			this.setLayout(null);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			this.add(getJButton(), null);
			BackColor = this.getBackground();
			setSize(640, 480);
			addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent e) {
					a = e.getPoint();
				}				
				public void mouseReleased(MouseEvent e) {
					a = null;
				}
			});

			addMouseMotionListener(new MouseMotionAdapter() {
				public void mouseDragged(MouseEvent e) {
					Point b = e.getPoint();

					if (a != null && b != null){
						Graphics g = getGraphics();
						//g.clearRect(0, 0, 640, 480);
						if (aOld != null && bOld != null){
							g.setColor(BackColor);
							g.drawLine(aOld.x, aOld.y, bOld.x, bOld.y);
						}	
						g.setColor(Color.BLACK);
						g.drawLine(a.x, a.y, b.x, b.y);
						
						aOld = a;
						bOld = b;

						
					}
				}
			});

			

			setVisible(true);
		}

		public static void main(String[] args) {
			new RectanglePaintingExample();
			
		}
 
Zurück