Java Code Examples for java.awt.Graphics2D.clearRect()

The following are Jave code examples for showing how to use clearRect() of the java.awt.Graphics2D class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: png_to_jpg_in_svg   File: PNG_to_JPG_in_SVG.java   View Source Code Vote up 7 votes
private String convertPngToJpeg(String pngBase64) throws IOException {
	byte[] pngBinary = DatatypeConverter.parseBase64Binary(pngBase64);
	InputStream in = new ByteArrayInputStream(pngBinary);
	BufferedImage pngImage = ImageIO.read(in);

	int width = pngImage.getWidth(), height = pngImage.getHeight();
	BufferedImage jpgImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

	Graphics2D g = jpgImage.createGraphics();
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
	g.setBackground(Color.WHITE);
	g.clearRect(0, 0, width, height);
	g.drawImage(pngImage, 0, 0, width, height, null);
	g.dispose();

	final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	writer.setOutput(ImageIO.createImageOutputStream(baos));
	writer.write(null, new IIOImage(jpgImage, null, null), JPEG_PARAMS);

	String jpgBase64 = DatatypeConverter.printBase64Binary(baos.toByteArray());
	return jpgBase64;
}
 
Example 2
Project: Firma   File: FirmaGenLayer.java   View Source Code Vote up 6 votes
public static void drawImage(int size, FirmaGenLayer genlayer, String name) {
	try {
		File outFile = new File(name + ".bmp");
		if (outFile.exists())
			return;
		int[] ints = genlayer.getInts(0, 0, size, size);
		BufferedImage outBitmap = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = (Graphics2D) outBitmap.getGraphics();
		graphics.clearRect(0, 0, size, size);
		for (int x = 0; x < size; x++) {
			for (int z = 0; z < size; z++) {
				if (ints[x * size + z] != -1 && FirmaBiome.getBiomeList()[ints[x * size + z]] != null) {
					graphics.setColor(Color.getColor("", ((FirmaBiome) Biome.getBiome(ints[x * size + z])).getBiomeColor()));
					graphics.drawRect(x, z, 1, 1);
				}
			}
		}
		System.out.println(outFile.getAbsolutePath());
		ImageIO.write(outBitmap, "BMP", outFile);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 3
Project: incubator-netbeans   File: NimbusViewTabDisplayerUI.java   View Source Code Vote up 6 votes
@Override
protected void paintDisplayerBackground( Graphics g, JComponent c ) {

    int x = 0;
    int y = 0;
    int width = c.getWidth();
    int height = c.getHeight();
    
    Object o = null;
    o = UIManager.get("TabbedPane:TabbedPaneTab[Enabled].backgroundPainter");

    if ((o != null) && (o instanceof javax.swing.Painter)) {
        javax.swing.Painter painter = (javax.swing.Painter) o;
        BufferedImage bufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bufIm.createGraphics();
        g2d.setBackground(UIManager.getColor("Panel.background"));
        g2d.clearRect(0, 0, width, height);
        painter.paint(g2d, null, width, height);
        g.drawImage(bufIm, x, y, null);
    }
}
 
Example 4
Project: VASSAL-src   File: MapBoard.java   View Source Code Vote up 6 votes
@Override
boolean draw(Graphics2D g) {
  // set background color
  g.setBackground(tableColor);
  Dimension d = getLayout().getBoardSize();
  g.clearRect(0, 0, d.width, d.height);

  // See if map image file exists
  File sml = action.getCaseInsensitiveFile(new File(forceExtension(path, "sml")), null, false, null);
  if (sml != null) {
    try {
      readScannedMapLayoutFile(sml, g);
    }
    // FIXME: review error message
    catch (IOException e) {}
  }
  else if (getSet().underlay != null) {
    // If sml file doesn't exist, see if there is a single-sheet underlay image
  g.drawImage(getSet().underlay, null, 0, 0);
  }
  return true;
}
 
Example 5
Project: jaer   File: ToradexOakG3AxisAccelerationSensorGUI.java   View Source Code Vote up 6 votes
@Override
public void paint(Graphics g) {
    super.paint(g);
    if (sensor == null) {
        return;
    }
    Graphics2D g2 = (Graphics2D) g;
    g2.setBackground(Color.WHITE);
    g2.clearRect(0, 0, getWidth(), getHeight());
    g2.setColor(Color.RED);
    g2.setStroke(new BasicStroke(2f));
    ToradexOakG3AxisAccelerationSensor.Acceleration v=sensor.getAcceleration();
    int w=getWidth(), h=getHeight();
    final float m = ToradexOakG3AxisAccelerationSensor.MAX_ACCEL;
    float x = w*v.x/m+w/2;
    float y = h*v.y/m+h/2;
    float z = (v.z+m/2)/m*h/10;
    final int r = (int)z;
    int xx=(int)(x-r), yy=(int)(y-r);
    g2.drawOval(xx, yy, r * 2, r * 2);
}
 
Example 6
Project: OpenJSharp   File: CGLGraphicsConfig.java   View Source Code Vote up 6 votes
@Override
public void flip(final LWComponentPeer<?, ?> peer, final Image backBuffer,
                 final int x1, final int y1, final int x2, final int y2,
                 final BufferCapabilities.FlipContents flipAction) {
    final Graphics g = peer.getGraphics();
    try {
        g.drawImage(backBuffer, x1, y1, x2, y2, x1, y1, x2, y2, null);
    } finally {
        g.dispose();
    }
    if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
        final Graphics2D bg = (Graphics2D) backBuffer.getGraphics();
        try {
            bg.setBackground(peer.getBackground());
            bg.clearRect(0, 0, backBuffer.getWidth(null),
                         backBuffer.getHeight(null));
        } finally {
            bg.dispose();
        }
    }
}
 
Example 7
Project: openjdk-jdk10   File: AltTabCrashTest.java   View Source Code Vote up 6 votes
public void renderToBS() {
    width = getWidth();
    height = getHeight();

    do {
        Graphics2D g2d = (Graphics2D)bufferStrategy.getDrawGraphics();

        g2d.clearRect(0, 0, width, height);
        synchronized (balls) {
            for (Ball b : balls) {
                b.move();
                b.paint(g2d, null);
            }
        }
        g2d.dispose();
    } while (bufferStrategy.contentsLost() ||
            bufferStrategy.contentsRestored());
}
 
Example 8
Project: jdk8u-jdk   File: CGLGraphicsConfig.java   View Source Code Vote up 6 votes
@Override
public void flip(final LWComponentPeer<?, ?> peer, final Image backBuffer,
                 final int x1, final int y1, final int x2, final int y2,
                 final BufferCapabilities.FlipContents flipAction) {
    final Graphics g = peer.getGraphics();
    try {
        g.drawImage(backBuffer, x1, y1, x2, y2, x1, y1, x2, y2, null);
    } finally {
        g.dispose();
    }
    if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
        final Graphics2D bg = (Graphics2D) backBuffer.getGraphics();
        try {
            bg.setBackground(peer.getBackground());
            bg.clearRect(0, 0, backBuffer.getWidth(null),
                         backBuffer.getHeight(null));
        } finally {
            bg.dispose();
        }
    }
}
 
Example 9
Project: incubator-netbeans   File: NimbusEditorTabCellRenderer.java   View Source Code Vote up 5 votes
private static void paintTabBackground (Graphics g, int index, Component c,
int x, int y, int w, int h) {

    Shape clip = g.getClip();
    NimbusEditorTabCellRenderer ren = (NimbusEditorTabCellRenderer) c;

    w +=1;
    boolean isPreviousTabSelected = ren.isPreviousTabSelected();
    if (isPreviousTabSelected) {
        g.setClip(x+1, y, w-1, h);
    }

    Object o = null;
    if (ren.isSelected()) {
        if (ren.isActive()) {
            o = UIManager.get("TabbedPane:TabbedPaneTab[MouseOver+Selected].backgroundPainter");
        } else {
            o = UIManager.get("TabbedPane:TabbedPaneTab[Selected].backgroundPainter");
        }
    } else {
        o = UIManager.get("TabbedPane:TabbedPaneTab[Enabled].backgroundPainter");
    }
    if ((o != null) && (o instanceof javax.swing.Painter)) {
        javax.swing.Painter painter = (javax.swing.Painter) o;
        BufferedImage bufIm = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bufIm.createGraphics();
        g2d.setBackground(UIManager.getColor("Panel.background"));
        g2d.clearRect(0, 0, w, h);
        painter.paint(g2d, null, w, h);
        g.drawImage(bufIm, x, y, null);
    }

    if (isPreviousTabSelected) {
        g.setClip(clip);
    }
}
 
Example 10
Project: jmt   File: JobsDrawer.java   View Source Code Vote up 5 votes
@Override
public void paint(Graphics g) {
	Graphics2D g2d = (Graphics2D) g;
	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
	g2d.setStroke(dCst.getDrawStroke());
	changeDrawSettings(dCst);

	//draw jobs
	drawJobsRemaining(donejobs, totjobs, panelW * 0.15f, 10.0f, panelW * 0.7f, panelH / 2.0f, g2d);
}
 
Example 11
Project: QN-ACTR-Release   File: JobsDrawer.java   View Source Code Vote up 5 votes
@Override
public void paint(Graphics g) {
	Graphics2D g2d = (Graphics2D) g;
	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
	g2d.setStroke(dCst.getDrawStroke());
	changeDrawSettings(dCst);

	//disegna jobs
	drawJobsRemaining(donejobs, totjobs, panelW * 0.15f, 10.0f, panelW * 0.7f, panelH / 2.0f, g2d);
}
 
Example 12
Project: Bachelor-Thesis   File: PlayField.java   View Source Code Vote up 5 votes
/**
 * Draws the play field. To be called by Swing framework or directly if
 * different context than screen is desired
 * @param g The graphics context to draw the field to
 */
public void paint(Graphics g) {
	Graphics2D g2 = (Graphics2D)g;
	g2.setBackground(bgColor);
	
	// clear old playfield graphics
	g2.clearRect(0, 0, this.getWidth(), this.getHeight());
	if (underlayImage != null) {
		g2.drawImage(underlayImage,curTransform, null);
	}

	// draw map (is exists and drawing requested)
	if (mapGraphic != null && showMapGraphic) {
		mapGraphic.draw(g2);
	}
	
	// draw hosts
	for (DTNHost h : w.getHosts()) {
		new NodeGraphic(h).draw(g2); // TODO: Optimization..?
	}
	
	// draw overlay graphics
	for (int i=0, n=overlayGraphics.size(); i<n; i++) {
		overlayGraphics.get(i).draw(g2);
	}
	
	// draw reference scale
	this.refGraphic.draw(g2);
}
 
Example 13
Project: jdk8u-jdk   File: OffScreenImage.java   View Source Code Vote up 5 votes
private void initSurface(int width, int height) {
    Graphics2D g2 = createGraphics();
    try {
        g2.clearRect(0, 0, width, height);
    } finally {
        g2.dispose();
    }
}
 
Example 14
Project: CustomWorldGen   File: ModelLoader.java   View Source Code Vote up 5 votes
@Override
public boolean load(IResourceManager manager, ResourceLocation location)
{
    BufferedImage image = new BufferedImage(this.getIconWidth(), this.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = image.createGraphics();
    graphics.setBackground(Color.WHITE);
    graphics.clearRect(0, 0, this.getIconWidth(), this.getIconHeight());
    int[][] pixels = new int[Minecraft.getMinecraft().gameSettings.mipmapLevels + 1][];
    pixels[0] = new int[image.getWidth() * image.getHeight()];
    image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels[0], 0, image.getWidth());
    this.clearFramesTextureData();
    this.framesTextureData.add(pixels);
    return false;
}
 
Example 15
Project: openjdk-jdk10   File: OffScreenImage.java   View Source Code Vote up 5 votes
private void initSurface(int width, int height) {
    Graphics2D g2 = createGraphics();
    try {
        g2.clearRect(0, 0, width, height);
    } finally {
        g2.dispose();
    }
}
 
Example 16
Project: openjdk-jdk10   File: CrashTest.java   View Source Code Vote up 4 votes
private static void test(final float lineStroke,
                             final boolean useDashes,
                             final float dashMinLen)
        throws ArrayIndexOutOfBoundsException
    {
        System.out.println("---\n" + "test: "
            + "lineStroke=" + lineStroke
            + ", useDashes=" + useDashes
            +", dashMinLen=" + dashMinLen
        );

        final BasicStroke stroke = createStroke(lineStroke, useDashes, dashMinLen);

        // TODO: test Dasher.firstSegmentsBuffer resizing ?
// array.dasher.firstSegmentsBuffer.d_float[2] sum: 6 avg: 3.0 [3 | 3]
        /*
         // Marlin growable arrays:
         = new StatLong("array.dasher.firstSegmentsBuffer.d_float");
         = new StatLong("array.stroker.polystack.curves.d_float");
         = new StatLong("array.stroker.polystack.curveTypes.d_byte");
         = new StatLong("array.marlincache.rowAAChunk.d_byte");
         = new StatLong("array.marlincache.touchedTile.int");
         = new StatLong("array.renderer.alphaline.int");
         = new StatLong("array.renderer.crossings.int");
         = new StatLong("array.renderer.aux_crossings.int");
         = new StatLong("array.renderer.edgeBuckets.int");
         = new StatLong("array.renderer.edgeBucketCounts.int");
         = new StatLong("array.renderer.edgePtrs.int");
         = new StatLong("array.renderer.aux_edgePtrs.int");
         */
        // size > 8192 (exceed both tile and buckets arrays)
        final int size = 9000;
        System.out.println("image size = " + size);

        final BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

        final Graphics2D g2d = (Graphics2D) image.getGraphics();
        try {
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

            g2d.setClip(0, 0, size, size);
            g2d.setBackground(Color.WHITE);
            g2d.clearRect(0, 0, size, size);

            g2d.setStroke(stroke);
            g2d.setColor(Color.BLACK);

            final long start = System.nanoTime();

            paint(g2d, size - 10f);

            final long time = System.nanoTime() - start;

            System.out.println("paint: duration= " + (1e-6 * time) + " ms.");

            if (SAVE_IMAGE) {
                try {
                    final File file = new File("CrashTest-dash-" + useDashes + ".bmp");

                    System.out.println("Writing file: " + file.getAbsolutePath());
                    ImageIO.write(image, "BMP", file);
                } catch (IOException ex) {
                    System.out.println("Writing file failure:");
                    ex.printStackTrace();
                }
            }
        } finally {
            g2d.dispose();
        }
    }
 
Example 17
Project: incubator-netbeans   File: NimbusViewTabDisplayerUI.java   View Source Code Vote up 4 votes
@Override
protected void paintTabBackground(Graphics g, int index, int x, int y,
                                  int width, int height) {
    boolean isLast = index == getDataModel().size()-1;
    if (!isLast) {
        width++;
    }

    Shape clip = g.getClip();
    boolean isPreviousTabSelected = index-1 == displayer.getSelectionModel().getSelectedIndex();
    if (isPreviousTabSelected) {
        g.setClip(x+1, y, width-1, height);
    }

    final boolean attention = isAttention(index);
    Object o = null;
    if (isSelected(index)) {
        String mouseOver = "TabbedPane:TabbedPaneTab[MouseOver+Selected].backgroundPainter";
        String selected = "TabbedPane:TabbedPaneTab[Selected].backgroundPainter";
        if (isActive()) {
            o = UIManager.get( attention ? selected : mouseOver);
        } else {
            o = UIManager.get( attention ? mouseOver: selected);
        }
    } else {
        if( attention ) {
            o = UIManager.get("TabbedPane:TabbedPaneTab[Disabled].backgroundPainter");
        } else {
            o = UIManager.get("TabbedPane:TabbedPaneTab[Enabled].backgroundPainter");
        }
    }

    if ((o != null) && (o instanceof javax.swing.Painter)) {
        javax.swing.Painter painter = (javax.swing.Painter) o;
        BufferedImage bufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bufIm.createGraphics();
        g2d.setBackground(UIManager.getColor("Panel.background"));
        g2d.clearRect(0, 0, width, height);
        painter.paint(g2d, null, width, height);
        g.drawImage(bufIm, x, y, null);
    }

    if (isPreviousTabSelected) {
        g.setClip(clip);
    }
}
 
Example 18
Project: openjdk-jdk10   File: CrashTest.java   View Source Code Vote up 4 votes
private static void testHugeImage(final int width, final int height)
    throws ArrayIndexOutOfBoundsException
{
    System.out.println("---\n" + "testHugeImage: "
        + "width=" + width + ", height=" + height);

    final BasicStroke stroke = createStroke(2.5f, false, 0);

    // size > 24bits (exceed both tile and buckets arrays)
    System.out.println("image size = " + width + " x "+height);

    final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

    final Graphics2D g2d = (Graphics2D) image.getGraphics();
    try {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, width, height);

        g2d.setStroke(stroke);
        g2d.setColor(Color.BLACK);

        final Path2D.Float path = new Path2D.Float(WIND_NON_ZERO, 32);
        path.moveTo(0, 0);
        path.lineTo(width, 0);
        path.lineTo(width, height);
        path.lineTo(0, height);
        path.lineTo(0, 0);

        final long start = System.nanoTime();

        g2d.draw(path);

        final long time = System.nanoTime() - start;

        System.out.println("paint: duration= " + (1e-6 * time) + " ms.");

        if (SAVE_IMAGE) {
            try {
                final File file = new File("CrashTest-huge-"
                    + width + "x" +height + ".bmp");

                System.out.println("Writing file: " + file.getAbsolutePath());
                ImageIO.write(image, "BMP", file);
            } catch (IOException ex) {
                System.out.println("Writing file failure:");
                ex.printStackTrace();
            }
        }
    } finally {
        g2d.dispose();
    }
}
 
Example 19
Project: openjdk-jdk10   File: CrashNaNTest.java   View Source Code Vote up 4 votes
private static void testPathClosed() {
    final int width = 100;
    final int height = 100;

    final BufferedImage image = new BufferedImage(width, height,
                                        BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g2d = (Graphics2D) image.getGraphics();
    try {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, width, height);

        final Path2D.Double path = new Path2D.Double();
        path.moveTo(40, 40);
        path.lineTo(0,   0);
        path.lineTo(80, 0);
        path.closePath();
        path.lineTo(80, 80);
        path.lineTo(0, 80);
        path.closePath();

        g2d.setColor(Color.BLUE);
        g2d.fill(path);

        g2d.setColor(Color.BLACK);
        g2d.draw(path);

        if (SAVE_IMAGE) {
            try {
                final File file = new File("CrashNaNTest-path-closed.png");
                System.out.println("Writing file: "
                                   + file.getAbsolutePath());
                ImageIO.write(image, "PNG", file);
            } catch (IOException ex) {
                System.out.println("Writing file failure:");
                ex.printStackTrace();
            }
        }

        // Check image on few pixels:
        final Raster raster = image.getData();

        checkPixel(raster, 10, 05, Color.BLUE.getRGB());
        checkPixel(raster, 70, 05, Color.BLUE.getRGB());
        checkPixel(raster, 40, 35, Color.BLUE.getRGB());

        checkPixel(raster, 10, 75, Color.BLUE.getRGB());
        checkPixel(raster, 70, 75, Color.BLUE.getRGB());
        checkPixel(raster, 40, 45, Color.BLUE.getRGB());

    } finally {
        g2d.dispose();
    }
}
 
Example 20
Project: openjdk-jdk10   File: CrashNaNTest.java   View Source Code Vote up 4 votes
private static void testFillDefaultAt() {
    final int width = 400;
    final int height = 400;

    final BufferedImage image = new BufferedImage(width, height,
                                        BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g2d = (Graphics2D) image.getGraphics();
    try {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, width, height);

        final Path2D.Double path = new Path2D.Double();
        path.moveTo(100, 100);

        for (int i = 0; i < 20000; i++) {
            path.lineTo(110 + 0.01 * i, 110);
            path.lineTo(111 + 0.01 * i, 100);
        }

        path.lineTo(NaN, 200);
        path.lineTo(200, 200);
        path.lineTo(200, NaN);
        path.lineTo(300, 300);
        path.lineTo(NaN, NaN);
        path.lineTo(100, 200);
        path.closePath();

        final Path2D.Double path2 = new Path2D.Double();
        path2.moveTo(0, 0);
        path2.lineTo(100, height);
        path2.lineTo(0, 200);
        path2.closePath();

        g2d.setColor(Color.BLUE);
        g2d.fill(path);
        g2d.setColor(Color.GREEN);
        g2d.fill(path2);

        g2d.setColor(Color.BLACK);
        g2d.draw(path);
        g2d.draw(path2);

        if (SAVE_IMAGE) {
            try {
                final File file = new File("CrashNaNTest-fill.png");
                System.out.println("Writing file: "
                                   + file.getAbsolutePath());
                ImageIO.write(image, "PNG", file);
            } catch (IOException ex) {
                System.out.println("Writing file failure:");
                ex.printStackTrace();
            }
        }

        // Check image on few pixels:
        final Raster raster = image.getData();

        checkPixel(raster, 200, 195, Color.BLUE.getRGB());
        checkPixel(raster, 105, 195, Color.BLUE.getRGB());
        checkPixel(raster, 286, 290, Color.BLUE.getRGB());

        checkPixel(raster, 108, 105, Color.WHITE.getRGB());
        checkPixel(raster, 205, 200, Color.WHITE.getRGB());

        checkPixel(raster, 5, 200, Color.GREEN.getRGB());

    } finally {
        g2d.dispose();
    }
}