限制 JPanel 上的绘图区域并保存图形状态

Restricting the drawing area on a JPanel and saving graphics states

黑色JPanel的目的是绘图。

public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    sectors = 12;

    Graphics2D g2d = (Graphics2D) g;    
    g2d.setColor(Color.RED);

    sector = new Line2D.Double(getWidth()/2, 0, getWidth()/2, getHeight());
    //g2d.setClip(new Ellipse2D.Double(getWidth()/2,getHeight()/2, radius, radius));


    //draws the sectors on the screen
    for(int i=0; i<sectors; i++)
    {   
        g2d.draw(sector);
        g2d.rotate(Math.toRadians(30),getWidth()/2,getHeight()/2);
    }

    //draws the doily
    if(dragging)
    {
        for(int i=0; i<sectors; i++)
        {
            g2d.fillOval((int) draw.getX(), (int) draw.getY(),20, 20);
            g2d.rotate(Math.toRadians(30), getWidth()/2, getHeight()/2);
        }

        //saves the current drawing in a stack
        graphics.push(g2d);
    }
}

对于你的第一个问题,

您需要阅读 Java 的自定义绘画 http://docs.oracle.com/javase/tutorial/uiswing/painting/

不是要劝阻你,但这是一个棘手的过程, 要回答你的问题,这里有一个类似的 post Java Change shape of JPanel