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

The following are Jave code examples for showing how to use clearRect() of the java.awt.Graphics 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: VASSAL-src   File: RegionGrid.java   View Source Code Vote up 6 votes
public void paint(Graphics g) {
  Rectangle b = getVisibleRect();
  g.clearRect(b.x, b.y, b.width, b.height);
  myBoard.draw(g, 0, 0, 1.0, this);
  final Rectangle bounds =
    new Rectangle(new Point(),myBoard.bounds().getSize());
  grid.forceDraw(g,bounds,bounds,1.0,false);
  final Rectangle selection = config.getSelectionRect();
  if (selection != null) {
    final Graphics2D g2d = (Graphics2D) g;
    final Stroke str = g2d.getStroke();
    g2d.setStroke(new BasicStroke(2.0f));
    g2d.setColor(Color.RED);
    g2d.drawRect(selection.x, selection.y, selection.width, selection.height);
    g2d.setStroke(str);
  }
}
 
Example 2
Project: VASSAL-src   File: Visualizer.java   View Source Code Vote up 6 votes
public Visualizer(Visualizable obs) {

    observer = obs;
    setBorder(BorderFactory.createLineBorder(Color.black));

    visPanel = new JPanel() {
      private static final long serialVersionUID = 1L;

      public void paint(Graphics g) {
        g.clearRect(0, 0, observer.getVisualizerWidth(), observer.getVisualizerHeight());
        g.drawImage(observer.getVisualizerImage(), 0, 0, this);
      }
    };

    add(visPanel, BorderLayout.CENTER);
  }
 
Example 3
Project: BrainControl   File: HealthPanel.java   View Source Code Vote up 6 votes
public void draw(int healthLevel) {
	int squareW = this.getWidth();
	int squareH = this.getHeight()-2;
	int squareX = 0;
	int squareY = 0;
	int widthX = (int)((double)squareW / (double)(Player.healthLevelScaling*2));
	Graphics g = this.getGraphics();
	g.clearRect(0, 0, squareW, squareH);

	g.setColor(getColorForHealth(healthLevel));
	for(int i = 0; i<healthLevel;i++){
		
		g.fillRect(squareX+ i * widthX +2 , squareY, widthX, squareH);
		
	}
	g.setColor(Color.BLACK);
	for(int j =0; j <Player.healthLevelScaling*2;j++) {
		
		g.drawRect(squareX+ j * widthX, squareY, widthX-2, squareH);
	}


}
 
Example 4
Project: Logisim   File: Canvas.java   View Source Code Vote up 6 votes
protected void paintBackground(Graphics g) {
	if (AppPreferences.ANTI_ALIASING.getBoolean()) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	}
	g.clearRect(0, 0, getWidth(), getHeight());
}
 
Example 5
Project: etomica   File: Image.java   View Source Code Vote up 6 votes
static int[] drawImageToBuffer(Object gOffscreen, Object imageOffscreen,
                               Object imageobj, int width, int height,
                               int bgcolor) {
  Graphics g = (Graphics) gOffscreen;
  java.awt.Image image = (java.awt.Image) imageobj;
  int width0 = image.getWidth(null);
  int height0 = image.getHeight(null);
  if (g instanceof Graphics2D) {
    ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
        AlphaComposite.SRC_IN, 1.0f));
    g.setColor(bgcolor == 0 ? new Color(0, 0, 0, 0) : new Color(bgcolor));
    g.fillRect(0, 0, width, height);
    ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
        AlphaComposite.SRC_OVER, 1.0f));
    g.drawImage(image, 0, 0, width, height, 0, 0, width0, height0, null);
  } else {
    g.clearRect(0, 0, width, height);
    g.drawImage(image, 0, 0, width, height, 0, 0, width0, height0, null);
  }
  return grabPixels(imageOffscreen, width, height, null, 0, 0);
}
 
Example 6
Project: Ultraino   File: VolAlphaPanel.java   View Source Code Vote up 6 votes
@Override
public void paint(Graphics g) {
    final int w = getWidth();
    final int h = getHeight();
    g.clearRect(0, 0, w, h);
    
    g.setColor(Color.BLACK);
    float widthPerBar = w / (float)xResolution;
    float startX = 0;
    synchronized(this){
        totalValue = 0;
        for(int i = 0; i < xResolution; ++i){
            float endX = startX + widthPerBar;
            float v = values.get(i);
            totalValue += v;
            float height = v * h;

            g.fillRect((int)startX, h - (int)height, (int)widthPerBar, (int)height);

            startX = endX;
        }
    }
}
 
Example 7
Project: VASSAL-src   File: BoardSlot.java   View Source Code Vote up 5 votes
public void paintIcon(Component c, Graphics g, int x, int y) {
  if (board != null) {
    board.draw(g,x,y,picker.getSlotScale(), c);
  }
  else {
    g.clearRect(x,y,getIconWidth(),getIconHeight());
  }
}
 
Example 8
Project: openjdk-jdk10   File: WCanvasPeer.java   View Source Code Vote up 5 votes
@Override
public void paint(Graphics g) {
    Dimension d = ((Component)target).getSize();
    if (g instanceof Graphics2D ||
        g instanceof sun.awt.Graphics2Delegate) {
        // background color is setup correctly, so just use clearRect
        g.clearRect(0, 0, d.width, d.height);
    } else {
        // emulate clearRect
        g.setColor(((Component)target).getBackground());
        g.fillRect(0, 0, d.width, d.height);
        g.setColor(((Component)target).getForeground());
    }
    super.paint(g);
}
 
Example 9
Project: Ultraino   File: TestMaxSpeed.java   View Source Code Vote up 5 votes
@Override
public void paint(Graphics g) {
    final int w = getWidth();
    final int h = getHeight();
    g.clearRect(0, 0, w, h);
    g.setColor( Color.BLUE );
    final int mi = M.min(w, dataLenght);
    for(int i = 0; i < mi; ++i){
        final int iy = bufferData[i] * h / 128;
        g.drawRect(i, iy, 1, 1);
    }
}
 
Example 10
Project: N-Queen-Puzzle-Knight-sTour   File: Queen.java   View Source Code Vote up 5 votes
public void paint(Graphics g){
    int d = dimension;
    Image img1 = Toolkit.getDefaultToolkit().getImage("src/pic_queen.png");
    g.setColor(new Color(255, 189, 35));
    g.fillRect(margin, margin, dim, dim);
    if(d == 1)
         g.drawImage(img1, margin, margin, size, size , null , this);
    else{
        for(int i = margin; i < dim + margin; i+=2*size){
            for(int j = margin; j < dim + margin; j+=2*size){
                g.clearRect(i, j, size, size);
            }
        }

        for(int i = margin+size; i < dim + margin; i+=2*size){
            for(int j = margin + size; j < dim + margin; j+=2*size){
                g.clearRect(i, j, size, size);
            }
        }
    }
    
    
    for(int i = 0; i < d; i++)
        for(int j = 0; j < d; j++)
            if(t[i][j] == 'Q')
            g.drawImage(img1, margin + i*size, margin + j * size, size, size , null , this);
    
    
}
 
Example 11
Project: Logisim   File: Toolbar.java   View Source Code Vote up 5 votes
@Override
public void paintComponent(Graphics g) {
	if (AppPreferences.ANTI_ALIASING.getBoolean()) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	}
	g.clearRect(0, 0, getWidth(), getHeight());
	CanvasTool current = canvas.getTool();
	for (int i = 0; i < tools.length; i++) {
		AbstractTool[] column = tools[i];
		int x = ICON_SEP + i * (ICON_SEP + ICON_WIDTH);
		int y = ICON_SEP;
		for (int j = 0; j < column.length; j++) {
			AbstractTool tool = column[j];
			if (tool == listener.toolPressed && listener.inTool) {
				g.setColor(Color.darkGray);
				g.fillRect(x, y, ICON_WIDTH, ICON_HEIGHT);
			}
			Icon icon = tool.getIcon();
			if (icon != null)
				icon.paintIcon(this, g, x, y);
			if (tool == current) {
				GraphicsUtil.switchToWidth(g, 2);
				g.setColor(Color.black);
				g.drawRect(x - 1, y - 1, ICON_WIDTH + 2, ICON_HEIGHT + 2);
			}
			y += ICON_HEIGHT + ICON_SEP;
		}
	}
	g.setColor(Color.black);
	GraphicsUtil.switchToWidth(g, 1);
}
 
Example 12
Project: New-Kepard   File: JWindow.java   View Source Code Vote up 5 votes
private void PaintFlightLog(Graphics g)
{
	int paintsCount = -1;
	
	int textSize = 0;
	for(int i = 0; i < window.paintText.size(); i++)
		for(int k = 0; k < window.paintText.get(i).split("\n").length; k++)
			textSize++;
	
	g.clearRect(0, 0, window.button_abort.getBounds().x-1, getHeight());
	for(int i = 0; i < window.paintText.size(); i++)
	{
		String text = window.paintText.get(i);
		
		if(text.substring(0, 5).equals("ALERT")) g.setColor(Color.RED);
		
		String[] texts = text.split("\n");
		
		for(int k = 0; k < texts.length; k++)
		{
			paintsCount++;
			g.drawString(texts[k], window.paintStartPos.width, getHeight() - (window.paintStartPos.height + window.paintDistance*(textSize -paintsCount)));
		}
		
		g.setColor(Color.BLACK);
	}
}
 
Example 13
Project: OpenJSharp   File: XYZApp.java   View Source Code Vote up 5 votes
@Override
public void update(Graphics g) {
    if (backBuffer == null) {
        g.clearRect(0, 0, getSize().width, getSize().height);
    }
    paint(g);
}
 
Example 14
Project: openjdk-jdk10   File: XYZApp.java   View Source Code Vote up 5 votes
@Override
public void update(Graphics g) {
    if (backBuffer == null) {
        g.clearRect(0, 0, getSize().width, getSize().height);
    }
    paint(g);
}
 
Example 15
Project: OpenJSharp   File: WCanvasPeer.java   View Source Code Vote up 5 votes
@Override
public void paint(Graphics g) {
    Dimension d = ((Component)target).getSize();
    if (g instanceof Graphics2D ||
        g instanceof sun.awt.Graphics2Delegate) {
        // background color is setup correctly, so just use clearRect
        g.clearRect(0, 0, d.width, d.height);
    } else {
        // emulate clearRect
        g.setColor(((Component)target).getBackground());
        g.fillRect(0, 0, d.width, d.height);
        g.setColor(((Component)target).getForeground());
    }
    super.paint(g);
}
 
Example 16
Project: jdk8u-jdk   File: VolatileSurfaceManager.java   View Source Code Vote up 5 votes
/**
 * Set contents of the current SurfaceData to default state (i.e. clear
 * the background).
 */
public void initContents() {
    // images with forced acceleration type may have a null sdCurrent
    // because we do not create a backup surface for them
    if (sdCurrent != null) {
        Graphics g = vImg.createGraphics();
        g.clearRect(0, 0, vImg.getWidth(), vImg.getHeight());
        g.dispose();
    }
}
 
Example 17
Project: jdk8u-jdk   File: WCanvasPeer.java   View Source Code Vote up 5 votes
@Override
public void paint(Graphics g) {
    Dimension d = ((Component)target).getSize();
    if (g instanceof Graphics2D ||
        g instanceof sun.awt.Graphics2Delegate) {
        // background color is setup correctly, so just use clearRect
        g.clearRect(0, 0, d.width, d.height);
    } else {
        // emulate clearRect
        g.setColor(((Component)target).getBackground());
        g.fillRect(0, 0, d.width, d.height);
        g.setColor(((Component)target).getForeground());
    }
    super.paint(g);
}
 
Example 18
Project: openjdk-jdk10   File: RepaintArea.java   View Source Code Vote up 4 votes
/**
 * Invokes paint and update on target Component with optimal
 * rectangular clip region.
 * If PAINT bounding rectangle is less than
 * MAX_BENEFIT_RATIO times the benefit, then the vertical and horizontal unions are
 * painted separately.  Otherwise the entire bounding rectangle is painted.
 *
 * @param   target Component to {@code paint} or {@code update}
 * @since   1.4
 */
public void paint(Object target, boolean shouldClearRectBeforePaint) {
    Component comp = (Component)target;

    if (isEmpty()) {
        return;
    }

    if (!comp.isVisible()) {
        return;
    }

    RepaintArea ra = this.cloneAndReset();

    if (!subtract(ra.paintRects[VERTICAL], ra.paintRects[HORIZONTAL])) {
        subtract(ra.paintRects[HORIZONTAL], ra.paintRects[VERTICAL]);
    }

    if (ra.paintRects[HORIZONTAL] != null && ra.paintRects[VERTICAL] != null) {
        Rectangle paintRect = ra.paintRects[HORIZONTAL].union(ra.paintRects[VERTICAL]);
        int square = paintRect.width * paintRect.height;
        int benefit = square - ra.paintRects[HORIZONTAL].width
            * ra.paintRects[HORIZONTAL].height - ra.paintRects[VERTICAL].width
            * ra.paintRects[VERTICAL].height;
        // if benefit is comparable with bounding box
        if (MAX_BENEFIT_RATIO * benefit < square) {
            ra.paintRects[HORIZONTAL] = paintRect;
            ra.paintRects[VERTICAL] = null;
        }
    }
    for (int i = 0; i < paintRects.length; i++) {
        if (ra.paintRects[i] != null
            && !ra.paintRects[i].isEmpty())
        {
            // Should use separate Graphics for each paint() call,
            // since paint() can change Graphics state for next call.
            Graphics g = comp.getGraphics();
            if (g != null) {
                try {
                    g.setClip(ra.paintRects[i]);
                    if (i == UPDATE) {
                        updateComponent(comp, g);
                    } else {
                        if (shouldClearRectBeforePaint) {
                            g.clearRect( ra.paintRects[i].x,
                                         ra.paintRects[i].y,
                                         ra.paintRects[i].width,
                                         ra.paintRects[i].height);
                        }
                        paintComponent(comp, g);
                    }
                } finally {
                    g.dispose();
                }
            }
        }
    }
}
 
Example 19
Project: New-Kepard   File: JWindow.java   View Source Code Vote up 4 votes
private void PaintFlightTelemetry(Graphics g)
{
	g.clearRect(window.log_box.width, 0, window.log_box.width + window.telemetry_box_width, window.log_box.height);
	g.drawString("Booster:", window.log_box.width + telemetry_startPos[0], 10);
	g.drawString("Capsule:", window.log_box.width + telemetry_startPos[1], 10);
	
	if(telemetry_isActive[0])
	{
		for(int i = 0; i < window.telemetry_booster.length; i++)
			g.drawString(window.telemetry_booster[i][0] + window.telemetry_booster[i][1], window.log_box.width + telemetry_startPos[0], 25 + 15*i);
	}
	else g.drawString("NO TELEMETRY", window.log_box.width + telemetry_startPos[0], 25);
	
	if(telemetry_isActive[1])
	{
		for(int i = 0; i < window.telemetry_capsule.length; i++)
			g.drawString(window.telemetry_capsule[i][0] + window.telemetry_capsule[i][1], window.log_box.width + telemetry_startPos[1], 25 + 15*i);
	}
	else g.drawString("NO TELEMETRY", window.log_box.width + telemetry_startPos[1], 25);
	
	if(telemetry_isActive[2])
	{
		double zoom = Math.max(2*Math.max(Math.abs(window.telemetry_booster_finalDistance.x), Math.abs(window.telemetry_booster_finalDistance.y)), telemetry_align_box_realSize);
		double zoomSize = Math.min(zoom, 150d);
		
		int boxStartW = telemetry_align_box.x + telemetry_align_box.width/2;
		int boxStartH = telemetry_align_box.y + telemetry_align_box.height/2;
		int padW = (int) ((telemetry_align_box.width * telemetry_align_pad_realSize) / zoomSize);
		int padH = (int) ((telemetry_align_box.height * telemetry_align_pad_realSize) / zoomSize);
		
		//pad drawing
		g.drawRect(window.log_box.width + boxStartW - padW, boxStartH - padH, 2*padW, 2*padH);
		g.drawLine(window.log_box.width + boxStartW, boxStartH - padH, window.log_box.width + boxStartW, boxStartH + padH);
		g.drawLine(window.log_box.width + boxStartW - padW, boxStartH, window.log_box.width + boxStartW + padH, boxStartH);
		String finalDistance = new DecimalFormat("#0.0").format(window.telemetry_booster_finalDistance.Magnitude());
		
		g.drawString(finalDistance, window.log_box.width + boxStartW + telemetry_pad_distance_pos.x - g.getFontMetrics().stringWidth(finalDistance)/2, boxStartH + padH + telemetry_pad_distance_pos.y);
		
		int rocketW = (int) (telemetry_align_box.width * telemetry_align_rocket_realSize / zoomSize);
		int rocketH = (int) (telemetry_align_box.height * telemetry_align_rocket_realSize / zoomSize);
		int rocketPosW = (int) (telemetry_align_box.width * window.telemetry_booster_finalDistance.x / zoom);
		int rocketPosH = (int) (telemetry_align_box.height * window.telemetry_booster_finalDistance.y / zoom);
		
		int brakeW = (int) (telemetry_align_box.width * telemetry_brakes_realWidth / zoomSize);
		int brakeH = (int) (telemetry_align_box.height * telemetry_brakes_realHeight / zoomSize);
		int brakeDist = (int) (telemetry_align_box.width * telemetry_brakes_realDistance / zoomSize);
		
		//rocket drawing
		drawRocketWithBrakes(g, window.log_box.width + boxStartW + rocketPosW, boxStartH - rocketPosH, rocketW, rocketH, brakeDist, brakeW, brakeH);
	}
}
 
Example 20
Project: jdk8u-jdk   File: RepaintArea.java   View Source Code Vote up 4 votes
/**
 * Invokes paint and update on target Component with optimal
 * rectangular clip region.
 * If PAINT bounding rectangle is less than
 * MAX_BENEFIT_RATIO times the benefit, then the vertical and horizontal unions are
 * painted separately.  Otherwise the entire bounding rectangle is painted.
 *
 * @param   target Component to <code>paint</code> or <code>update</code>
 * @since   1.4
 */
public void paint(Object target, boolean shouldClearRectBeforePaint) {
    Component comp = (Component)target;

    if (isEmpty()) {
        return;
    }

    if (!comp.isVisible()) {
        return;
    }

    RepaintArea ra = this.cloneAndReset();

    if (!subtract(ra.paintRects[VERTICAL], ra.paintRects[HORIZONTAL])) {
        subtract(ra.paintRects[HORIZONTAL], ra.paintRects[VERTICAL]);
    }

    if (ra.paintRects[HORIZONTAL] != null && ra.paintRects[VERTICAL] != null) {
        Rectangle paintRect = ra.paintRects[HORIZONTAL].union(ra.paintRects[VERTICAL]);
        int square = paintRect.width * paintRect.height;
        int benefit = square - ra.paintRects[HORIZONTAL].width
            * ra.paintRects[HORIZONTAL].height - ra.paintRects[VERTICAL].width
            * ra.paintRects[VERTICAL].height;
        // if benefit is comparable with bounding box
        if (MAX_BENEFIT_RATIO * benefit < square) {
            ra.paintRects[HORIZONTAL] = paintRect;
            ra.paintRects[VERTICAL] = null;
        }
    }
    for (int i = 0; i < paintRects.length; i++) {
        if (ra.paintRects[i] != null
            && !ra.paintRects[i].isEmpty())
        {
            // Should use separate Graphics for each paint() call,
            // since paint() can change Graphics state for next call.
            Graphics g = comp.getGraphics();
            if (g != null) {
                try {
                    g.setClip(ra.paintRects[i]);
                    if (i == UPDATE) {
                        updateComponent(comp, g);
                    } else {
                        if (shouldClearRectBeforePaint) {
                            g.clearRect( ra.paintRects[i].x,
                                         ra.paintRects[i].y,
                                         ra.paintRects[i].width,
                                         ra.paintRects[i].height);
                        }
                        paintComponent(comp, g);
                    }
                } finally {
                    g.dispose();
                }
            }
        }
    }
}