Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/PerformanceMonitor.java
41154 views
/*1*2* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d;323334import java.awt.BorderLayout;35import java.awt.Color;36import java.awt.Component;37import java.awt.Dimension;38import java.awt.Font;39import java.awt.FontMetrics;40import java.awt.Graphics;41import java.awt.Graphics2D;42import java.awt.event.MouseAdapter;43import java.awt.event.MouseEvent;44import java.awt.image.BufferedImage;45import javax.swing.JPanel;46import javax.swing.border.EtchedBorder;47import javax.swing.border.TitledBorder;484950/**51* Displays the time for a Surface to paint. Displays the number52* of frames per second on animated demos. Up to four surfaces fit53* in the display area.54*/55@SuppressWarnings("serial")56public class PerformanceMonitor extends JPanel {5758Surface surf;5960public PerformanceMonitor() {61setLayout(new BorderLayout());62setBorder(new TitledBorder(new EtchedBorder(), "Performance"));63add(surf = new Surface());64}656667public class Surface extends JPanel implements Runnable {6869public Thread thread;70private BufferedImage bimg;71private Font font = new Font(Font.SERIF, Font.PLAIN, 12);72private JPanel panel;7374public Surface() {75setBackground(Color.black);76addMouseListener(new MouseAdapter() {7778@Override79public void mouseClicked(MouseEvent e) {80if (thread == null) {81start();82} else {83stop();84}85}86});87}8889@Override90public Dimension getMinimumSize() {91return getPreferredSize();92}9394@Override95public Dimension getMaximumSize() {96return getPreferredSize();97}9899@Override100public Dimension getPreferredSize() {101int textH = getFontMetrics(font).getHeight();102return new Dimension(135, 2 + textH * 4);103}104105@Override106public void paint(Graphics g) {107if (bimg != null) {108g.drawImage(bimg, 0, 0, this);109}110}111112public void start() {113thread = new Thread(this);114thread.setPriority(Thread.MIN_PRIORITY);115thread.setName("PerformanceMonitor");116thread.start();117}118119public synchronized void stop() {120thread = null;121setSurfaceState();122notify();123}124125public void setSurfaceState() {126if (panel != null) {127for (Component comp : panel.getComponents()) {128if (((DemoPanel) comp).surface != null) {129((DemoPanel) comp).surface.setMonitor(thread != null);130}131}132}133}134135public void setPanel(JPanel panel) {136this.panel = panel;137}138139@Override140@SuppressWarnings("SleepWhileHoldingLock")141public void run() {142143Thread me = Thread.currentThread();144145while (thread == me && !isShowing() || getSize().width == 0) {146try {147Thread.sleep(500);148} catch (InterruptedException e) {149return;150}151}152153Dimension d = new Dimension(0, 0);154Graphics2D big = null;155FontMetrics fm = null;156int ascent = 0;157int descent = 0;158159while (thread == me && isShowing()) {160161if (getWidth() != d.width || getHeight() != d.height) {162d = getSize();163bimg = (BufferedImage) createImage(d.width, d.height);164big = bimg.createGraphics();165big.setFont(font);166fm = big.getFontMetrics();167ascent = fm.getAscent();168descent = fm.getDescent();169setSurfaceState();170}171172big.setBackground(getBackground());173big.clearRect(0, 0, d.width, d.height);174if (panel == null) {175continue;176}177big.setColor(Color.green);178int ssH = 1;179for (Component comp : panel.getComponents()) {180if (((DemoPanel) comp).surface != null) {181String pStr = ((DemoPanel) comp).surface.perfStr;182if (pStr != null) {183ssH += ascent;184big.drawString(pStr, 4, ssH + 1);185ssH += descent;186}187}188}189repaint();190191try {192Thread.sleep(999);193} catch (InterruptedException e) {194break;195}196}197thread = null;198}199} // End Surface200} // End PeformanceMonitor201202203204