Search code examples
javaswinggraphicspaintcomponent

Java Jpanel Zoom Button (netbeans 7.4 Gui buiilder)


I have a paint component where I draw some shapes but the problem my shapes exceed jPanel Size . because of that I search a solution of zoom jpanel. I don't know if this possible could someone help me I use net beans gui builder


Solution

  • If you want the shapes to size according to your JPanel, make use of the JPanel's getWidth() and getHeight() e.g.

    int x = (int)(getWidth() * 0.1);
    int y = (int)(getHeight() * 0.1);
    int width = (int)(getWidth() * 0.8);
    int height = (int)(getheight() * 0.8):
    g.fillRect(x, y, width, height);
    

    See a full running example here

    You can see here how to customize initialization code for your JPanel in GUI Builder