import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.geom.Line2D;public class Line {
public static void main(String[] args) {
LineFrame frame = new LineFrame();
frame.setDefaultCloseOperation(3);
frame.show();
}
}class LineFrame extends JFrame{
public LineFrame(){
setTitle("Line");
setResizable(false);
setSize(300,200);
LinePanel panel = new LinePanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
}class LinePanel extends JPanel {
Graphics2D g2;
public LinePanel(){

}
public void paintComponent(Graphics g){
super.paintComponent(g);
g2 = (Graphics2D)g;
g2.draw(line);
}private double leftX = 100.0;
private double topY =500.0 ;
private double W = 50.0;
private double H = 50.0;
private double MovLen = 5.0;
private Line2D line =new Line2D.Double(leftX,topY,W,H);}
如果享用鼠标划就要加上鼠标事件,然后得到位置坐标,重划JPanel.

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public  class painter extends JPanel
    {
    protected Graphics g;
    protected int x1;
    protected int x2;
    protected int y1;
    protected int y2;
    protected int red;
    protected int green;
    protected int blue;

    painter(int X1,int Y1,int X2,int Y2,int RED,int GREEN,int BLUE)
    {
    super(null);
    x1=X1;
    y1=Y1;
    x2=X2;
    y2=Y2;
    red=RED;
    green=GREEN;
    blue=BLUE;
    }
    public void set(int X1,int Y1,int X2,int Y2,int RED,int GREEN,int BLUE)
    {
    x1=X1;
    y1=Y1;
    x2=X2;
    y2=Y2;
    red=RED;
    green=GREEN;
    blue=BLUE;

    }
    public void paint()
    {
    super.paint(g);
    g.setColor(new Color(red,green,blue));
    g.drawLine(x1,y1,x2,y2);
    }
    }
    我自己写了这个painter类,但把这个类添加到CONTAINER的时候却系统报错说window不能添加到container里边,麻烦指点下是什么问题呢.我不是继承了JPanel么,怎么变成window了?