-1

The purpose of the black JPanel is drawing.

  • How can I restrict the drawing to the radius of the circle formed by the lines?

  • Is there a way to save the graphics object state so that more drawing can be added to it as well as adding an undo function?

enter image description here

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • can you add the code for the black JPanel – Kennedy Mar 05 '17 at 14:55
  • I just set its color to black and give it a size. Everything else is in paintComponent() – Radoslav Todorov Mar 05 '17 at 14:58
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) *"How can I restrict the drawing to the radius of the circle formed by the lines?"* [`Graphics.setClip(Shape)`](http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html#setClip-java.awt.Shape-) 3) *"Is there a way.."* Whoah! This is a Q&A site, not a help desk. Each specific question should be self contained, and on its own question thread. I suggest you [edit] this post to remove the 2nd question, or it's in risk of being closed as 'too broad'. – Andrew Thompson Mar 05 '17 at 15:10
  • I have no problems taking risks. – Radoslav Todorov Mar 05 '17 at 15:14
  • *"I have no problems taking risks."* You just took a risk on a down vote. Congrats! You won one. And a tip: Tip: Add @Kennedy (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Mar 05 '17 at 16:44

1 Answers1

0

For your first question,

You would need to read up on Java's Custom Painting http://docs.oracle.com/javase/tutorial/uiswing/painting/

Not to discourage you but this is a tricky process, To answer your question there is a similar post here Java Change shape of JPanel

Community
  • 1
  • 1
Kennedy
  • 547
  • 4
  • 23