📄 surface.java
字号:
db = new DataBufferUShort(imageDataUShort, imageDataUShort.length); wr = Raster.createPackedRaster(db, w, h, w, new int[] {rMask16, gMask16, bMask16}, null); break; case 32: int[] imageDataInt = new int[w * h]; dcm = new DirectColorModel(32, rMask32, gMask32, bMask32); db = new DataBufferInt(imageDataInt, imageDataInt.length); wr = Raster.createPackedRaster(db, w, h, w, new int[] {rMask32, gMask32, bMask32}, null); break; default: {new Exception("Invalid # of bit per pixel").printStackTrace();} } return new BufferedImage(dcm, wr, false, null); } public Graphics2D createGraphics2D(int width, int height, BufferedImage bi, Graphics g) { Graphics2D g2 = null; if (bi != null) { g2 = bi.createGraphics(); } else { g2 = (Graphics2D) g; } g2.setBackground(getBackground()); g2.setRenderingHint(KEY_ANTIALIASING, AntiAlias); g2.setRenderingHint(KEY_RENDERING, Rendering); if (clearSurface || clearOnce) { g2.clearRect(0, 0, width, height); clearOnce = false; } if (texture != null) { // set composite to opaque for texture fills g2.setComposite(AlphaComposite.SrcOver); g2.setPaint(texture); g2.fillRect(0, 0, width, height); } if (composite != null) { g2.setComposite(composite); } return g2; } // ...demos that extend Surface must implement this routine... public abstract void render(int w, int h, Graphics2D g2); /** * It's possible to turn off double-buffering for just the repaint * calls invoked directly on the non double buffered component. * This can be done by overriding paintImmediately() (which is called * as a result of repaint) and getting the current RepaintManager and * turning off double buffering in the RepaintManager before calling * super.paintImmediately(g). */ public void paintImmediately(int x,int y,int w, int h) { RepaintManager repaintManager = null; boolean save = true; if (!isDoubleBuffered()) { repaintManager = RepaintManager.currentManager(this); save = repaintManager.isDoubleBufferingEnabled(); repaintManager.setDoubleBufferingEnabled(false); } super.paintImmediately(x, y, w, h); if (repaintManager != null) { repaintManager.setDoubleBufferingEnabled(save); } } public void paint(Graphics g) { super.paint(g); Dimension d = getSize(); if(biw != d.width || bih != d.height) { toBeInitialized = true; biw = d.width; bih = d.height; } if (imageType == 1) bimg = null; else if(bimg == null || toBeInitialized) { bimg = createBufferedImage((Graphics2D)g, d.width, d.height, imageType-2); clearOnce = true; } if (toBeInitialized) { if (animating != null) animating.reset(d.width, d.height); toBeInitialized = false; startClock(); } if (animating != null && animating.thread != null) { animating.step(d.width, d.height); } Graphics2D g2 = createGraphics2D(d.width, d.height, bimg, g); render(d.width, d.height, g2); g2.dispose(); if (bimg != null) { g.drawImage(bimg, 0, 0, null); toolkit.sync(); } if (perfMonitor) { LogPerformance(); } } public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { if (pi >= 1) { return Printable.NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D) g; g2d.translate(pf.getImageableX(), pf.getImageableY()); g2d.translate(pf.getImageableWidth() / 2, pf.getImageableHeight() / 2); Dimension d = getSize(); double scale = Math.min(pf.getImageableWidth() / d.width, pf.getImageableHeight() / d.height); if (scale < 1.0) { g2d.scale(scale, scale); } g2d.translate(-d.width / 2.0, -d.height / 2.0); if (bimg == null) { Graphics2D g2 = createGraphics2D(d.width, d.height, null, g2d); render(d.width, d.height, g2); g2.dispose(); } else { g2d.drawImage(bimg, 0, 0, this); } return Printable.PAGE_EXISTS; } public void startClock() { orig = System.currentTimeMillis(); start = orig; frame = 0; } private static final int REPORTFRAMES = 30; private void LogPerformance() { if ((frame % REPORTFRAMES) == 0) { long end = System.currentTimeMillis(); long rel = (end - start); long tot = (end - orig); if (frame == 0) { perfStr = name + " " + rel+" ms"; if (animating == null || animating.thread == null) { frame = -1; } } else { String s1 = Float.toString((REPORTFRAMES/(rel/1000.0f))); s1 = (s1.length() < 4) ? s1.substring(0,s1.length()) : s1.substring(0,4); perfStr = name + " " + s1 + " fps"; } if (outputPerf) { System.out.println(perfStr); } start = end; } ++frame; } // System.out graphics state information. public void verbose() { String str = " " + name + " "; if (animating != null && animating.thread != null) { str = str.concat(" Running"); } else if (this instanceof AnimatingSurface) { str = str.concat(" Stopped"); } str = str.concat(" " + GlobalControls.screenCombo.getSelectedItem()); str.concat((AntiAlias == VALUE_ANTIALIAS_ON ) ? " ANTIALIAS_ON " : " ANTIALIAS_OFF "); str.concat((Rendering == VALUE_RENDER_QUALITY) ? "RENDER_QUALITY " : "RENDER_SPEED "); if (texture != null) { str = str.concat("Texture "); } if (composite != null) { str = str.concat("Composite=" + composite.getAlpha() + " "); } Runtime r = Runtime.getRuntime(); r.gc(); float freeMemory = (float) r.freeMemory(); float totalMemory = (float) r.totalMemory(); str = str.concat(((totalMemory - freeMemory)/1024) + "K used"); System.out.println(str); } public static void createDemoFrame(Surface surface) { final DemoPanel dp = new DemoPanel(surface); Frame f = new Frame("Java2D Demo - " + surface.name); f.addWindowListener(new WindowAdapter() { public void windowClosing (WindowEvent e) { System.exit(0); } public void windowDeiconified(WindowEvent e) { dp.start(); } public void windowIconified (WindowEvent e) { dp.stop(); } }); f.add("Center", dp); f.pack(); f.setSize(new Dimension(500,300)); f.setVisible(true); if (surface.animating != null) { surface.animating.start(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -