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
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));
}
}