使用它的所有组件扩展JPanel

时间:2014-01-13 03:26:09

标签: java swing user-interface jpanel paintcomponent

我想调整JPanel及其所有内部组件的大小,我目前唯一的方法是删除所有组件并在缩放后重绘它们,但这种方式效率很低,因为我有很多在某些数据结构中的组件,所以为了修改比例,我需要迭代一个哈希表来修改每个单独的组件......这需要时间和内存,那么还有更简单的方法吗?

这里我做了什么,我想我很接近只需要一些技术/数学表示来解决这个问题

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;

    }

在这里你可以看到原来的"视图"以及圆圈之间的空间,在我缩放之后,圆圈(由于缩放)接近 that's what I got

交叉等级

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

    }

}

0 个答案:

没有答案