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

The following are Jave code examples for showing how to use drawRect() 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: rapidminer   File: CompositeButtonPainter.java   View Source Code Vote up 6 votes
/**
 * Draws the component border.
 *
 * @param graphics
 *            the graphics context
 */
void paintBorder(Graphics graphics) {
	Graphics2D g = (Graphics2D) graphics.create();
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

	g.setColor(Colors.BUTTON_BORDER);

	int radius = RapidLookAndFeel.CORNER_DEFAULT_RADIUS;
	switch (position) {
		case SwingConstants.LEFT:
			g.drawRoundRect(0, 0, button.getWidth() + radius, button.getHeight() - 1, radius, radius);
			break;
		case SwingConstants.CENTER:
			g.drawRect(0, 0, button.getWidth() + radius, button.getHeight() - 1);
			break;
		default:
			g.drawRoundRect(-radius, 0, button.getWidth() + radius - 1, button.getHeight() - 1, radius, radius);
			g.drawLine(0, 0, 0, button.getHeight());
			break;
	}
}
 
Example 2
Project: geomapapp   File: MMapServer.java   View Source Code Vote up 6 votes
private static BufferedImage drawTileName(BufferedImage image, String name) {
	BufferedImage i2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
	Graphics2D g2 = i2.createGraphics();
	g2.drawImage(image, null, 0, 0);
	g2.setColor(Color.red);
	g2.setStroke(new BasicStroke(4));
	g2.drawRect(8, 8, image.getWidth() - 8, image.getHeight() - 8);

	g2.setColor(Color.RED);
	g2.setFont(g2.getFont().deriveFont(Font.BOLD).deriveFont(13f));
	FontMetrics fm = g2.getFontMetrics();
	String[] parts = name.split("/");
	for (int i = 0; i < parts.length; i++) {
		String s = i == parts.length - 1 ? parts[i] : parts[i] + "/";
		int x = 40 + i * 5;
		int y = 40 + i * (fm.getHeight() + 5);
		Rectangle2D rect = fm.getStringBounds(s, g2);
		g2.setColor(Color.white);
		g2.fillRect(x, y - fm.getAscent(), (int)rect.getWidth(), fm.getHeight());
		g2.setColor(Color.red);
		g2.drawString(s, x, y);
	}
	return i2;
}
 
Example 3
Project: Progetto-C   File: SheetPanel.java   View Source Code Vote up 6 votes
/**
 * @see javax.swing.JComponent#paint(java.awt.Graphics)
 */
public void paint(Graphics g1d) {
       Graphics2D g = (Graphics2D) g1d;
       
	g.setPaint(background);
	g.fillRect(0,0,getWidth(), getHeight());
	g.setColor(Color.yellow);
	g.drawRect(0,0,width,height);
	
	if (image != null) {
		g.drawImage(image, 0, 0, null);
	}
	
	g.setColor(Color.green);
	for (int i=0;i<selected.size();i++) {
		Sprite sprite = (Sprite) selected.get(i);
		
		g.drawRect(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
	}
}
 
Example 4
Project: incubator-netbeans   File: LinkButton.java   View Source Code Vote up 6 votes
@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2 = Utils.prepareGraphics( g );
    if( showBorder && !Utils.isDefaultButtons() ) {
        Border b = underline ? mouseoverBorder : regularBorder;
        b.paintBorder(this, g, 0, 0, getWidth(), getHeight());
    }
    super.paintComponent(g2);

    if( showBorder && Utils.isDefaultButtons() )
        return;

    Dimension size = getSize();
    if( hasFocus() && isEnabled() ) {
        g2.setStroke( LINK_IN_FOCUS_STROKE );
        g2.setColor( Utils.getFocusedLinkColor() );
        g2.drawRect( 0, 0, size.width - 1, size.height - 1 );
    }
}
 
Example 5
Project: marathonv5   File: MissingIcon.java   View Source Code Vote up 6 votes
public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2d = (Graphics2D) g.create();

    g2d.setColor(Color.WHITE);
    g2d.fillRect(x + 1, y + 1, width - 2, height - 2);

    g2d.setColor(Color.BLACK);
    g2d.drawRect(x + 1, y + 1, width - 2, height - 2);

    g2d.setColor(Color.RED);

    g2d.setStroke(stroke);
    g2d.drawLine(x + 10, y + 10, x + width - 10, y + height - 10);
    g2d.drawLine(x + 10, y + height - 10, x + width - 10, y + 10);

    g2d.dispose();
}
 
Example 6
Project: geomapapp   File: PhotoDisplay.java   View Source Code Vote up 6 votes
public void paint(Graphics graphics) {
		Graphics2D g = (Graphics2D)graphics;
		Dimension d = getPreferredSize();
		g.setColor(Color.black);
		g.drawRect( 0, 0, d.width - 1, d.height - 1 );
		g.setColor(backgroundC);
		g.fillRect( 1, 1, d.width - 2, d.height - 2 );
		g.setFont(new Font("SansSerif", Font.PLAIN, 9));
		Enumeration photoDepthList = photosAtDepths.keys();
		
		g.setColor(Color.black);
		int i = 0;
		while ( photoDepthList.hasMoreElements() ) {
			double depth = Double.parseDouble((String)photoDepthList.nextElement());
			int y = (int)( ( depth ) * zScale  );
//			if ( cores[i].recovered > 0 ) {
//				y += (int)( ( cores[i].recovered * 0.5 ) * zScale  );
//			}
			g.fillOval( 7, y, 6, 6);
			i++;
		}
		prevAge = -1;
	}
 
Example 7
Project: incubator-netbeans   File: CustomizableSideBar.java   View Source Code Vote up 6 votes
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    int thick = getThickness();
    if (!(g instanceof Graphics2D)) {
        return;
    }
    
    Graphics2D g2d = (Graphics2D)g.create();
    if (thick >= 1) {
        g2d.setColor(this.lineColor);
        int x2 = x + width - ((thick +1) / 2);
        g2d.drawLine(x2 - SIDEBAR_GAP_WIDTH, 
                0, x2 - SIDEBAR_GAP_WIDTH, y + height - 1);
    }
    g2d.setColor(textBkColor);
    int gap = width - SIDEBAR_GAP_WIDTH;
    g2d.drawRect(gap, 0, width - gap, height);
}
 
Example 8
Project: VASSAL-src   File: KeyBufferer.java   View Source Code Vote up 5 votes
public void draw(Graphics g, Map map) {
  if (selection != null) {
    final Graphics2D g2d = (Graphics2D) g;
    final Stroke str = g2d.getStroke();
    g2d.setStroke(new BasicStroke(thickness));
    g2d.setColor(color);
    g2d.drawRect(selection.x, selection.y, selection.width, selection.height);
    g2d.setStroke(str);
  }
}
 
Example 9
Project: Lernkartei_2017   File: Push.java   View Source Code Vote up 5 votes
@Override
protected void paintComponent(final Graphics g) {
  final Graphics2D g2d = (Graphics2D) g;
  // background
  g2d.setPaint(lpg);
  g2d.fillRect(1, 1, getWidth() - 2, getHeight() - 2);
  g2d.setColor(Color.BLACK);

  // border
  g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
}
 
Example 10
Project: incubator-netbeans   File: LinkButton.java   View Source Code Vote up 5 votes
@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2 = prepareGraphics( g );
    super.paintComponent(g2);

    Dimension size = getSize();
    if( hasFocus() && isEnabled() ) {
        g2.setStroke( LINK_IN_FOCUS_STROKE );
        g2.setColor( linkInFocusColor );
        g2.drawRect( 0, 0, size.width - 1, size.height - 1 );
    }
}
 
Example 11
Project: incubator-netbeans   File: DropTargetLayer.java   View Source Code Vote up 5 votes
private  void drawOpenSpotAtEndOfMenuBar(Graphics2D g2, JComponent mb) {
    Point mblocation = SwingUtilities.convertPoint(mb, new Point(0,0), this);
    if(mb.getComponentCount() > 0) {
        Component lastComp = mb.getComponent(mb.getComponentCount()-1);
        mblocation.x += lastComp.getX() + lastComp.getWidth();
    }
    g2.drawRect(mblocation.x+2, mblocation.y+2, mb.getHeight()-4, mb.getHeight()-4);
}
 
Example 12
Project: VASSAL-src   File: SetupStack.java   View Source Code Vote up 5 votes
private BufferedImage featherDragImage(BufferedImage src,
                                         int w, int h, int b) {
// FIXME: duplicated from PieceMover!
      final BufferedImage dst =
        ImageUtils.createCompatibleTranslucentImage(w, h);

      final Graphics2D g = dst.createGraphics();
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);

      // paint the rectangle occupied by the piece at specified alpha
      g.setColor(new Color(0xff, 0xff, 0xff, CURSOR_ALPHA));
      g.fillRect(0, 0, w, h);

      // feather outwards
      for (int f = 0; f < b; ++f) {
        final int alpha = CURSOR_ALPHA * (f + 1) / b;
        g.setColor(new Color(0xff, 0xff, 0xff, alpha));
        g.drawRect(f, f, w-2*f, h-2*f);
      }

      // paint in the source image
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN));
      g.drawImage(src, 0, 0, null);
      g.dispose();

      return dst;
    }
 
Example 13
Project: agar.io   File: EZ.java   View Source Code Vote up 5 votes
@Override public void paint(Graphics2D g2) {
  if (this.isShowing) {
    if (img == null) {
      g2.setColor(Color.BLACK);
      String err = "Failed to load image";
      int wos = EZElement.getWidthOf(err);
      int hos = EZElement.getHeightOf(err);
      g2.drawRect((int) xCenter - wos / 2 - 10, (int) yCenter - hos / 2 - 10, wos + 20, hos + 20);
      g2.drawString(err, (int) xCenter - wos / 2, (int) yCenter);
    }
    else {
      AffineTransform tmp = g2.getTransform();
      g2.setTransform(EZElement.transformHelper(this));
      if(imgHasFocus) {
        g2.drawImage(img,
            -(xbrf - xtlf)/2, -(ybrf-ytlf)/2,
            (xbrf - xtlf)/2, (ybrf-ytlf)/2,
            xtlf, ytlf,
            xbrf, ybrf, null);
      }
      else {
        g2.drawImage(img, -img.getWidth() / 2, -img.getHeight() / 2, null);
      }
      g2.setTransform(tmp);
    }
  }
}
 
Example 14
Project: ChessBot   File: ChessDrawerWindow.java   View Source Code Vote up 5 votes
@Override
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    
    g2.setColor(Color.ORANGE);
    g2.setStroke(new BasicStroke(2));

    if (x1 < x2) {
        
        int width = x2 - x1;
        int height = y2 - y1;
        
        int xCell = width / 8;
        int yCell = height / 8;
        
        int xDif = xCell;
        int yDif = yCell;
        
        for(int i = 0; i < 7; i++){
            
            g2.drawLine(x1, y1 + yCell, x2, y2 - height + yCell);
            g2.drawLine(x1 + xCell, y1, x2 - width + xCell, y2);
            
            yCell += yDif;
            xCell += xDif;
        }
        
        g2.drawRect(x1, y1, x2 - x1, y2 - y1);
    }
}
 
Example 15
Project: chordatlas   File: ColorRGBAPainter.java   View Source Code Vote up 4 votes
@Override
public void paint( Object oa, Graphics2D g2, PanMouseAdaptor ma ) {
			
	ColorRGBA col = (ColorRGBA)oa;
	
	int r = (int)(col.r * 255), 
		g = (int)(col.g * 255), 
		b = (int)(col.b * 255);
	
	float[] hsb = Color.RGBtoHSB( r,g,b, null );
	
	g2.setColor(new Color(r,g,b) );
	
	g2.drawRect( ma.toX( hsb[0] * 10), ma.toY( hsb[2] * 10), ma.toZoom( 0.1 ), ma.toZoom( 0.1 )  );
	
}
 
Example 16
Project: the-one-mdonnyk   File: NodeGraphic.java   View Source Code Vote up 4 votes
/**
 * Visualize node's location, radio ranges and connections
 * @param g2 The graphic context to draw to
 */
private void drawHost(Graphics2D g2) {
	Coord loc = node.getLocation();

	if (drawCoverage && node.isActive()) {
		ArrayList<NetworkInterface> interfaces = new ArrayList<NetworkInterface>();
		interfaces.addAll(node.getInterfaces());
		for (NetworkInterface ni : interfaces) {
			double range = ni.getTransmitRange();
			Ellipse2D.Double coverage;

			coverage = new Ellipse2D.Double(scale(loc.getX()-range),
					scale(loc.getY()-range), scale(range * 2), scale(range * 2)); 

			// draw the "range" circle
			g2.setColor(rangeColor);
			g2.draw(coverage);
		}
	}

	if (drawConnections) {
		g2.setColor(conColor);
		Coord c1 = node.getLocation();
		ArrayList<Connection> conList = new ArrayList<Connection>();
		// create a copy to prevent concurrent modification exceptions
		conList.addAll(node.getConnections());
		for (Connection c : conList) {
			Coord c2 = c.getOtherNode(node).getLocation();

			g2.drawLine(scale(c1.getX()), scale(c1.getY()),
					scale(c2.getX()), scale(c2.getY()));
		}
	}

	g2.setColor(hostColor);	// draw rectangle to host's location
	g2.drawRect(scale(loc.getX()-1),scale(loc.getY()-1),scale(2),scale(2));

	if (drawNodeName) {
		g2.setColor(hostNameColor);
		// Draw node's address next to it
		g2.drawString(node.toString(), scale(loc.getX()),
				scale(loc.getY()));
	}
}
 
Example 17
Project: incubator-netbeans   File: DropTargetLayer.java   View Source Code Vote up 4 votes
private void drawSubselectedItem(Graphics2D g2, JMenuItem item) {
    Point location = SwingUtilities.convertPoint(item, new java.awt.Point(0, 0), this);
    g2.translate(location.x, location.y);

    int iconGap = item.getIconTextGap();
    int iconLeft = getIconLeft(item);
    int iconWidth = getIconWidth(item);
    int iconHeight = getIconHeight(item);
    int iconTop = (item.getHeight() - iconHeight) / 2;
    int accelWidth = getAcceleratorWidth(item);

    int textWidth = item.getWidth() - iconLeft - iconWidth - iconGap - accelWidth;
    int accelLeft = item.getWidth() - accelWidth;

    // draw bounding boxes
    g2.setColor(Color.LIGHT_GRAY);
    //g2.drawRect(iconLeft, 0, iconWidth-1, item.getHeight()-1);
    //g2.drawRect(textLeft, 0, textWidth-1, item.getHeight()-1);
    //draw the accelerator areaa
    //g2.drawRect(accelLeft, 0, accelWidth - 1, item.getHeight() - 1);

    // draw the selection rectangles
    g2.setStroke(SELECTION_STROKE);
    g2.setColor(SELECTION_COLOR);
    switch (canvas.getCurrentSelectedPortion()) {
        case Icon:
            {
                if (item.getIcon() != null) {
                    g2.drawRect(iconLeft - 1, iconTop - 1, iconWidth + 1, iconHeight + 1);
                }
                break;
            }
        case Text:
            {
                g2.drawRect(iconLeft + iconWidth + iconGap - 1, -1, textWidth + 1, item.getHeight() + 1);
                break;
            }
        case Accelerator:
            {
                if (item instanceof javax.swing.JMenu) {
                    break;
                }
                g2.drawRect(accelLeft - 1, -1, accelWidth + 1, item.getHeight() + 1);
                break;
            }
        case All:
            {
                g2.drawRect(0, 0, item.getWidth() - 1, item.getHeight() - 1);
            }
    }
    g2.translate(-location.x, -location.y);
}
 
Example 18
Project: incubator-netbeans   File: FoldView.java   View Source Code Vote up 4 votes
@Override
public void paint(Graphics2D g, Shape alloc, Rectangle clipBounds) {
    Rectangle2D.Double allocBounds = ViewUtils.shape2Bounds(alloc);
    if (allocBounds.intersects(clipBounds)) {
        Font origFont = g.getFont();
        Color origColor = g.getColor();
        Color origBkColor = g.getBackground();
        Shape origClip = g.getClip();
        try {
            // Leave component font
            
            g.setBackground(getBackgroundColor());

            int xInt = (int) allocBounds.getX();
            int yInt = (int) allocBounds.getY();
            int endXInt = (int) (allocBounds.getX() + allocBounds.getWidth() - 1);
            int endYInt = (int) (allocBounds.getY() + allocBounds.getHeight() - 1);
            g.setColor(getBorderColor());
            g.drawRect(xInt, yInt, endXInt - xInt, endYInt - yInt);
            
            g.setColor(getForegroundColor());
            g.clearRect(xInt + 1, yInt + 1, endXInt - xInt - 1, endYInt - yInt - 1);
            g.clip(alloc);
            TextLayout textLayout = getTextLayout();
            if (textLayout != null) {
                EditorView.Parent parent = (EditorView.Parent) getParent();
                float ascent = parent.getViewRenderContext().getDefaultAscent();
                String desc = fold.getDescription(); // For empty desc a single-space text layout is returned
                float x = (float) (allocBounds.getX() + EXTRA_MARGIN_WIDTH);
                float y = (float) allocBounds.getY();
                if (desc.length() > 0) {
                    
                    textLayout.draw(g, x, y + ascent);
                }
            }
        } finally {
            g.setClip(origClip);
            g.setBackground(origBkColor);
            g.setColor(origColor);
            g.setFont(origFont);
        }
    }
}
 
Example 19
Project: openjdk-jdk10   File: Margins.java   View Source Code Vote up 4 votes
public int print(Graphics g, PageFormat pf, int page)
    throws PrinterException {

    if (page > 0) {
        return NO_SUCH_PAGE;
    }
    int ix = (int)pf.getImageableX();
    int iy = (int)pf.getImageableY();
    int iw = (int)pf.getImageableWidth();
    int ih = (int)pf.getImageableHeight();
    System.out.println("ix="+ix+" iy="+iy+" iw="+iw+" ih="+ih);
    if ((ix < 0) || (iy < 0)) {
        throw new RuntimeException("Imageable x or y is a negative value.");
    }


    Paper paper = pf.getPaper();
    int wid = (int)paper.getWidth();
    int hgt = (int)paper.getHeight();
    System.out.println("wid="+wid+" hgt="+hgt);
    /*
     * If imageable width/height is -ve, then print was done with 1" margin
     * e.g. ix=72 iy=72 iw=451 ih=697 and paper wid=595
     * but with fix, we get print with hardware margin e.g.
     * ix=12, iy=12, iw=571, ih=817
     */
    if ((wid - iw > 72) || (hgt - ih > 72)) {
        throw new RuntimeException("Imageable width or height is negative value");
    }
    if ((ix+iw > wid) || (iy+ih > hgt)) {
        throw new RuntimeException("Printable width or height "
                + "exceeds paper width or height.");
    }
    // runtime checking to see if the margins/printable area
    // correspond to the entire size of the paper, for now, make it pass
    // as for linux, the hwmargin is not taken into account - bug6574279
    if (ix == 0 && iy == 0 && (ix+iw == wid) && (iy+ih == hgt)) {
        return PAGE_EXISTS;
    }

    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(ix, iy);
    g2d.setColor(Color.black);
    g2d.drawRect(1, 1, iw-2, ih-2);

    return PAGE_EXISTS;
}
 
Example 20
Project: incubator-netbeans   File: CoverageBar.java   View Source Code Vote up 4 votes
@Override
protected void paintComponent(Graphics g) {

    if (!(g instanceof Graphics2D)) {
        return;
    }

    int width = getWidth();
    int barRectWidth = width;
    int height = getHeight();
    int barRectHeight = height;

    if (barRectWidth <= 0 || barRectHeight <= 0) {
        return;
    }

    int amountFull = (int) (barRectWidth * coveragePercentage / 100.0f);

    Graphics2D g2 = (Graphics2D) g;

    g2.setColor(getBackground());

    Color notCoveredLight = NOT_COVERED_LIGHT;
    Color notCoveredDark = NOT_COVERED_DARK;
    Color coveredLight = COVERED_LIGHT;
    Color coveredDark = COVERED_DARK;
    if (emphasize) {
        coveredDark = coveredDark.darker();
    } else if (selected) {
        coveredLight = coveredLight.brighter();
        coveredDark = coveredDark.darker();
    }
    if (emphasize) {
        notCoveredDark = notCoveredDark.darker();
    } else if (selected) {
        notCoveredLight = notCoveredLight.brighter();
        notCoveredDark = notCoveredDark.darker();
    }

    g2.setPaint(new GradientPaint(0, 0, notCoveredLight,
            0, height / 2, notCoveredDark));
    g2.fillRect(amountFull, 1, width - 1, height / 2);
    g2.setPaint(new GradientPaint(0, height / 2, notCoveredDark,
            0, 2 * height, notCoveredLight));
    g2.fillRect(amountFull, height / 2, width - 1, height / 2);

    g2.setColor(getForeground());

    g2.setPaint(new GradientPaint(0, 0, coveredLight,
            0, height / 2, coveredDark));
    g2.fillRect(1, 1, amountFull, height / 2);
    g2.setPaint(new GradientPaint(0, height / 2, coveredDark,
            0, 2 * height, coveredLight));
    g2.fillRect(1, height / 2, amountFull, height / 2);

    Rectangle oldClip = g2.getClipBounds();
    if (coveragePercentage > 0.0f) {
        g2.setColor(coveredDark);
        g2.clipRect(0, 0, amountFull + 1, height);
        g2.drawRect(0, 0, width - 1, height - 1);
    }
    if (coveragePercentage < 100.0f) {
        g2.setColor(notCoveredDark);
        g2.setClip(oldClip);
        g2.clipRect(amountFull, 0, width, height);
        g2.drawRect(0, 0, width - 1, height - 1);
    }
    g2.setClip(oldClip);

    g2.setFont(getFont());
    paintDropShadowText(g2, barRectWidth, barRectHeight);
}