paintComponent的显示调用??
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
import java.awt.Point;
import java.awt.color.*;
class J_Panel extends JPanel{
Vector<Point> p= new Vector<Point>();
protected J_Panel(){
JOptionPane.showMessageDialog(null, "请点击面板上你画图形的顶点。");
JButton b=new JButton("确定");
Graphics d=this.getGraphics();
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
super.paintComponent(d); //怎么显示调用啊 ,应该是父组件的paint
}
});
add(b);
addMouseListener(new MouseAdapter(){
public void MousePressed(MouseEvent e){
Point temp=new Point(e.getX(),e.getY());
p.add(temp);
}
}
);
}
protected void paintComponent(Graphics g){
int i=0, n=p.size();
Point temp1,temp2;
for(i=0;i<n-1;i++){
temp1=p.get(i);
temp2=p.get(i+1);
g.drawLine(temp1.x, temp1.y, temp2.x, temp2.y);
}
}
}
public class PLine extends JFrame {
public PLine(){
super("图形填充程序");
Container c=getContentPane();
c.setLayout(new BorderLayout());
c.add(new J_Panel(), BorderLayout.CENTER);
//c.setBackground(new Color(Color.green));
}
public static void main(String args[]){
PLine app=new PLine();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(600,400);
app.setVisible(true);
}
}