Custom component painting

Advertisement

Custom component painting
Hi
I want to paint graphics as fast as possible,
meaning slow cpu and memory loss. In this
case Im looking over a header panel, one of
the smaaler components. Its painting 1 background
image and another one on top, it also have a
title and description.
Can anything here be done more effective?
public final class HeaderPanel extends JPanel {
   private static final Font fntTitle = UIManager.getFont("Header.titleFont");
   private static final Font fntDescription = UIManager.getFont("Header.descriptionFont");
   private static final Dimension preferredSize = new Dimension(UIManager.getDimension("Header.prefferedSize"));
   private static final Image imgBackground = (Image) UIManager.get("Header.imageBackground");
   private static final Image imgForeground = (Image) UIManager.get("Header.ImageForeground");
   private static final Image imgIcon = (Image) UIManager.get("Header.icon");
   private static final int overlayWidth = imgForeground.getWidth(null);
   private static final int overlayHeight = imgForeground.getHeight(null);
   private static final BufferedImage bufferedImage = new BufferedImage(preferredSize.width, preferredSize.height, BufferedImage.TYPE_INT_RGB);
   private static final BufferedImage bufferedOverlay = new BufferedImage(overlayWidth, overlayHeight, BufferedImage.TYPE_INT_ARGB);
   private final BufferedImage bufferedText = new BufferedImage(overlayWidth, overlayHeight, BufferedImage.TYPE_INT_ARGB);
   static {
      bufferedImage.getGraphics().drawImage(imgBackground, 0, 0, preferredSize.width, preferredSize.height, null);
      bufferedOverlay.getGraphics().drawImage(imgForeground, 0, 0, overlayWidth, overlayHeight, null);
      bufferedOverlay.getGraphics().drawImage(imgIcon, overlayWidth - 10 - imgIcon.getWidth(null), overlayHeight / 2 - imgIcon.getHeight(null) / 2, imgIcon.getWidth(null), imgIcon.getHeight(null), null);
   public HeaderPanel(String title, String description) {
      super(true);
      Graphics2D g2d = (Graphics2D) bufferedText.getGraphics();
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g2d.setColor(Color.BLACK);
      g2d.setFont(fntTitle);
      g2d.drawString(title, 31, 31);
      g2d.setFont(fntDescription);
      g2d.drawString(description, 41, 51);
      setPreferredSize(preferredSize);
   public void paint(Graphics g) {
      g.drawImage(bufferedImage, 0, 0, getWidth(), getHeight(), null);
      g.drawImage(bufferedOverlay, getWidth() - overlayWidth, 0, overlayWidth, overlayHeight, null);
      g.drawImage(bufferedText, 0, 0, overlayWidth, overlayHeight, null);
}
Repaly
Hi
I want to paint graphics as fast as possible,
meaning slow cpu and memory loss. In this
case Im looking over a header panel, one of
the smaaler components. Its painting 1 background
image and another one on top, it also have a
title and description.
Can anything here be done more effective?
public final class HeaderPanel extends JPanel {
   private static final Font fntTitle = UIManager.getFont("Header.titleFont");
   private static final Font fntDescription = UIManager.getFont("Header.descriptionFont");
   private static final Dimension preferredSize = new Dimension(UIManager.getDimension("Header.prefferedSize"));
   private static final Image imgBackground = (Image) UIManager.get("Header.imageBackground");
   private static final Image imgForeground = (Image) UIManager.get("Header.ImageForeground");
   private static final Image imgIcon = (Image) UIManager.get("Header.icon");
   private static final int overlayWidth = imgForeground.getWidth(null);
   private static final int overlayHeight = imgForeground.getHeight(null);
   private static final BufferedImage bufferedImage = new BufferedImage(preferredSize.width, preferredSize.height, BufferedImage.TYPE_INT_RGB);
   private static final BufferedImage bufferedOverlay = new BufferedImage(overlayWidth, overlayHeight, BufferedImage.TYPE_INT_ARGB);
   private final BufferedImage bufferedText = new BufferedImage(overlayWidth, overlayHeight, BufferedImage.TYPE_INT_ARGB);
   static {
      bufferedImage.getGraphics().drawImage(imgBackground, 0, 0, preferredSize.width, preferredSize.height, null);
      bufferedOverlay.getGraphics().drawImage(imgForeground, 0, 0, overlayWidth, overlayHeight, null);
      bufferedOverlay.getGraphics().drawImage(imgIcon, overlayWidth - 10 - imgIcon.getWidth(null), overlayHeight / 2 - imgIcon.getHeight(null) / 2, imgIcon.getWidth(null), imgIcon.getHeight(null), null);
   public HeaderPanel(String title, String description) {
      super(true);
      Graphics2D g2d = (Graphics2D) bufferedText.getGraphics();
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g2d.setColor(Color.BLACK);
      g2d.setFont(fntTitle);
      g2d.drawString(title, 31, 31);
      g2d.setFont(fntDescription);
      g2d.drawString(description, 41, 51);
      setPreferredSize(preferredSize);
   public void paint(Graphics g) {
      g.drawImage(bufferedImage, 0, 0, getWidth(), getHeight(), null);
      g.drawImage(bufferedOverlay, getWidth() - overlayWidth, 0, overlayWidth, overlayHeight, null);
      g.drawImage(bufferedText, 0, 0, overlayWidth, overlayHeight, null);
}
Read More: The other 15 answers