3

I want to resize a JPanel and all of it's inner components , the only way I have at the moment is removing all the components and redrawing them after scaling , but this way is so inefficient because I have lot of components which are in some data structure ,so in order to modify the scales, I need to iterate through a hashtable to modify each single component... that takes time and memory , so is there any easier way ?

here what I did , I guess am close just need some technical/mathematical representation to solve this

public void zoomIn() {
        int nW, nH;
        int width, height;
        width = v.getPanel2().getSize().width;
        height = v.getPanel2().getSize().height;

        nW = width + (width / 2);
        nH = height + (height / 2);
        Dimension newDim = new Dimension();
        newDim.width = nW;
        newDim.height = nH;
        v.getPanel2().setPreferredSize(newDim);
        v.getPanel2().removeAll();

        {
            Enumeration e = intersections.keys();
            while (e.hasMoreElements()) {
                Intersection i = (Intersection) intersections.get(e
                        .nextElement());
                addIntersection(new Intersection(i.getNoEdg(), i.getId(),
                        i.getxPos(), i.getyPos(), zf));

            }
        }
        zf *= 2;

    }

here you can see the original "view" and the space between the circles , after I zoom , the circles (due to scaling) gets near that's what I got

Intersection Class

public class Intersection {
    private String id;
    private IntersectionGraph graph;
    private Hashtable<Edge, Double> Edges;
    private int noEdg = 0;
    private double importance = 0.0;
    private int xPos,yPos;

    public Intersection(int noEdges, String id, int xPos, int yPos) {
        this.id = id;
        this.xPos=xPos;
        this.yPos=yPos;
        graph = new IntersectionGraph(xPos, yPos, id);
        setNoEdg(noEdges);
        Edges = new Hashtable<>(noEdg);
        System.out.println("Intersection " + id + " has been created !");
    }



    public Intersection(int noEdges, String id, int xPos, int yPos,double scale) {
        this.id = id;
        this.xPos=xPos;
        this.yPos=yPos;
        graph = new IntersectionGraph(xPos, yPos, id,scale);
        setNoEdg(noEdges);
        Edges = new Hashtable<>(noEdg);
        System.out.println("Intersection " + id + " has been created !");
    }
    public double getImportance() {
        return importance;
    }
}

IntersectionGraph class

class IntersectionGraph extends JComponent {
    private int importance = 100;
    private int x;
    private int y;
    private String id;
    private Graphics2D g2d;
    double scale=1;


    public IntersectionGraph(int x, int y, String id) {
        this.id = id;
        this.x = x;
        this.y = y;
    }

    public IntersectionGraph(int x, int y, String id,double scale) {
        this.id = id;
        this.x = x;
        this.y = y;
        this.scale=scale;
    }

    public void paintComponent(Graphics g) {

        g2d = (Graphics2D) g.create();
        setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
        g2d.setColor(Color.BLACK);

        g2d.scale(scale, scale);
        //setBounds(x, y, importance, importance);
        g2d.drawOval(0, 0, importance, importance);
        //this.setBorder(BorderFactory.createTitledBorder(id));

    }

}
Exorcismus
  • 2,243
  • 1
  • 35
  • 68
  • What is the layout that you are using – Jabir Jan 13 '14 at 03:28
  • Got an example of what you are currently doing? – MadProgrammer Jan 13 '14 at 03:29
  • 1
    FlowLayout , and the components inside are instances of JComponent class , I use panel.add(new instance.paintComponent) to add the components to the layout – Exorcismus Jan 13 '14 at 03:30
  • @MadProgrammer it's a panel in which user can add objects , and he can zoom in/out to see wider area and add more stuff, I need the resizing for the zoom in/out – Exorcismus Jan 13 '14 at 03:32
  • Try taking a look at [this](http://stackoverflow.com/questions/15699916/how-do-i-make-this-panel-zoom-toward-the-middle-of-the-panel/15700496#15700496) and maybe [this](http://stackoverflow.com/questions/12797933/jpanel-resize-on-repaint/12798480#12798480) – MadProgrammer Jan 13 '14 at 03:35
  • Java uses layout managers which help you with the layout of your window. Check them out. http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – ithenoob Jan 13 '14 at 03:50
  • What exactly are you adding to the panel? – MadProgrammer Jan 13 '14 at 04:33
  • different "paintComponent" , it's like when I create new object of (say a person with some specific id) , this person creates a new JComponent and paints it on the panel – Exorcismus Jan 13 '14 at 15:08
  • you need to add 1/2width to your x and 1/2 height to your y while redrawing them this will solve the spacing issue however I can't think of a better and more optimize method to achieve this. – Arsaceus Aug 25 '15 at 15:16

0 Answers0