1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.hierarchyviewer.ui; 18 19 import com.android.hierarchyviewer.scene.ViewNode; 20 21 import javax.swing.*; 22 import java.awt.*; 23 24 class CaptureRenderer extends JLabel { 25 private ViewNode node; 26 private boolean showExtras; 27 CaptureRenderer(ImageIcon icon, ViewNode node)28 CaptureRenderer(ImageIcon icon, ViewNode node) { 29 super(icon); 30 this.node = node; 31 setBackground(Color.BLACK); 32 } 33 34 @Override getPreferredSize()35 public Dimension getPreferredSize() { 36 Dimension d = super.getPreferredSize(); 37 38 if (node.hasMargins) { 39 d.width += node.marginLeft + node.marginRight; 40 d.height += node.marginTop + node.marginBottom; 41 } 42 43 return d; 44 } 45 setShowExtras(boolean showExtras)46 public void setShowExtras(boolean showExtras) { 47 this.showExtras = showExtras; 48 repaint(); 49 } 50 51 @Override paintComponent(Graphics g)52 protected void paintComponent(Graphics g) { 53 Icon icon = getIcon(); 54 int width = icon.getIconWidth(); 55 int height = icon.getIconHeight(); 56 57 int x = (getWidth() - width) / 2; 58 int y = (getHeight() - height) / 2; 59 60 icon.paintIcon(this, g, x, y); 61 62 if (showExtras) { 63 g.translate(x, y); 64 g.setXORMode(Color.WHITE); 65 if ((node.paddingBottom | node.paddingLeft | 66 node.paddingTop | node.paddingRight) != 0) { 67 g.setColor(Color.RED); 68 g.drawRect(node.paddingLeft, node.paddingTop, 69 width - node.paddingRight - node.paddingLeft, 70 height - node.paddingBottom - node.paddingTop); 71 } 72 if (node.baseline != -1) { 73 g.setColor(Color.BLUE); 74 g.drawLine(0, node.baseline, width, node.baseline); 75 } 76 if (node.hasMargins && (node.marginLeft | node.marginBottom | 77 node.marginRight | node.marginRight) != 0) { 78 g.setColor(Color.BLACK); 79 g.drawRect(-node.marginLeft, -node.marginTop, 80 node.marginLeft + width + node.marginRight, 81 node.marginTop + height + node.marginBottom); 82 } 83 g.translate(-x, -y); 84 } 85 } 86 } 87