Java Code Examples for java.awt.Graphics#dispose()

The following examples show how to use java.awt.Graphics#dispose() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompositionArea.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private Rectangle getCaretRectangle(TextHitInfo caret) {
    int caretLocation = 0;
    TextLayout layout = composedTextLayout;
    if (layout != null) {
        caretLocation = Math.round(layout.getCaretInfo(caret)[0]);
    }
    Graphics g = getGraphics();
    FontMetrics metrics = null;
    try {
        metrics = g.getFontMetrics();
    } finally {
        g.dispose();
    }
    return new Rectangle(TEXT_ORIGIN_X + caretLocation,
                         TEXT_ORIGIN_Y - metrics.getAscent(),
                         0, metrics.getAscent() + metrics.getDescent());
}
 
Example 2
Source File: ImageTests.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public void modifyTest(TestEnvironment env) {
    int size = env.getIntValue(sizeList);
    Image src = tsit.getImage(env, size, size);
    Graphics g = src.getGraphics();
    if (hasGraphics2D) {
        ((Graphics2D) g).setComposite(AlphaComposite.Src);
    }
    if (size == 1) {
        g.setColor(colorsets[transparency][4]);
        g.fillRect(0, 0, 1, 1);
    } else {
        int mid = size/2;
        g.setColor(colorsets[transparency][0]);
        g.fillRect(0, 0, mid, mid);
        g.setColor(colorsets[transparency][1]);
        g.fillRect(mid, 0, size-mid, mid);
        g.setColor(colorsets[transparency][2]);
        g.fillRect(0, mid, mid, size-mid);
        g.setColor(colorsets[transparency][3]);
        g.fillRect(mid, mid, size-mid, size-mid);
    }
    g.dispose();
    env.setSrcImage(src);
}
 
Example 3
Source File: AcceleratedScaleTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void initVI(GraphicsConfiguration gc) {
    int res;
    if (destVI == null) {
        res = VolatileImage.IMAGE_INCOMPATIBLE;
    } else {
        res = destVI.validate(gc);
    }
    if (res == VolatileImage.IMAGE_INCOMPATIBLE) {
        if (destVI != null) destVI.flush();
        destVI = gc.createCompatibleVolatileImage(IMAGE_SIZE, IMAGE_SIZE);
        destVI.validate(gc);
        res = VolatileImage.IMAGE_RESTORED;
    }
    if (res == VolatileImage.IMAGE_RESTORED) {
        Graphics vig = destVI.getGraphics();
        vig.setColor(Color.red);
        vig.fillRect(0, 0, destVI.getWidth(), destVI.getHeight());
        vig.dispose();
    }
}
 
Example 4
Source File: ImageGenerator.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public ImageGenerator(int _width, int _height, Color bgColor)
{
      width = _width;
      height = _height;
      bi = new BufferedImage(
          width,
          height,
          BufferedImage.TYPE_INT_ARGB);
      Graphics gr = bi.getGraphics();
      if(null==bgColor){
          bgColor = Color.WHITE;
      }
      gr.setColor(bgColor);
      gr.fillRect(0, 0, width, height);
      paint(gr);
      gr.dispose();
}
 
Example 5
Source File: CursorSprite.java    From megamek with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void prepare() {
    // create image for buffer
    Image tempImage = new BufferedImage(bounds.width, bounds.height,
            BufferedImage.TYPE_INT_ARGB);
    Graphics graph = tempImage.getGraphics();
    
    if (GUIPreferences.getInstance().getAntiAliasing()) {
        ((Graphics2D) graph).setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
    }

    // fill with key color
    graph.setColor(new Color(0,0,0,0));
    graph.fillRect(0, 0, bounds.width, bounds.height);
    // draw attack poly
    graph.setColor(color);
    graph.drawPolygon(BoardView1.hexPoly);

    // create final image
    image = bv.getScaledImage(bv.createImage(tempImage.getSource()), false);
    
    graph.dispose();
    tempImage.flush();
}
 
Example 6
Source File: ImageTests.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public void modifyTest(TestEnvironment env) {
    int size = env.getIntValue(sizeList);
    Image src = tsit.getImage(env, size, size);
    Graphics g = src.getGraphics();
    if (hasGraphics2D) {
        ((Graphics2D) g).setComposite(AlphaComposite.Src);
    }
    if (size == 1) {
        g.setColor(colorsets[transparency][4]);
        g.fillRect(0, 0, 1, 1);
    } else {
        int mid = size/2;
        g.setColor(colorsets[transparency][0]);
        g.fillRect(0, 0, mid, mid);
        g.setColor(colorsets[transparency][1]);
        g.fillRect(mid, 0, size-mid, mid);
        g.setColor(colorsets[transparency][2]);
        g.fillRect(0, mid, mid, size-mid);
        g.setColor(colorsets[transparency][3]);
        g.fillRect(mid, mid, size-mid, size-mid);
    }
    g.dispose();
    env.setSrcImage(src);
}
 
Example 7
Source File: AppearanceCanvas.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void paintForeground(Graphics g) {
	double zoom = grid.getZoomFactor();
	Graphics gScaled = g.create();
	if (zoom != 1.0 && zoom != 0.0 && gScaled instanceof Graphics2D) {
		((Graphics2D) gScaled).scale(zoom, zoom);
	}
	super.paintForeground(gScaled);
	gScaled.dispose();
}
 
Example 8
Source File: QRCodeUtil.java    From JavaWeb with Apache License 2.0 5 votes vote down vote up
private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
    File file = new File(imgPath);
    if (!file.exists()) {
        System.err.println(""+imgPath+"   该文件不存在!");
        return;
    }
    Image src = ImageIO.read(new File(imgPath));
    int width = src.getWidth(null);
    int height = src.getHeight(null);
    if (needCompress) { //压缩LOGO
        if (width > WIDTH) {
            width = WIDTH;
        }
        if (height > HEIGHT) {
            height = HEIGHT;
        }
        Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = tag.getGraphics();
        g.drawImage(image, 0, 0, null); // 绘制缩小后的图
        g.dispose();
        src = image;
    }
    //插入LOGO
    Graphics2D graph = source.createGraphics();
    int x = (QRCODE_SIZE - width) / 2;
    int y = (QRCODE_SIZE - height) / 2;
    graph.drawImage(src, x, y, width, height, null);
    Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
    graph.setStroke(new BasicStroke(3f));
    graph.draw(shape);
    graph.dispose();
}
 
Example 9
Source File: StatusBar.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void showStatus(String msg) {
  this.msg = msg;
  Graphics g = getGraphics();
  if(g != null) {
    paint(g);
    g.dispose();
  } else {
    //      No graphics in showStatus
  }
}
 
Example 10
Source File: CaptchaUtils.java    From EasyEE with MIT License 5 votes vote down vote up
/**
	 * 生成随机图片
	 */
	public BufferedImage genRandomCodeImage(StringBuffer randomCode) {
		// BufferedImage类是具有缓冲区的Image类
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// 获取Graphics对象,便于对图像进行各种绘制操作
		Graphics g = image.getGraphics();
//		g.setColor(getRandColor(200, 250));
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);
		//g.setColor(new Color()); 
		//g.drawRect(0,0,width-1,height-1); 
		g.setColor(getRandColor(80, 140));

		
		// 绘制随机字符
		g.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
		
		for (int i = 0; i < strNum; i++) {
			randomCode.append(drowString(g, i));
		}
		
		// 绘制干扰线
			for (int i = 0; i <= lineNum; i++) {
				drowLine(g);
			}
		
		g.dispose();
		return image;
	}
 
Example 11
Source File: DiacriticsDrawingTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImage drawString(String text) {
    BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.createGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
    g.setColor(Color.black);
    g.setFont(FONT);
    g.drawString(text, TEXT_X, TEXT_Y);
    g.dispose();
    return image;
}
 
Example 12
Source File: bug8032667_image_diff.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static BufferedImage getImage(JComponent component, int scale, int width, int height) {
    final BufferedImage image = new BufferedImage(
            scale * width, scale * height, BufferedImage.TYPE_INT_ARGB);
    final Graphics g = image.getGraphics();
    ((Graphics2D) g).scale(scale, scale);
    component.paint(g);
    g.dispose();
    return image;
}
 
Example 13
Source File: MainPanel.java    From javagame with MIT License 5 votes vote down vote up
/**
 * �o�b�t�@����ʂɕ`��
 */
private void paintScreen() {
	try {
		Graphics g = getGraphics(); // �O���t�B�b�N�I�u�W�F�N�g���擾
		if ((g != null) && (dbImage != null)) {
			g.drawImage(dbImage, 0, 0, null); // �o�b�t�@�C���[�W����ʂɕ`��
		}
		Toolkit.getDefaultToolkit().sync();
		if (g != null) {
			g.dispose(); // �O���t�B�b�N�I�u�W�F�N�g��j��
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 14
Source File: TestEnvironment.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void flushToScreen() {
    if (testImage != null && comp != null) {
        Graphics g = comp.getGraphics();
        if (GraphicsTests.hasGraphics2D) {
            ((Graphics2D) g).setComposite(AlphaComposite.Src);
        }
        g.drawImage(testImage, 0, 0, null);
        g.dispose();
    }
}
 
Example 15
Source File: MultiResolutionDrawImageWithTransformTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public TestSurfaceData(int width, int height, double scale) {
    super(StateTrackable.State.DYNAMIC, SurfaceType.Custom, ColorModel.getRGBdefault());
    this.scale = scale;
    gc = new TestGraphicsConfig(scale);
    this.width = (int) Math.ceil(scale * width);
    this.height = (int) Math.ceil(scale * height);
    buffImage = new BufferedImage(this.width, this.height,
            BufferedImage.TYPE_INT_RGB);

    Graphics imageGraphics = buffImage.createGraphics();
    imageGraphics.setColor(BACKGROUND_COLOR);
    imageGraphics.fillRect(0, 0, this.width, this.height);
    imageGraphics.dispose();
}
 
Example 16
Source File: GifTransparencyTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected void checkResult(BufferedImage src, BufferedImage dst) {
    int w = src.getWidth();
    int h = src.getHeight();


    if (dst.getWidth() != w || dst.getHeight() != h) {
        throw new RuntimeException("Test failed: wrong result dimension");
    }

    BufferedImage bg = new BufferedImage(2 * w, h, BufferedImage.TYPE_INT_RGB);
    Graphics g = bg.createGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, 2 * w, h);

    g.drawImage(src, 0, 0, null);
    g.drawImage(dst, w, 0, null);

    g.dispose();

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int src_rgb = bg.getRGB(x, y);
            int dst_rgb = bg.getRGB(x + w, y);

            if (dst_rgb != src_rgb) {
                throw new RuntimeException("Test failed: wrong color " +
                        Integer.toHexString(dst_rgb) + " at " + x + ", " +
                        y + " (instead of " + Integer.toHexString(src_rgb) +
                        ")");
            }
        }
    }
    System.out.println("Test passed.");
}
 
Example 17
Source File: JmeParticleEmitterButtonProperty.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void paintValue(Graphics gfx, Rectangle box) {
    if (ed == null) {
        getInplaceEditor();
    }
    ed.setSize(box.width, box.height);
    ed.doLayout();
    Graphics g = gfx.create(box.x, box.y, box.width, box.height);
    ed.setOpaque(false);
    ed.paint(g);
    g.dispose();
    pe.refresh(false);
}
 
Example 18
Source File: Hooks.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Copy an image
 * @param src
 * @return
 */
private static Image copy(Image src)
{
	final int width = src.getWidth(null);
	final int height = src.getHeight(null);
	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	Graphics graphics = image.getGraphics();
	graphics.drawImage(src, 0, 0, width, height, null);
	graphics.dispose();
	return image;
}
 
Example 19
Source File: OutputImageTests.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
Context(TestEnvironment env, Result result, int testType) {
    super(env, result);

    String content = (String)env.getModifier(contentList);
    if (content == null) {
        content = CONTENT_BLANK;
    }
    // REMIND: add option for non-opaque images
    image = createBufferedImage(size, size, content, false);

    result.setUnits(size*size);
    result.setUnitName("pixel");

    if (testType == TEST_IMAGEIO || testType == TEST_IMAGEWRITER) {
        ImageWriterSpi writerspi =
            (ImageWriterSpi)env.getModifier(imageioWriteFormatList);
        format = writerspi.getFileSuffixes()[0].toLowerCase();
        if (testType == TEST_IMAGEWRITER) {
            try {
                writer = writerspi.createWriterInstance();
            } catch (IOException e) {
                System.err.println("error creating writer");
                e.printStackTrace();
            }
            if (env.isEnabled(installListenerTog)) {
                writer.addIIOWriteProgressListener(
                    new WriteProgressListener());
            }
        }
        if (format.equals("wbmp")) {
            // REMIND: this is a hack to create an image that the
            //         WBMPImageWriter can handle (a better approach
            //         would involve checking the ImageTypeSpecifier
            //         of the writer's default image param)
            BufferedImage newimg =
                new BufferedImage(size, size,
                                  BufferedImage.TYPE_BYTE_BINARY);
            Graphics g = newimg.createGraphics();
            g.drawImage(image, 0, 0, null);
            g.dispose();
            image = newimg;
        }
    } else { // testType == TEST_JPEGCODEC
        format = "jpeg";
    }

    initOutput();
}
 
Example 20
Source File: InputImageTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
Context(TestEnvironment env, Result result, int testType) {
    super(env, result);

    String content = (String)env.getModifier(contentList);
    if (content == null) {
        content = CONTENT_BLANK;
    }
    // REMIND: add option for non-opaque images
    image = createBufferedImage(size, size, content, false);

    result.setUnits(size*size);
    result.setUnitName("pixel");

    if (testType == TEST_IMAGEIO || testType == TEST_IMAGEREADER) {
        ImageReaderSpi readerspi =
            (ImageReaderSpi)env.getModifier(imageioReadFormatList);
        format = readerspi.getFileSuffixes()[0].toLowerCase();
        if (testType == TEST_IMAGEREADER) {
            seekForwardOnly = env.isEnabled(seekForwardOnlyTog);
            ignoreMetadata = env.isEnabled(ignoreMetadataTog);
            try {
                reader = readerspi.createReaderInstance();
            } catch (IOException e) {
                System.err.println("error creating reader");
                e.printStackTrace();
            }
            if (env.isEnabled(installListenerTog)) {
                reader.addIIOReadProgressListener(
                    new ReadProgressListener());
            }
        }
        if (format.equals("wbmp")) {
            // REMIND: this is a hack to create an image that the
            //         WBMPImageWriter can handle (a better approach
            //         would involve checking the ImageTypeSpecifier
            //         of the writer's default image param)
            BufferedImage newimg =
                new BufferedImage(size, size,
                                  BufferedImage.TYPE_BYTE_BINARY);
            Graphics g = newimg.createGraphics();
            g.drawImage(image, 0, 0, null);
            g.dispose();
            image = newimg;
        }
    } else if (testType == TEST_TOOLKIT) {
        format = (String)env.getModifier(toolkitReadFormatList);
    } else { // testType == TEST_JPEGCODEC
        format = "jpeg";
    }

    initInput();
}