画线软件一步一步的升级
package cn.hncu.gui.draw;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class PaintLineJFrame1 extends JFrame{
public PaintLineJFrame1(){
super("使用鼠标画直线");
setBounds(200, 200, 500, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add( new PaintLineCanvas1() );
setVisible(true);
}
public static void main(String[] args) {
new PaintLineJFrame1();
}
}
class PaintLineCanvas1 extends Canvas implements MouseListener{
private Point start,end;
//private Line lines[] = new Line[100];
public PaintLineCanvas1() {
this.addMouseListener(this);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.RED);
if (start!=null && end!=null) {
//g.drawLine(100, 100, 200, 200);//写死
g.drawLine(start.x, start.y, end.x, end.y);//从start画到end
}
}
//刷新图形时,会保留残影---消除画面抖动
@Override
public void update(Graphics g) {
//super.update(g);//父类中的该方法会清除掉画布中原有的图形
paint(g);
}
//下面几个是鼠标事件响应方法
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
start = new Point(e.getX(),e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
end = new Point(e.getX(),e.getY());
//lines[i] = new Line(start,end);
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
--------------------------------------------------------------------------------------------------------------
package cn.hncu.gui.draw;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
public class PaintLineJFrame2 extends JFrame{
public PaintLineJFrame2(){
super("使用鼠标画直线");
setBounds(200, 200, 500, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add( new PaintLineCanvas2() );
setVisible(true);
}
public static void main(String[] args) {
new PaintLineJFrame2();
}
}
class PaintLineCanvas2 extends Canvas implements MouseListener, MouseMotionListener{
private Point start,end,lastEnd;
//private Line lines[] = new Line[100];
public PaintLineCanvas2() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
@Override
public void paint(Graphics g) {
if (start!=null && end!=null && lastEnd!=null) {
g.setColor(this.getBackground());
g.drawLine(start.x, start.y, lastEnd.x, lastEnd.y);//擦除
g.setColor(Color.RED);
g.drawLine(start.x, start.y, end.x, end.y);//从start画到end
}
}
//刷新图形时,会保留残影---消除画面抖动
@Override
public void update(Graphics g) {
//super.update(g);//父类中的该方法会清除掉画布中原有的图形
paint(g);
}
//下面几个是鼠标事件响应方法
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
start = new Point(e.getX(),e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
end = new Point(e.getX(),e.getY());
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
//鼠标MotionListener的响应方法
@Override
public void mouseDragged(MouseEvent e) {
// if(end!=null){
// lastEnd = new Point(end.x,end.y);
// }
lastEnd = end;//引用赋值,必捆绑。但是下面的end自己又另开内存,所以两者又分开了,没捆绑
end = new Point(e.getX(),e.getY());
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
-------------------------------------------------------------------------------------------------------------------------------
package cn.hncu.gui.draw;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
public class PaintLineJFrame3 extends JFrame{
public PaintLineJFrame3(){
super("使用鼠标画直线");
setBounds(200, 200, 500, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add( new PaintLineCanvas3() );
setVisible(true);
}
public static void main(String[] args) {
new PaintLineJFrame3();
}
}
class PaintLineCanvas3 extends Canvas implements MouseListener, MouseMotionListener{
private Point start,end;
public PaintLineCanvas3() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
@Override
public void paint(Graphics g) {
if (start!=null && end!=null) {
g.setColor(Color.RED);
g.drawLine(start.x, start.y, end.x, end.y);//从start画到end
}
}
//刷新图形时,会保留残影---消除画面抖动
@Override
public void update(Graphics g) {
//super.update(g);//父类中的该方法会清除掉画布中原有的图形
paint(g);
}
//下面几个是鼠标事件响应方法
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if(end!=null){
end = null;
}
start = new Point(e.getX(),e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
//鼠标MotionListener的响应方法
@Override
public void mouseDragged(MouseEvent e) {
start = end;
end = new Point(e.getX(),e.getY());
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
---------------------------------------------------------------------------------------------------------------------------------------
绘图的演示
package cn.hncu.gui.draw;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FigureDemo extends JFrame {
public FigureDemo() {
super("绘图演示程序");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100, 100, 500, 400);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1));
panel.add(new MyCanvas());
panel.add(new MyJPanel());
getContentPane().add(panel,BorderLayout.CENTER);
JPanel panel2 = new JPanel();
//panel2.add(new JButton("OK"));
MyJButton btn = new MyJButton("OK");
panel2.add(btn);
getContentPane().add(panel2,BorderLayout.NORTH);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "aa");
}
});
setVisible(true);
}
public static void main(String[] args) {
new FigureDemo();
}
}
//法1:用Canvas来画图
class MyCanvas extends Canvas{
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawLine(10, 10, 200, 200);//相对坐标
g.setColor(Color.GREEN);
g.fillOval(100, 100, 50, 50);
// g.setColor(Color.orange);
// g.fillOval(100, 100, 200, 200);
}
}
//法2:用JPanel来画图
class MyJPanel extends JPanel{
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawLine(10, 10, 200, 200);
g.setColor(Color.GREEN);
g.fillOval(100, 100, 50, 50);//相对坐标
}
}
class MyJButton extends JButton{
public MyJButton(String str) {
super(str);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), getHeight());
g.setColor(Color.red);
g.drawLine(0, 0, this.getWidth(), getHeight());
}
}
---------------------------------------------------------------------------------------------------------------------------------