/* This file is part of the source code for 3D-XplorMath-J, Version 1.0 (January 2008). * Copyright (c) 2008 The 3D-XplorMath Consortium (http://3d-xplormath.org). * This source code is released under a BSD License, which allows redistribution * in source and binary form, with or without modification, provided copyright * and license information are included, and with no warranty or guarantee of * any kind. For details, see http://3d-xplormath.org/j/source/BSDLicense.txt */ package vmm.fractals; import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import vmm.core.Transform; import vmm.core.View; import vmm.core.render.GeometryRenderer2D; /** * For use with the Mandelbrot exhibit. */ class GeometryRenderer2DWithBackgroundImage extends GeometryRenderer2D { protected BufferedImage image; protected Graphics2D imageGraphics; public void startRender(View view, Transform transform, int width, int height) { super.startRender(view, transform, width, height); } public boolean newImage(int width, int height) { if (image == null || image.getWidth() != width || image.getHeight() != height) { image = null; if (imageGraphics != null) imageGraphics.dispose(); imageGraphics = null; image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); imageGraphics = image.createGraphics(); imageGraphics.setColor(Color.WHITE); imageGraphics.fillRect(0,0,width,height); return true; } else return false; } public boolean restartRender(View view, Transform transform, int width, int height) { if (image == null || image.getWidth() != width || image.getHeight() != height) return false; return super.restartRender(view, transform, width, height); } public void dispose() { super.dispose(); if (imageGraphics != null) imageGraphics.dispose(); image = null; imageGraphics = null; } protected void drawBackground(Graphics2D g) { if (image == null) super.drawBackground(g); else g.drawImage(image,0,0,null); } //--------------------------- Some pixel-oriented drawing methods ----------------------- /** * Sets the pixel with pixel coordinates (x,y) to be a specified color. * The pixel color is changed in the off-screen image, not on the screen immmediately. * The current transformation is not applied to the coordinates. * @param color the color for the pixel; if null, the current drawing color is used. * @param x the horizontal pixel coordinate. * @param y the vertical pixel coordinate. */ public void drawPixelDirect(Color color, int x, int y) { if (x < 0 || y < 0 || x >= image.getWidth() || y >= image.getHeight()) return; if (color == null) color = currentColor; int rgb; rgb = (color.getRed() << 16) | (color.getGreen() << 8) | (color.getBlue()); image.setRGB(x,y,rgb); } /** * Draws a line in the current color between points that are specified using * pixel coordinates. */ public void drawLineDirect(Color c, int x1, int y1, int x2, int y2) { imageGraphics.setColor(c); if (antialiased) imageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); imageGraphics.drawLine(x1,y1,x2,y2); } /** * Draws a filled-in rectangle in the current color, where the rectangle is specified in pixel coordinates. */ public void fillRectDirect(Color c, int x, int y, int width, int height) { imageGraphics.setColor(c); imageGraphics.fillRect(x,y,width,height); } public void setImageRGBDirect(int x, int y, int width, int height, int[] rgb) { try { image.setRGB(x,y,width,height,rgb,0,width); } catch (Exception e) { } } }