上面的程序刚开始我指定了width=140,height=232大家把我改成每次能获得panel的大小,传给width,height,动态改变.给大家个程序参考,因为我太笨,不会移植我的程序.象下面的哪个星一样,拖窗口,星也跟着变.
import java.awt.*;
import javax.swing.*;class Star2 extends JComponent
{
public void paint(Graphics g)
{
double x1,x2,y1,y2;
for(double angle=0;angle<Math.PI;angle=angle+Math.PI/16)
{
x1=Math.cos(angle)*radius+radius;
y1=Math.sin(angle)*radius+radius;
x2=Math.cos(angle+Math.PI)*radius+radius;
y2=Math.sin(angle+Math.PI)*radius+radius;
g.drawLine((int)x1,(int)y1,(int)x2,(int)y2);
}
}

public void setBounds(int x,int y,int width,int height)
{
radius=Math.min(width,height)/2;
super.setBounds(x,y,width,height);
repaint();

}

public Dimension getMinimumSize()
{
return new Dimension(2*radius,2*radius);
}

public Dimension getPreferredSize()
{
return new Dimension(2*radius,2*radius);
}
private int radius=100;
}public class Nesting{
public static void main(String[] args)
{
JFrame frame=new JFrame("Nesting containers");
Container c=frame.getContentPane();
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(4,1));
panel.add(new JButton("One"));
panel.add(new JButton("Two"));
panel.add(new JButton("Three"));
panel.add(new JButton("Four"));
c.add(panel,BorderLayout.WEST);
c.add(new Star2(),BorderLayout.CENTER);
frame.pack();
frame.show();
}

}

解决方案 »

  1.   

    改好了,呵呵,累啊!找了半天问题
    关键是这里:
    this.width = width;
    this.height = height;
    super.setBounds(x, y, width, height);
    repaint();下面是改好的:
    import java.awt.*;
    import javax.swing.*;class Drawpolyline extends JComponent {
    public Drawpolyline() {
    } public void paint(Graphics g) {
    x[0] = a;
    y[0] = b + 5 * height / 232;
    x[1] = x[0] + 26 * width / 139;
    y[1] = y[0] + 26 * height / 232;
    x[2] = x[1];
    y[2] = y[1] + 67 * height / 232;
    x[3] = x[2] - 15 * width / 139;
    y[3] = y[2] + 15 * height / 232;
    x[4] = a;
    y[4] = y[0] + 96 * height / 232;
    Polygon p1 = new Polygon(x, y, 5);
    g.drawPolygon(p1);
    g.setColor(Color.red);
    g.fillPolygon(p1); x1[0] = x[0] + 5 * width / 139;
    y1[0] = y[0] - 5 * height / 232;
    x1[1] = x1[0] + 127 * width / 139;
    y1[1] = y1[0];
    x1[2] = x1[1] - 26 * width / 139;
    y1[2] = y1[1] + 26 * height / 232;
    x1[3] = x[0] + 31 * width / 139;
    y1[3] = y1[0] + 26 * height / 232;
    Polygon p2 = new Polygon(x1, y1, 4);
    g.drawPolygon(p2);
    g.setColor(Color.RED);
    g.fillPolygon(p2); x2[0] = x1[2] + 5 * width / 139;
    y2[0] = y1[2] + 5 * height / 232;
    x2[1] = x2[0] + 26 * width / 139;
    y2[1] = y2[0] - 26 * height / 232;
    x2[2] = x2[1];
    y2[2] = y2[1] + 96 * height / 232;
    x2[3] = x2[2] - 8 * width / 139;
    y2[3] = y[2] + 15 * height / 232;
    x2[4] = x2[0];
    y2[4] = y2[0] + 67 * height / 232;
    Polygon p3 = new Polygon(x2, y2, 5);
    g.drawPolygon(p3);
    g.setColor(Color.RED);
    g.fillPolygon(p3); x3[0] = x[1];
    y3[0] = y[2] + 8 * height / 232;
    x3[1] = x3[0] + 86 * width / 139;
    y3[1] = y3[0];
    x3[2] = x3[1] + 12 * width / 139;
    y3[2] = y3[1] + 12 * height / 232;
    x3[3] = x3[2] - 12 * width / 139;
    y3[3] = y3[2] + 12 * height / 232;
    x3[4] = x3[3] - 86 * width / 139;
    y3[4] = y3[3];
    x3[5] = x3[4] - 12 * width / 139;
    y3[5] = y3[3] - 12 * height / 232;
    Polygon p4 = new Polygon(x3, y3, 6);
    g.drawPolygon(p4);
    g.setColor(Color.RED);
    g.fillPolygon(p4); x4[0] = x3[4];
    y4[0] = y3[4] + 8 * height / 232;
    x4[1] = x3[4];
    y4[1] = y4[0] + 67 * height / 232;
    x4[2] = x4[1] - 26 * width / 139;
    y4[2] = y4[1] + 26 * height / 232;
    x4[3] = x4[2];
    y4[3] = y4[2] - 96 * height / 232;
    x4[4] = x4[3] + 12 * width / 139;
    y4[4] = y4[3] - 11 * height / 232;
    Polygon p5 = new Polygon(x4, y4, 5);
    g.drawPolygon(p5);
    g.setColor(Color.RED);
    g.fillPolygon(p5); x5[0] = x4[1] + 5 * width / 139;
    ;
    y5[0] = y4[1] + 5 * height / 232;
    x5[1] = x5[0] + 80 * width / 139;
    y5[1] = y5[0];
    x5[2] = x5[1] + 26 * width / 139;
    y5[2] = y5[1] + 26 * height / 232;
    x5[3] = x5[0] - 26 * width / 139;
    y5[3] = y5[0] + 26 * height / 232;
    Polygon p6 = new Polygon(x5, y5, 4);
    g.drawPolygon(p6);
    g.setColor(Color.RED);
    g.fillPolygon(p6); x6[0] = x2[3];
    y6[0] = y2[3] + 8 * height / 232;
    x6[1] = x6[0] + 10 * width / 139;
    y6[1] = y6[0] + 10 * height / 232;
    x6[2] = x6[1];
    y6[2] = y6[1] + 96 * height / 232;
    x6[3] = x6[2] - 26 * width / 139;
    y6[3] = y6[2] - 26 * height / 232;
    x6[4] = x6[3];
    y6[4] = y3[4] + 8 * height / 232;
    Polygon p7 = new Polygon(x6, y6, 5);
    g.drawPolygon(p7);
    g.setColor(Color.RED);
    g.fillPolygon(p7);
    } public void setBounds(int x, int y, int width, int height) {
    this.width = width;
    this.height = height;
    super.setBounds(x, y, width, height);
    repaint();
    } public Dimension getMinimumSize() {
    return new Dimension(width, height);
    } public Dimension getPreferredSize() {
    return new Dimension(width, height);
    } private int width = 300, height = 350, a = 20, b = 20; int[] x = new int[5];
    int[] y = new int[5];
    int[] x1 = new int[4];
    int[] y1 = new int[4];
    int[] x2 = new int[5];
    int[] y2 = new int[5];
    int[] x3 = new int[6];
    int[] y3 = new int[6];
    int[] x4 = new int[5];
    int[] y4 = new int[5];
    int[] x5 = new int[4];
    int[] y5 = new int[4];
    int[] x6 = new int[5];
    int[] y6 = new int[5]; public static void main(String[] args) {
    JFrame frame = new JFrame("Drawpolyline");
    Container c = frame.getContentPane();
    c.add(new Drawpolyline(), BorderLayout.CENTER);
    frame.pack();
    frame.show();
    }
    }