主题:[转帖]请教关于awt绘图问题!!
为什么这个程序画了直线后,如果要想多画几条直线,怎样才能保留原先画的直线?
请指教!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class pet extends Frame
{
int x1,y1,x2,y2;
public pet(String str)
{
super(str);
}
public void put()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
}
});
man m=new man();
add(m);
}
public static void main(String[] args)
{
pet p=new pet("nihao");
p.setSize(600,400);
p.setLocation(180,180);
p.put();
p.setVisible(true);
}
class man extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(x1,y1,x2,y2);
repaint();
}
}
}
请指教!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class pet extends Frame
{
int x1,y1,x2,y2;
public pet(String str)
{
super(str);
}
public void put()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
}
});
man m=new man();
add(m);
}
public static void main(String[] args)
{
pet p=new pet("nihao");
p.setSize(600,400);
p.setLocation(180,180);
p.put();
p.setVisible(true);
}
class man extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(x1,y1,x2,y2);
repaint();
}
}
}