Java Code Examples for java.awt.Container#getGraphics()

The following examples show how to use java.awt.Container#getGraphics() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextPanel.java    From osp with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Gets the preferred size of this component.
 * @return a dimension object indicating this component's preferred size
 * @see #getMinimumSize
 * @see LayoutManager
 */
public Dimension getPreferredSize() {
  Container c = this.getParent();
  String text = this.text; // local reference for thread safety
  if((c==null)||text.equals("")) { //$NON-NLS-1$
    return ZEROSIZE;
  }
  Graphics2D g2 = (Graphics2D) c.getGraphics();
  if(g2==null) {
    return ZEROSIZE;
  }
  Font oldFont = g2.getFont();
  g2.setFont(font);
  FontMetrics fm = g2.getFontMetrics();
  int boxHeight = fm.getAscent()+4;      // current string height
  int boxWidth = fm.stringWidth(text)+6; // current string width
  g2.setFont(oldFont);
  return new Dimension(boxWidth, boxHeight);
}
 
Example 2
Source File: DGUI.java    From Geom_Kisrhombille with GNU General Public License v3.0 5 votes vote down vote up
public void paint(Graphics g){
if(dg.image!=null){
  Container c=getContentPane();
  int 
    xoff=(c.getWidth()-dg.imagewidth)/2,
    yoff=(c.getHeight()-dg.imageheight)/2;
  t.setToIdentity();
  t.translate(xoff,yoff);
  Graphics2D h=(Graphics2D)c.getGraphics();
  h.drawImage(dg.image,t,null);}}