< prev index next >

src/demo/share/jfc/J2Ddemo/java2d/MemoryMonitor.java

Print this page


   1 /*
   2  *
   3  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


 108                     surf.start();
 109                     add(surf);
 110                 }
 111                 revalidate();
 112                 repaint();
 113             }
 114         });
 115     }
 116 
 117 
 118     public class Surface extends JPanel implements Runnable {
 119 
 120         public Thread thread;
 121         public long sleepAmount = 1000;
 122         private int w, h;
 123         private BufferedImage bimg;
 124         private Graphics2D big;
 125         private Font font = new Font(Font.SERIF, Font.PLAIN, 11);
 126         private Runtime r = Runtime.getRuntime();
 127         private int columnInc;
 128         private int pts[];
 129         private int ptNum;
 130         private int ascent, descent;
 131         private Rectangle graphOutlineRect = new Rectangle();
 132         private Rectangle2D mfRect = new Rectangle2D.Float();
 133         private Rectangle2D muRect = new Rectangle2D.Float();
 134         private Line2D graphLine = new Line2D.Float();
 135         private Color graphColor = new Color(46, 139, 87);
 136         private Color mfColor = new Color(0, 100, 0);
 137         private String usedStr;
 138 
 139         public Surface() {
 140             setBackground(BLACK);
 141             addMouseListener(new MouseAdapter() {
 142 
 143                 @Override
 144                 public void mouseClicked(MouseEvent e) {
 145                     if (thread == null) {
 146                         start();
 147                     } else {
 148                         stop();


 230 
 231             // .. Draw animated column movement ..
 232             int graphColumn = graphW / 15;
 233 
 234             if (columnInc == 0) {
 235                 columnInc = graphColumn;
 236             }
 237 
 238             for (int j = graphX + columnInc; j < graphW + graphX; j +=
 239                             graphColumn) {
 240                 graphLine.setLine(j, graphY, j, graphY + graphH);
 241                 big.draw(graphLine);
 242             }
 243 
 244             --columnInc;
 245 
 246             if (pts == null) {
 247                 pts = new int[graphW];
 248                 ptNum = 0;
 249             } else if (pts.length != graphW) {
 250                 int tmp[] = null;
 251                 if (ptNum < graphW) {
 252                     tmp = new int[ptNum];
 253                     System.arraycopy(pts, 0, tmp, 0, tmp.length);
 254                 } else {
 255                     tmp = new int[graphW];
 256                     System.arraycopy(pts, pts.length - tmp.length, tmp, 0,
 257                             tmp.length);
 258                     ptNum = tmp.length - 2;
 259                 }
 260                 pts = new int[graphW];
 261                 System.arraycopy(tmp, 0, pts, 0, tmp.length);
 262             } else {
 263                 big.setColor(YELLOW);
 264                 pts[ptNum] =
 265                         (int) (graphY + graphH * (freeMemory / totalMemory));
 266                 for (int j = graphX + graphW - ptNum, k = 0; k < ptNum; k++, j++) {
 267                     if (k != 0) {
 268                         if (pts[k] != pts[k - 1]) {
 269                             big.drawLine(j - 1, pts[k - 1], j, pts[k]);
 270                         } else {


 320                     big = bimg.createGraphics();
 321                     big.setFont(font);
 322                     FontMetrics fm = big.getFontMetrics(font);
 323                     ascent = fm.getAscent();
 324                     descent = fm.getDescent();
 325                 }
 326                 repaint();
 327                 try {
 328                     Thread.sleep(sleepAmount);
 329                 } catch (InterruptedException e) {
 330                     break;
 331                 }
 332                 if (dateStampCB.isSelected()) {
 333                     System.out.println(new Date().toString() + " " + usedStr);
 334                 }
 335             }
 336             thread = null;
 337         }
 338     }
 339 
 340     public static void main(String s[]) {
 341         final MemoryMonitor demo = new MemoryMonitor();
 342         WindowListener l = new WindowAdapter() {
 343 
 344             @Override
 345             public void windowClosing(WindowEvent e) {
 346                 System.exit(0);
 347             }
 348 
 349             @Override
 350             public void windowDeiconified(WindowEvent e) {
 351                 demo.surf.start();
 352             }
 353 
 354             @Override
 355             public void windowIconified(WindowEvent e) {
 356                 demo.surf.stop();
 357             }
 358         };
 359         JFrame f = new JFrame("J2D Demo - MemoryMonitor");
 360         f.addWindowListener(l);
   1 /*
   2  *
   3  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


 108                     surf.start();
 109                     add(surf);
 110                 }
 111                 revalidate();
 112                 repaint();
 113             }
 114         });
 115     }
 116 
 117 
 118     public class Surface extends JPanel implements Runnable {
 119 
 120         public Thread thread;
 121         public long sleepAmount = 1000;
 122         private int w, h;
 123         private BufferedImage bimg;
 124         private Graphics2D big;
 125         private Font font = new Font(Font.SERIF, Font.PLAIN, 11);
 126         private Runtime r = Runtime.getRuntime();
 127         private int columnInc;
 128         private int[] pts;
 129         private int ptNum;
 130         private int ascent, descent;
 131         private Rectangle graphOutlineRect = new Rectangle();
 132         private Rectangle2D mfRect = new Rectangle2D.Float();
 133         private Rectangle2D muRect = new Rectangle2D.Float();
 134         private Line2D graphLine = new Line2D.Float();
 135         private Color graphColor = new Color(46, 139, 87);
 136         private Color mfColor = new Color(0, 100, 0);
 137         private String usedStr;
 138 
 139         public Surface() {
 140             setBackground(BLACK);
 141             addMouseListener(new MouseAdapter() {
 142 
 143                 @Override
 144                 public void mouseClicked(MouseEvent e) {
 145                     if (thread == null) {
 146                         start();
 147                     } else {
 148                         stop();


 230 
 231             // .. Draw animated column movement ..
 232             int graphColumn = graphW / 15;
 233 
 234             if (columnInc == 0) {
 235                 columnInc = graphColumn;
 236             }
 237 
 238             for (int j = graphX + columnInc; j < graphW + graphX; j +=
 239                             graphColumn) {
 240                 graphLine.setLine(j, graphY, j, graphY + graphH);
 241                 big.draw(graphLine);
 242             }
 243 
 244             --columnInc;
 245 
 246             if (pts == null) {
 247                 pts = new int[graphW];
 248                 ptNum = 0;
 249             } else if (pts.length != graphW) {
 250                 int[] tmp = null;
 251                 if (ptNum < graphW) {
 252                     tmp = new int[ptNum];
 253                     System.arraycopy(pts, 0, tmp, 0, tmp.length);
 254                 } else {
 255                     tmp = new int[graphW];
 256                     System.arraycopy(pts, pts.length - tmp.length, tmp, 0,
 257                             tmp.length);
 258                     ptNum = tmp.length - 2;
 259                 }
 260                 pts = new int[graphW];
 261                 System.arraycopy(tmp, 0, pts, 0, tmp.length);
 262             } else {
 263                 big.setColor(YELLOW);
 264                 pts[ptNum] =
 265                         (int) (graphY + graphH * (freeMemory / totalMemory));
 266                 for (int j = graphX + graphW - ptNum, k = 0; k < ptNum; k++, j++) {
 267                     if (k != 0) {
 268                         if (pts[k] != pts[k - 1]) {
 269                             big.drawLine(j - 1, pts[k - 1], j, pts[k]);
 270                         } else {


 320                     big = bimg.createGraphics();
 321                     big.setFont(font);
 322                     FontMetrics fm = big.getFontMetrics(font);
 323                     ascent = fm.getAscent();
 324                     descent = fm.getDescent();
 325                 }
 326                 repaint();
 327                 try {
 328                     Thread.sleep(sleepAmount);
 329                 } catch (InterruptedException e) {
 330                     break;
 331                 }
 332                 if (dateStampCB.isSelected()) {
 333                     System.out.println(new Date().toString() + " " + usedStr);
 334                 }
 335             }
 336             thread = null;
 337         }
 338     }
 339 
 340     public static void main(String[] s) {
 341         final MemoryMonitor demo = new MemoryMonitor();
 342         WindowListener l = new WindowAdapter() {
 343 
 344             @Override
 345             public void windowClosing(WindowEvent e) {
 346                 System.exit(0);
 347             }
 348 
 349             @Override
 350             public void windowDeiconified(WindowEvent e) {
 351                 demo.surf.start();
 352             }
 353 
 354             @Override
 355             public void windowIconified(WindowEvent e) {
 356                 demo.surf.stop();
 357             }
 358         };
 359         JFrame f = new JFrame("J2D Demo - MemoryMonitor");
 360         f.addWindowListener(l);
< prev index next >