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

The following are Jave code examples for showing how to use clipRect() 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: brModelo   File: InspectorItemSeparador.java   View Source Code Vote up 5 votes
@Override
protected void paintBase(Graphics2D g) {
    if (endOFF) {
        return;
    }
    setArea(null);
    Rectangle r = this.getBounds();
    g.setColor(Color.GRAY);
    g.drawRoundRect(0, 0, r.width - 1, r.height - 1, 10, 10);

    g.setColor(Color.lightGray);
    g.drawRoundRect(5, 5, r.height - 10, r.height - 10, 4,4);

    int met = (r.height - 11) / 2;
    g.setColor(Color.black);
    g.drawLine(7, 6 + met, r.height - 7, 6 + met);
    if ('+' == getEstado()) {
        g.drawLine(6 + met, 7, 6 + met, r.height - 7);
    }

    g.setColor(Color.BLACK);
    Rectangle bkp = g.getClipBounds();
    g.clipRect(0, 0, r.width - 1, r.height);
    if (isSelecionado()) {
        g.setFont(new Font(this.getFont().getFontName(), Font.BOLD, getFont().getSize()));
        g.drawRoundRect(0, 0, r.width - 1, r.height - 1, 10, 10);
    }
    int tmp = (r.width - g.getFontMetrics().stringWidth(getTexto())) / 2;

    g.drawString(getTexto(), tmp, (int) (r.height * 0.72));
    g.setClip(bkp);
}
 
Example 2
Project: geomapapp   File: Ras2ToJPG.java   View Source Code Vote up 5 votes
public void saveJPEG(OutputStream out) throws IOException {
	if(image==null) throw new IOException("no image loaded");
	Rectangle rect = new Rectangle();
	Dimension dim = getPreferredSize();
	
	xAvg = 1;
	yAvg = 1;
	dim = getPreferredSize();
	rect.width = dim.width-rect.x;
	rect.height = dim.height-rect.y;
	
	BufferedImage im = new BufferedImage( rect.width, rect.height, BufferedImage.TYPE_INT_RGB);
	Graphics2D g2 = im.createGraphics();
	g2.translate(-rect.x, -rect.y);
	if(border != null) {
		Dimension size = getPreferredSize();
		Rectangle bounds = new Rectangle(0, 0, size.width, size.height);
		if(rect.contains(bounds)) {
			rect=bounds;
			g2.clipRect(rect.x, rect.y, rect.width, rect.height);
		}
		Insets ins = border.getBorderInsets();
		border.paintBorder(null, g2, rect.x, rect.y, rect.width, rect.height);
		g2.translate(ins.left, ins.top);
		g2.clipRect(rect.x, rect.y, 
			rect.width-ins.left-ins.right, 
			rect.height-ins.top-ins.bottom);
	}
	g2.drawImage(image.getImage(), 0, 0, null);
	g2.translate( rect.x, rect.y );
	//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
	//encoder.encode(im);
	ImageIO.write(im, "JPEG", out);
	out.flush();
}
 
Example 3
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);
}
 
Example 4
Project: nativehtml   File: AbstractSwingComponentElement.java   View Source Code Vote up 4 votes
private void drawBackground(Graphics2D g2d, int x, int y, int w, int h) {
  CssStyleDeclaration style = getComputedStyle();
  if (style.isSet(CssProperty.BACKGROUND_COLOR)) {
    g2d.setColor(createColor(style.getColor(CssProperty.BACKGROUND_COLOR)));
    g2d.fillRect(x, y, w, h);
  }

  if (!style.isSet(CssProperty.BACKGROUND_IMAGE)) {
    return;
  }

  String bgImage = style.getString(CssProperty.BACKGROUND_IMAGE);
  Image image = ((SwingPlatform) getOwnerDocument().getPlatform()).getImage(this, getOwnerDocument().getUrl().resolve(bgImage));
  
  if (image == null) {
    return;
  }

  Shape savedClip = g2d.getClip(); 
  g2d.clipRect(x, y, w, h);
  CssEnum repeat = style.getEnum(CssProperty.BACKGROUND_REPEAT);
     int bgY = 0;
     int bgX = 0;
     if (repeat == CssEnum.REPEAT_Y || repeat == CssEnum.REPEAT) {
       do {
         if (repeat == CssEnum.REPEAT) {
           int currentBgX = bgX;
           do {
             g2d.drawImage(image, x + currentBgX, y + bgY, null);
             currentBgX += image.getWidth(null);
           } while (currentBgX < w);
         } else {
           g2d.drawImage(image, x + bgX, y + bgY, null);
         }
         bgY += image.getHeight(null);
       } while (bgY < h);
     } else if (repeat == CssEnum.REPEAT_X) {
       do {
         g2d.drawImage(image, x + bgX, y + bgY, null);
         bgX += image.getWidth(null);
       } while (bgX < w);
     } else {
       g2d.drawImage(image, x + bgX, y + bgY, null);
     }
     
     g2d.setClip(savedClip);
}
 
Example 5
Project: brModelo   File: InspectorItemCor.java   View Source Code Vote up 4 votes
@Override
    protected void paintBase(Graphics2D g) {
        Rectangle r = this.getBounds();
        int esq = (int) (r.width * Criador.getDivisor());
        setArea(new Rectangle(esq -2, 0, 4, r.height - 1));

        //int dir = r.width - esq;
        if (!isSelecionado()) {
            g.setColor(Color.GRAY);
            g.drawRoundRect(0, 0, r.width - 1, r.height - 1, 10, 10);
            g.drawLine(esq, 0, esq, r.height - 1);

            g.setColor(Color.BLACK);
            
            getCorParaTexto(g);
            
            Rectangle bkp = g.getClipBounds();
            g.clipRect(0, 0, esq - 1, r.height);
            g.drawString(getTexto(), (Criador.espaco * 2) + 1, (int) (r.height * 0.72));

            g.setClip(bkp);
            int re = esq + r.height -5;
            g.setColor(CanEdit() ? Color.BLACK : Color.LIGHT_GRAY);
            g.fillRect(esq + 4, 4, r.height - 9, r.height - 9);
            try {
                Color c = util.Utilidades.StringToColor(getTransValor());//new Color(Integer.parseInt(getTransValor()));
                g.setColor(c);
            } catch (Exception e) {
            }
            Color tmpc = g.getColor();
            String bonito = Integer.toString(tmpc.getRed()) + ", " + Integer.toString(tmpc.getGreen()) + ", " +Integer.toString(tmpc.getBlue())+ ", " +Integer.toString(tmpc.getAlpha());
            
            if (CanEdit()) {
                g.fillRect(esq + 5, 5, r.height - 10, r.height -10);
            }
            
//            g.setColor(CanEdit() ? Color.BLACK : Color.LIGHT_GRAY);
//            g.drawRect(tmp + 4, 4, r.height - 9, r.height -9);
            
            g.clipRect(re, 0, esq - 1, r.height);
            
            getCorParaTexto(g);

            g.drawString(bonito, re + (Criador.espaco * 2) + 1, (int) (r.height * 0.72));

            g.setClip(bkp);

        } else {
            super.paintBase(g);
        }
    }
 
Example 6
Project: brModelo   File: Legenda.java   View Source Code Vote up 4 votes
@Override
    public void DoPaint(Graphics2D g) {
        super.DoPaint(g);
        Rectangle rec = getArea();
        Color bkpc = g.getColor();

        g.setColor(getBorderColor());

        g.drawRect(rec.x, rec.y, rec.width, rec.height);

        Rectangle bkp = g.getClipBounds();
        g.clipRect(rec.x, rec.y, rec.width, rec.height);

        Font fn = getFont();
        Font font = new Font(fn.getFontName(), fn.getStyle(), fn.getSize() - 2);

        fn = g.getFont();
        g.setFont(font);

        altura = g.getFontMetrics().getHeight() + g.getFontMetrics().getDescent();
        alturaTitulo = altura + altura / 2;

        Composite originalComposite = g.getComposite();
        float alfa = 0.8f;
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alfa));

        if (getTipo() == TipoLegenda.tpObjetos) {
            altura = Math.max(32, altura);
        }

        int posi = altura + alturaTitulo + rec.y;
        final int lft = rec.x + 2;

        for (ItemDeLegenda it : getItens()) {

            if (it.isSelecionada()) {
                g.setColor(isDisablePainted()? disabledColor : new Color(204, 204, 255, 50));
                g.fillRect(lft, posi - altura - 2, getWidth(), altura + 4);
            }
            g.setColor(isDisablePainted()? disabledColor : it.cor);
            int moveleft;
            switch (getTipo()) {
                case tpLinhas:
                    moveleft = 3 * altura;
                    g.fillRoundRect(lft, posi - (altura / 2) - 2, moveleft - 2, 4, 2, 2);
                    g.setColor(bkpc);
                    g.drawString(it.texto, lft + moveleft, posi - 6);
                    break;
                case tpObjetos:
                    ImageIcon img = Editor.fromControler().getImagemNormal(getMaster().getCassesDoDiagrama()[it.getTag()].getSimpleName());
                    g.drawImage(util.TratadorDeImagens.ReColorBlackImg(img, it.getCor()), lft, posi - altura, null);
                    moveleft = altura + 2;
                    g.drawString(it.texto, lft + moveleft, posi - altura / 2 + 6);
                    break;
                default:
                    moveleft = altura;
                    g.fillRect(lft, posi - altura, altura - 4, altura - 4);
                    g.setColor(bkpc);
                    g.drawRect(lft, posi - altura, altura - 4, altura - 4);
                    g.drawString(it.texto, lft + moveleft, posi - 6);
            }
            it.Area = new Point(posi - altura - 2, altura + 4);
            posi += altura + 4;
        }

        g.setComposite(originalComposite);

//        g.setColor(Color.LIGHT_GRAY);
//        g.drawLine(lft - 1, posi - altura - 2, getLeft() + getWidth() - 1, posi - altura - 2);
        g.setClip(bkp);
        g.setFont(fn);
    }
 
Example 7
Project: geomapapp   File: XYGraph.java   View Source Code Vote up 4 votes
public void paintComponent( Graphics graphics ) {
	line = null;
	Dimension dim = getPreferredSize();
	Graphics2D g = (Graphics2D) graphics;
	if(!printing) g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
					RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	Rectangle r = getVisibleRect();
	if( !imaging ) {
		g.setColor(Color.white);
		g.fill( r );
	}
	Rectangle2D.Double bounds = new Rectangle2D.Double();
	Insets ins = axes.getInsets();
	double w = (double)(r.width - ins.left - ins.right);
	double h = (double)(r.height - ins.top - ins.bottom);
	double sX, sY;
	if( tracksWidth  || scPane==null) {
		bounds.x = xRange[0];
		bounds.width = xRange[1]-xRange[0];
		sX = w / bounds.width;
	} else {
		bounds.x = xRange[0] + (double)(r.x) / (xScale*zoom);
		bounds.width = w / (xScale*zoom);
		sX = xScale*zoom;
	}
	if( tracksHeight || scPane==null ) {
		bounds.y = yRange[1];
		bounds.height = yRange[0]-yRange[1];
		sY = h / bounds.height;
	} else {
		bounds.y = yRange[1] + (double)(r.y) / (yScale*zoom);
		bounds.height = (double)(r.height - ins.top - ins.bottom) / (yScale*zoom);
		sY = yScale*zoom;
	}
	if(printing) {
		double scale = (r.getWidth()-ins.left-ins.right)
			/(printRect.getWidth()-ins.top-ins.bottom);
		sX /= scale;
		sY /= scale;
		r = printRect;
	}
	axes.drawAxes( g, bounds, r );
	g.clipRect( r.x+ins.left, r.y+ins.top, 
		r.width-ins.left-ins.right, 
		r.height-ins.top-ins.bottom );
	g.translate( r.x+ins.left, r.y+ins.top );
	xy.plotXY( g, bounds, sX, sY, dataIndex );
}