2008년 1월 14일 월요일

JAVA Font Metrics




public void paintComponent (Graphics g) {

    Graphics2D g2 = (Graphics2D)g;

    String ss = "Pjw";
    int baseline1 = 230;
    int baseline2;
    int x1=30, x2, y1, y2, y3, y4;

    Font ft = new Font ("serif", Font.BOLD, 200);
    FontMetrics fm = g.getFontMetrics (ft);
    g.setFont (ft);

    x2 = x1 + fm.stringWidth (ss);
    y1 = baseline1 - fm.getAscent ();
    y2 = baseline1 + fm.getDescent ();
    y3 = baseline1 + fm.getDescent () + fm.getLeading ();
    baseline2 = baseline1 + fm.getHeight();
    y4 = baseline2 - fm.getAscent ();

    g2.setPaint (new Color(0x000000));
    g.drawString(ss,x1,baseline1);

    g2.setPaint (new Color(0x000080));
    g.drawLine(x1-20,y1,x2+20,y1);
    g2.setPaint (new Color(0x0000FF));
    g.drawLine(x1-20,baseline1,x2+20,baseline1);

    g2.setPaint (new Color(0x008000));
    g.drawLine(x1-20,y2,x2+20,y2);

    g2.setPaint (new Color(0x00FF00));
    g.drawLine(x1-20, y3, x2+20, y3);

    g2.setPaint (new Color(0x606060));
    g.drawString(ss, x1, baseline2);
    g.drawLine(x1-20,baseline2,x2+20,baseline2);

    g2.setPaint (new Color(0xFF0000));
    g.drawLine (x2-20, y4, x2+20, y4);

    g2.setPaint (new Color(0x800080));
    g.drawLine (x1, y1-20, x1, y4+20);
    g2.setPaint (new Color(0x008080));
    g.drawLine (x2, y1-20, x2, y4+20);

}

2008년 1월 3일 목요일

Java 메모리 사용량 알기

Java 에서 사용하고 있는 힙 크기 측정방법

// Runtime 개체를 받아온다.
Runtime rt = Runtime.getRuntime();

// 할당된 힙 메모리
rt.totalMemory()

// 힙 내에서 남아있는 메모리
rt.freeMemory()

// 힙 내에서 사용중인 메모리 구하는 방법
rt.totalMemory()-rt.freeMemory()

// Java 카비지 컬렉터를 실행한다.
rt.gc (); // 또는 System.gc()

* 위 함수는 힙 크기만을 측정할 뿐이며 Java 내부적으로 사용된 메모리는 측정되지 않는다.