第二个程序
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;public class xieru extends JFrame
{
private Draw d;
public static void main(String[] args)
{
xieru xr = new xieru();
xr.addWindowListener(
new WindowAdapter (){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public xieru()
{
d = new Draw();
getContentPane().add(d);
show();
setSize(200,200);
}
}class Draw extends JPanel
{
protected int x1,x2,y1,y2;
public Draw()
{
this.setBackground(Color.white);//设置面板为白色。但是程序运行的
//却不是白色的?????????
this.addMouseListener(new MyMouseListener(this));
this.addMouseMotionListener(new MyMouseMotionListener(this));
addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
x1=-1;//跟上一个程序对比这里。。
y1=-1;//
}
public void mouseReleased(MouseEvent e)
{
x1=x2;//跟上一个程序对比这里。。
y1=y2;//
}
}
);
addMouseMotionListener(
new MouseMotionAdapter ()
{
public void mouseDragged(MouseEvent e)
{
x2=e.getX();//跟上一个程序对比这里。。
y2=e.getY();
repaint();//。
}
}
);
}
public void paint(Graphics g)
{
if(x1==-1 || y1==-1)//跟上一个程序对比这里。
{
x1=x2;
y1=y2;
}
g.drawLine(x1,y1,x2,y2);//跟上一个程序对比这里。。
x1=x2;
y1=y2;//
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;public class xieru extends JFrame
{
private Draw d;
public static void main(String[] args)
{
xieru xr = new xieru();
xr.addWindowListener(
new WindowAdapter (){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public xieru()
{
d = new Draw();
getContentPane().add(d);
show();
setSize(200,200);
}
}class Draw extends JPanel
{
protected int x1,x2,y1,y2;
public Draw()
{
this.setBackground(Color.white);//设置面板为白色。但是程序运行的
//却不是白色的?????????
this.addMouseListener(new MyMouseListener(this));
this.addMouseMotionListener(new MyMouseMotionListener(this));
addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
x1=-1;//跟上一个程序对比这里。。
y1=-1;//
}
public void mouseReleased(MouseEvent e)
{
x1=x2;//跟上一个程序对比这里。。
y1=y2;//
}
}
);
addMouseMotionListener(
new MouseMotionAdapter ()
{
public void mouseDragged(MouseEvent e)
{
x2=e.getX();//跟上一个程序对比这里。。
y2=e.getY();
repaint();//。
}
}
);
}
public void paint(Graphics g)
{
if(x1==-1 || y1==-1)//跟上一个程序对比这里。
{
x1=x2;
y1=y2;
}
g.drawLine(x1,y1,x2,y2);//跟上一个程序对比这里。。
x1=x2;
y1=y2;//
}
}
第二个程序
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;public class xieru extends JFrame
{
private Draw d;
public static void main(String[] args)
{
xieru xr = new xieru();
xr.addWindowListener(
new WindowAdapter (){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public xieru()
{
d = new Draw();
getContentPane().add(d);
show();
setSize(200,200);
}
}class Draw extends JPanel
{
protected int x1,x2,y1,y2;
public Draw()
{
this.setBackground(Color.white);//设置面板为白色。但是程序运行的
//却不是白色的?????????
this.addMouseListener(new MyMouseListener(this));
this.addMouseMotionListener(new MyMouseMotionListener(this));
addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
x1=-1;//跟上一个程序对比这里。。
y1=-1;//
}
public void mouseReleased(MouseEvent e)
{
x1=x2;//跟上一个程序对比这里。。
y1=y2;//
}
}
);
addMouseMotionListener(
new MouseMotionAdapter ()
{
public void mouseDragged(MouseEvent e)
{
x2=e.getX();//跟上一个程序对比这里。。
y2=e.getY();
repaint();//。
}
}
);
}
public void paint(Graphics g)
{
if(x1==-1 || y1==-1)//跟上一个程序对比这里。
{
x1=x2;
y1=y2;
}
g.drawLine(x1,y1,x2,y2);//跟上一个程序对比这里。。
x1=x2;
y1=y2;//
}
}
//却不是白色的?????????
在你的public void paint(Graphics g)里加上
super.paint(g);
就可以了。
但是你的逻辑有问题,这样虽然能实现你的这个要求,但是画线的目的就达不到了。
=========================================
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;public class Test
{
public static void main(String[] args)
{
xieru xr = new xieru();
}
}class xieru extends JFrame
{
public xieru()
{
Draw d = new Draw();
getContentPane().add(d);
setSize(200,200);
show();
addWindowListener(
new WindowAdapter ()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}class Draw extends JPanel
{
protected int x1,x2,y1,y2;
public Draw()
{
setBackground(Color.white);//设置面板为白色。但是程序运行的
//却不是白色的?????????this.
// this.addMouseListener(new MyMouseListener(this));
// this.addMouseMotionListener(new MyMouseMotionListener(this));
addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
x1=-1;//跟上一个程序对比这里。。
y1=-1;//
}
public void mouseReleased(MouseEvent e)
{
x1=x2;//跟上一个程序对比这里。。
y1=y2;//
}
}
);
addMouseMotionListener(
new MouseMotionAdapter ()
{
public void mouseDragged(MouseEvent e)
{
x2=e.getX();//跟上一个程序对比这里。。
y2=e.getY();
repaint();//。
}
}
);
}
public void paintComponent(Graphics g)
{
if(x1==-1 || y1==-1)//跟上一个程序对比这里。
{
super.paintComponent(g);
x1=x2;
y1=y2;
}
g.drawLine(x1,y1,x2,y2);//跟上一个程序对比这里。。
x1=x2;
y1=y2;//
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JFrame{
private JButton b1,b2,b3;
private Draw dr;
public Test ()
{
dr =new Draw();
b1 = new JButton("line");
b1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dr.setDraw(e.getActionCommand());
dr.repaint();
}
}
);
b2 = new JButton("oval");
b2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dr.setDraw(e.getActionCommand());
dr.repaint();
}
}
);
b3 = new JButton("xie");
b3.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dr.setDraw(e.getActionCommand());
dr.repaint();
}
}
);
Container c = getContentPane();
c.add(b1,BorderLayout.SOUTH);
c.add(b2,BorderLayout.NORTH);
c.add(b3,BorderLayout.WEST);
c.add(dr,BorderLayout.CENTER);
validate();
show();
setSize(200,200);
repaint();
}
public static void main(String args[])
{
Test t =new Test();
t.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}class Line {
public int x0,y0,x1,y1;
}class Draw extends JPanel{
Vector lines;
Vector ovals ;
Vector write;
private String ss;
protected int x1,x2,y1,y2;
Line cur,curo; public Draw()
{ lines = new Vector();
ovals = new Vector();
write = new Vector();
setPreferredSize(new Dimension(150,100));
setBackground(Color.white);
addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if((cur == null)||(curo==null))
{
cur = new Line();
curo = new Line();
// text = new Line();
}
if(ss=="line")
{
cur.x0 = e.getX();
cur.y0 = e.getY();
}
else if(ss=="oval")
{
curo.x0 = e.getX();
curo.y0 = e.getY();
}
else if(ss=="xie")//跟下一个程序对比这里
{
x1=e.getX();
y1=e.getY();
}//。
}
public void mouseReleased(MouseEvent e) { if(ss=="line")
{
cur.x1 = e.getX();
cur.y1 = e.getY();
lines.add(cur);
cur =null;
}
else if (ss=="oval")
{
curo.x1 = e.getX();
curo.y1 = e.getY();
ovals.add(curo);
curo = null;
}
}
}); addMouseMotionListener( new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
if(ss =="line")
{
cur.x1 = e.getX();
cur.y1 = e.getY();
}
else if(ss=="oval")
{
curo.x1 = e.getX();
curo.y1 = e.getY();
} else if(ss == "xie")//跟下一个程序对比这里
{
Line text = new Line();
text.x0 = x1;
text.y0 = y1;
text.x1 = e.getX();
text.y1 = e.getY();
x1=e.getX();
y1=e.getY();
write.add(text);
}//。。// else if(ss=="xie")//跟下一个程序对比这里
// {
// x2=e.getX();
// y2=e.getY();
// }//
repaint();
}
});
}
public void setDraw(String s)
{
ss=s;
}
public void paintComponent(Graphics g) {
super.paintComponent(g); for(int i = 0;i<lines.size();i++){
Line tmp = (Line)lines.elementAt(i);
g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1);
}
if(cur != null )
g.drawLine(cur.x0,cur.y0,cur.x1,cur.y1);
for(int i = 0;i<ovals.size();i++){
Line tmpo = (Line)ovals.elementAt(i);
g.drawOval(Math.min(tmpo.x0,tmpo.x1),Math.min(tmpo.y0,tmpo.y1),
Math.abs(tmpo.x0-tmpo.x1),Math.abs(tmpo.y0-tmpo.y1));
}
if(curo != null )
g.drawOval(Math.min(curo.x0,curo.x1),Math.min(curo.y0,curo.y1),
Math.abs(curo.x0-curo.x1),Math.abs(curo.y0-curo.y1));
for(int i = 0;i<write.size();i++){
Line tmp = (Line)write.elementAt(i);
g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1);
}
// if(x1==-1 || y1==-1)////跟下一个程序对比这里。。
// {
//
// x1=x2;
// y1=y2;
// }
// g.drawLine(x1,y1,x2,y2);//跟下一个程序对比这里
//
// x1=x2;
// y1=y2;//。
}
}