java作业——画直线

单纯的直线

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class PaintLineJFrame extends JFrame{
	
	public PaintLineJFrame() {
		// TODO Auto-generated constructor stub
		super("直线");
		this.setBounds(400, 300, 400, 300);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.getContentPane().add(new MyCanvas());
		this.setVisible(true);
	}
	
	class MyCanvas extends Canvas implements MouseListener,MouseMotionListener{

	Point start, end;
	
	public MyCanvas() {
		// TODO Auto-generated constructor stub
		start = end = null;
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}
	
// MouseListener
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		start = new Point(e.getX(), e.getY());
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		end = new Point(e.getX(), e.getY());
		Graphics g = this.getGraphics();
		g.setColor(Color.RED);
		g.drawLine((int)start.getX(), (int)start.getY(), (int)end.getX(), (int)end.getY());

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		end = new Point(e.getX(),e.getY());
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
	public static void main(String[] args) {
		new PaintLineJFrame();
	}



}

可以擦除的直线

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class PaintLineJFrame extends JFrame{
	
	public PaintLineJFrame() {
		// TODO Auto-generated constructor stub
		super("用鼠标画直线");
		this.setBounds(400, 300, 400, 300);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.getContentPane().add(new PaintLineCanvas());
		this.setVisible(true);
	}

	
	class PaintLineCanvas extends Canvas implements MouseListener ,MouseMotionListener{
		
		Point start,end,lastend;
		public PaintLineCanvas() {
			start = end = lastend = null;
			this.addMouseListener(this);
			this.addMouseMotionListener(this);
		}

		public void paint(Graphics g) {
			if (start != null && lastend != null) {
				g.setColor(this.getBackground());
				g.drawLine(start.x, start.y, lastend.x, lastend.y);
				g.setColor(Color.blue);
				g.drawLine(start.x, start.y, end.x, end.y);
			}
		}

		public void update(Graphics g) {
			this.print(g);
		}
		
		
		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			start = new Point(e.getX(), e.getY());
		}


		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			end = new Point(e.getX(),e.getY());
			this.repaint();
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
	   public void mouseDragged(MouseEvent e) {
			// TODO Auto-generated method stub
			lastend = end;
			end = new Point(e.getX(),e.getY());
			this.repaint();
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
	}
	
	public static void main(String[] args) {
		new PaintLineJFrame();
	}
}

你可能感兴趣的:(java)