简体   繁体   中英

How to draw shapes inside JPanel?

Referring to my earlier question :

Based on your valuable answers, I choose Java 2D library to cater my requirements.

I completely read the above said library and have full understanding of dealing with the graphics stuff, like what to draw and how to draw etc.Now i only left with one question that how do i draw my required shapes inside JPanel and after drawing all those shapes how do i place that JPanel inside a JScrollPane?

You will typically draw your stuff inside a subclass of JComponent, say, MyComponent.

  1. Create an instance of MyComponent, MyComponent myc = new MyComponent();
  2. Put that inside a JScrollPane, eg JScrollPane jsp = new JScrollPane(myComponent);
  3. Then add the JScrollpane to the JPanel (exact code depends on the layout manager)

Override paintComponent(Graphics g) method. Cast Graphics to Graphics2D and use drawShape() method passing all your Shapes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM