下面是一个用graphics画的里面用到了vector
其实在applet中画图跟在应用程序中画图是一样的import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;public class Drawline extends JFrame {
private Draw dr;
public Drawline()
{
dr = new Draw();
dr.setBackground(Color.white);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(dr);
setSize(300,300);
show();
} public static void main(String args[])
{
Drawline dl = new Drawline();
dl.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 = new Vector();
Line cur; public Draw()
{
setPreferredSize(new Dimension(150,100));
addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(cur == null)
cur = new Line();
cur.x0 = e.getX();
cur.y0 = e.getY();
}
public void mouseReleased(MouseEvent e) {
cur.x1 = e.getX();
cur.y1 = e.getY();
lines.add(cur);
cur = null;
}
}); addMouseMotionListener( new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
cur.x1 = e.getX();
cur.y1 = e.getY();
repaint();
}
});
} 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);
}}
其实在applet中画图跟在应用程序中画图是一样的import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;public class Drawline extends JFrame {
private Draw dr;
public Drawline()
{
dr = new Draw();
dr.setBackground(Color.white);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(dr);
setSize(300,300);
show();
} public static void main(String args[])
{
Drawline dl = new Drawline();
dl.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 = new Vector();
Line cur; public Draw()
{
setPreferredSize(new Dimension(150,100));
addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(cur == null)
cur = new Line();
cur.x0 = e.getX();
cur.y0 = e.getY();
}
public void mouseReleased(MouseEvent e) {
cur.x1 = e.getX();
cur.y1 = e.getY();
lines.add(cur);
cur = null;
}
}); addMouseMotionListener( new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
cur.x1 = e.getX();
cur.y1 = e.getY();
repaint();
}
});
} 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);
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货