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

The following are Jave code examples for showing how to use setColor() 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: openjdk-jdk10   File: bug8132119.java   View Source Code Vote up 7 votes
private static void testDrawEmptyString() {
    JLabel label = new JLabel();
    BufferedImage buffImage = createBufferedImage(50, 50);
    Graphics2D g2 = buffImage.createGraphics();
    g2.setColor(DRAW_COLOR);
    BasicGraphicsUtils.drawString(null, g2, null, 0, 0);
    BasicGraphicsUtils.drawString(label, g2, null, 0, 0);
    BasicGraphicsUtils.drawString(null, g2, "", 0, 0);
    BasicGraphicsUtils.drawString(label, g2, "", 0, 0);
    BasicGraphicsUtils.drawStringUnderlineCharAt(null, g2, null, 3, 0, 0);
    BasicGraphicsUtils.drawStringUnderlineCharAt(label, g2, null, 3, 0, 0);
    BasicGraphicsUtils.drawStringUnderlineCharAt(null, g2, "", 3, 0, 0);
    BasicGraphicsUtils.drawStringUnderlineCharAt(label, g2, "", 3, 0, 0);
    g2.dispose();
    checkImageIsEmpty(buffImage);
}
 
Example 2
Project: litiengine   File: CombatEntityVision.java   View Source Code Vote up 6 votes
@Override
public void renderFogOfWar(final Graphics2D g) {
  if (this.fogOfWar == null) {
    return;
  }

  final AffineTransform oldTransform = g.getTransform();
  final AffineTransform at = new AffineTransform();
  at.scale(Game.getCamera().getRenderScale(), Game.getCamera().getRenderScale());
  at.translate(Game.getCamera().getPixelOffsetX(), Game.getCamera().getPixelOffsetY());

  g.setTransform(at);
  g.setColor(FogOfWarColor);
  g.fill(this.fogOfWar);
  g.setTransform(oldTransform);
}
 
Example 3
Project: spacesettlers   File: BeaconGraphics.java   View Source Code Vote up 6 votes
public void draw(Graphics2D g) {
      float radius = Beacon.BEACON_RADIUS;
      float diameter = Beacon.BEACON_RADIUS * 2;
      
      Ellipse2D.Double shape = new Ellipse2D.Double(drawLocation.getX() - radius,
      		drawLocation.getY() - radius, diameter, diameter);

      g.setColor(BEACON_COLOR);
      g.fill(shape);

      g.setStroke(JSpaceSettlersComponent.THICK_STROKE);
      g.setColor(BEACON_LINE_COLOR);
      g.draw(shape);

      // add an E to make it clear it is an energy beacon
g.setPaint(Color.BLACK);
g.drawString("E", (int) drawLocation.getX()-3, (int) drawLocation.getY() + 4);

  }
 
Example 4
Project: jaer   File: LabyrinthTableTiltControllerGUI.java   View Source Code Vote up 6 votes
@Override
public void paint(Graphics g) {
    final int r = 6;
    super.paint(g);

    // paint cross hairs to show center
    Graphics2D g2 = (Graphics2D) calibrationPanel.getGraphics();
    int w = calibrationPanel.getWidth(), h = calibrationPanel.getHeight();
    g2.setColor(Color.gray);
    g2.drawLine(w / 2, 0, w / 2, h);
    g2.drawLine(0, h / 2, w, h / 2);
    g2.setColor(Color.red) ;
    float x=w/2+w/2*currentPanTiltRad.x/controller.getTiltLimitRad();
    float y=h/2+h/2*(-currentPanTiltRad.y/controller.getTiltLimitRad());
    final int s=20;
    g2.drawOval((int)x-s/2, (int)y-s/2, s, s);

    trajectory.paint();
}
 
Example 5
Project: ramus   File: DFDSRole.java   View Source Code Vote up 6 votes
@Override
public void paint(Graphics2D g) {

    g.setColor(function.getBackground());
    final Rectangle2D rect = movingArea.getBounds(getBounds());

    RoundRectangle2D.Double rec = new RoundRectangle2D.Double(rect.getX(),
            rect.getY(), rect.getWidth(), rect.getHeight(),
            movingArea.getIDoubleOrdinate(4),
            movingArea.getIDoubleOrdinate(4));

    g.fill(rec);
    g.setFont(function.getFont());
    paintText(g);

    paintBorder(g);

    final Stroke tmp = g.getStroke();
    g.draw(rec);
    g.setStroke(tmp);
    paintTringle(g);

}
 
Example 6
Project: UtilsMaven   File: VerificationCodeImage.java   View Source Code Vote up 6 votes
/**
 * 随机像图片中画短直线
 *
 * @param image 被画直线的图片
 */
private void drawLine(BufferedImage image) {
    Random r = new Random();
    Graphics2D g2 = (Graphics2D) image.getGraphics();
    int max = this.textLength * this.linePower;
    for (int i = 0; i < max; i++) {
        int x1 = r.nextInt(w);
        int y1 = r.nextInt(h);
        int x2 = r.nextInt(w);
        int y2 = r.nextInt(h);
        g2.setStroke(new BasicStroke(1.5F));
        g2.setColor(this.randomColor());
        g2.drawLine(x1, y1, x2, y2);
    }
    g2.dispose();
}
 
Example 7
Project: openjdk-jdk10   File: IncorrectClipXorModeSW2Surface.java   View Source Code Vote up 6 votes
private static BufferedImage getBufferedImage(int sw) {
    final BufferedImage bi = new BufferedImage(sw, sw, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.setColor(Color.RED);
    g2d.fillRect(0, 0, sw, sw);
    g2d.dispose();

    final DataBuffer db = bi.getRaster().getDataBuffer();
    if (db instanceof DataBufferInt) {
        ((DataBufferInt) db).getData();
    } else if (db instanceof DataBufferShort) {
        ((DataBufferShort) db).getData();
    } else if (db instanceof DataBufferByte) {
        ((DataBufferByte) db).getData();
    } else {
        try {
            bi.setAccelerationPriority(0.0f);
        } catch (final Throwable ignored) {
        }
    }
    return bi;
}
 
Example 8
Project: openjdk-jdk10   File: GlyphVectorOutline.java   View Source Code Vote up 5 votes
public static void writeImage(File fontFile, File outputFile, String value) throws Exception {
    BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, image.getWidth(), image.getHeight());
    g.setColor(Color.BLACK);

    Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
    font = font.deriveFont(Font.PLAIN, 72f);
    FontRenderContext frc = new FontRenderContext(null, false, false);
    GlyphVector gv = font.createGlyphVector(frc, value);
    g.drawGlyphVector(gv, 10, 80);
    g.fill(gv.getOutline(10, 180));
    ImageIO.write(image, "png", outputFile);
}
 
Example 9
Project: Pogamut3   File: TimeTicsWidget.java   View Source Code Vote up 5 votes
@Override
protected void
        paintWidget() {
    int secondsPerTick = 10;
    int secTickHeight = 6;

    Graphics2D g = getGraphics();
    g.setColor(getForeground());

    int ticksNum = (int) (totalTime / (1000 * secondsPerTick));

    Stroke formerStroke = g.getStroke();
    g.setStroke(lineStroke);

    // draw main line
    g.drawLine(0, 0, (int) (totalTime / TLWidget.zoomFactor), 0);

    // draw ticks
    for (int tick = 0; tick <= ticksNum; tick++) {
        int x = tick * secondsPerTick * 1000 / TLWidget.zoomFactor;
        g.drawLine(x, 0, x, majorTickHeight);

        // draw label
        String time = ((tick * secondsPerTick) / 60) + ":" + ((tick * secondsPerTick) % 60);
        //g.drawString(time, x, majorTickHeight);

        Rectangle2D r = g.getFontMetrics().getStringBounds(time, g);
        g.drawString(time, x - (float)r.getWidth() * 0.5f, majorTickHeight + (float)(r.getHeight()));

        // draw minor ticks, per seconds
        int lastSecTick = (int) (totalTime / 1000 < (tick + 1) * secondsPerTick - 1 ? totalTime / 1000 : (tick+1) * secondsPerTick - 1);
        for (int secTick = tick * secondsPerTick + 1; secTick <= lastSecTick; secTick++) {
            int secX = secTick * 1000/TLWidget.zoomFactor;
            g.drawLine(secX, 0, secX, secTickHeight);
        }
    }

    g.setStroke(formerStroke);
}
 
Example 10
Project: geomapapp   File: MGGData.java   View Source Code Vote up 5 votes
public void draw( Graphics2D g ) {
	Rectangle area = g.getClipBounds();
	currentPoint = null;
	if( !intersects(area) ) return;
	Color color = g.getColor();
	g.setColor( OFF_COLOR );
	GeneralPath path = new GeneralPath();
	float offset = (float)this.offset;
	for( int seg=0 ; seg<cptIndex.length ; seg++ ) {
		path.moveTo( offset+cptX[seg][0], cptY[seg][0] );
		for( int i=0 ; i<cptIndex[seg].length ; i++ ) {
			path.lineTo( offset+cptX[seg][i], cptY[seg][i] );
		}
	}
	g.draw(path);
	double wrap = map.getWrap();
	if(wrap>0) {
		AffineTransform xform = g.getTransform();
		offset += (float)wrap;
		while( mapBounds.getX()+(double)offset < area.getX()+area.getWidth() ) {
			g.translate( (double)wrap, 0.d );
			g.draw(path);
			offset += (float)wrap;
		}
		g.setTransform( xform );
	}
	g.setColor( ON_COLOR );
	drawCurrentSeg(g, true);
	g.setColor( color );
}
 
Example 11
Project: geomapapp   File: FMSDraw.java   View Source Code Vote up 5 votes
public static void drawCoOrdinates(double[]x,double[]y,int size, Graphics2D g){

		GeneralPath path = new GeneralPath();
		path.moveTo(x[0],-y[0]);

		for(int j=1;j<(size+1);j++){
			path.lineTo(x[j], -y[j]);
		}
		path.closePath();
		g.setColor(Color.BLACK);
		g.fill(path);

	}
 
Example 12
Project: rapidminer   File: ImagePanel.java   View Source Code Vote up 5 votes
@Override
public void paintComponent(Graphics graphics) {
	super.paintComponent(graphics);
	Graphics2D g = (Graphics2D) graphics;
	g.setColor(Color.WHITE);
	g.fillRect(0, 0, getWidth(), getHeight());

	// determine if to use low res image (if available)
	Image imageToUse = image;
	if (minDimension != null) {
		double panelWidth = getWidth();
		double panelHeight = getHeight();
		if (panelWidth < minDimension.getWidth() || panelHeight < minDimension.getHeight()) {
			imageToUse = imageLowResolution;
		}
	}

	if (imageToUse != null) {
		if (!strechImage) {
			if (anchor == VerticalAnchor.BOTTOM) {
				g.drawImage(imageToUse, 0, getHeight() - imageToUse.getHeight(null), imageToUse.getWidth(null),
						imageToUse.getHeight(null), this);
			} else {
				g.drawImage(imageToUse, 0, 0, imageToUse.getWidth(null), imageToUse.getHeight(null), this);
			}
		} else {
			g.drawImage(imageToUse, 0, 0, getWidth(), getHeight(), this);
		}
	}
	paintChildren(graphics);
}
 
Example 13
Project: rapidminer   File: CardButton.java   View Source Code Vote up 5 votes
protected void paintComponent(Graphics graphics) {
    SwingTools.disableClearType(this);
    ((Graphics2D)graphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Graphics2D g = (Graphics2D)graphics.create();
    ButtonModel model = this.getModel();
    if(model.isEnabled() && (model.isRollover() || model.isSelected())) {
        g.setColor(this.colorHover);
        g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
    }

    g.dispose();
    super.paintComponent(graphics);
}
 
Example 14
Project: KeysPerSecond   File: BasePanel.java   View Source Code Vote up 5 votes
@Override
public void paintComponent(Graphics g1) {
	Graphics2D g = (Graphics2D) g1;

	g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, Main.config.getBackgroundOpacity()));
	g.setColor(Main.config.getBackgroundColor());
	g.fillRect(0, 0, this.getWidth(), this.getHeight());
	g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, Main.config.getForegroundOpacity()));
	g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

	BufferedImage colorImage = isActive() ? ColorManager.pressed : ColorManager.unpressed;
	g.drawImage(colorImage, 2, 2, this.getWidth() - 2, this.getHeight() - 2, 0, 0, colorImage.getWidth(), colorImage.getHeight(), this);

	g.setColor(isActive() ? Main.config.getBackgroundColor() : Main.config.getForegroundColor());

	String titleString = getTitle();
	Font titleFont = Main.config.mode.getTitleFont(titleString);
	Point namePos = Main.config.mode.getTitleDrawPosition(g, this, titleString, titleFont);
	g.setFont(titleFont);
	g.drawString(titleString, namePos.x, namePos.y);

	String valueString = getValue();
	Font valueFont = Main.config.mode.getValueFont(valueString);
	Point keyCountPos = Main.config.mode.getValueDrawPosition(g, this, valueString, valueFont);
	g.setFont(valueFont);
	g.drawString(valueString, keyCountPos.x, keyCountPos.y);
}
 
Example 15
Project: openjdk-jdk10   File: MisplacedBorder.java   View Source Code Vote up 5 votes
/**
 * Draws standard JMenuBar and border on top of it.
 */
private BufferedImage step2(final JMenuBar menubar) {
    final BufferedImage bi2 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);
    final Graphics2D g2d2 = bi2.createGraphics();
    g2d2.scale(2, 2);
    g2d2.setColor(Color.RED);
    g2d2.fillRect(0, 0, W, H);
    menubar.paintAll(g2d2);
    menubar.getBorder().paintBorder(menubar, g2d2, menubar.getX(), menubar
            .getX(), menubar.getWidth(), menubar.getHeight());
    g2d2.dispose();
    return bi2;
}
 
Example 16
Project: DicomViewer   File: PaletteSelector.java   View Source Code Vote up 5 votes
private void highlightColor(Graphics2D g2, int colorId, Color color, int zoom) {
    if ((colorId < 0) || (colorId >= palette.length)) {
        return;
    }

    int sx = getSizeX();
    int sy = getSizeY();
    int row = colorId / nbEltByLine;
    int col = colorId % nbEltByLine;
    int dx = 0;
    int dy = 0;

    if (col == 0) {
        dx = zoom + 2;
    }
    if (row == 0) {
        dy = zoom + 2;
    }
    if (col == nbEltByLine - 1) {
        dx = -zoom - 2;
    }
    if (row == nbLine - 1) {
        dy = -zoom - 2;
    }

    g2.setColor(palette[colorId]);
    g2.fillRect(dx + col * sx - zoom, dy + row * sy - zoom, sx + 2 * zoom, sy + 2 * zoom);
    g2.setStroke(new BasicStroke(3));
    g2.setColor(color);
    g2.drawRect(dx + col * sx - zoom - 2, dy + row * sy - zoom - 2, sx + 2 * (zoom + 2), sy + 2 * (zoom + 2));
    g2.setColor(Color.black);
    g2.drawRect(dx + col * sx - zoom, dy + row * sy - zoom, sx + 2 * zoom, sy + 2 * zoom);
}
 
Example 17
Project: incubator-netbeans   File: DropTargetLayer.java   View Source Code Vote up 4 votes
private static void drawVerticalTargetLine(Graphics2D g, int x, int y, int len) {
    g.setColor(DROP_TARGET_COLOR);
    g.setStroke(DROP_TARGET_LINE_STROKE);
    g.drawLine(x-1, y, x-1, y+len);
}
 
Example 18
Project: open-rmbt   File: ImageExport.java   View Source Code Vote up 4 votes
@Override
public BufferedImage generateImage(String lang, double upload, double download, double ping, String isp, String typ, String signal, String os) throws IOException {
    String unknownString = (lang.equals("de")) ? "unbekannt" : "unknown";
    BufferedImage img = new BufferedImage(390, 130, BufferedImage.TYPE_INT_ARGB);  
    img.createGraphics();  
    Graphics2D g = (Graphics2D)img.getGraphics(); 
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    BufferedImage img2 = ImageIO.read(getClass().getResourceAsStream("forumsmall_" + lang + ".png"));
    g.drawImage(img2, null, 0, 0);
    
    //Speeds
    g.setColor(Color.black);
    g.setFont(new Font("Droid Sans", Font.BOLD, 40));
    drawCenteredString(formatNumber(download, lang), 0, 20,130,65,g);
    drawCenteredString(formatNumber(upload, lang), 130, 20,130,65,g);
    drawCenteredString(formatNumber(ping, lang), 260, 20,130,65,g);


    //ISP and other information
    g.setColor(Color.WHITE);
    g.setFont(new Font("Verdana", Font.BOLD,10));

    //de
    if (lang.equals("de")) {
        //left
        g.drawString((typ == null)?unknownString:typ, 73, 109);
        g.drawString((isp == null)?unknownString:isp, 73, 124);

        //right
        g.drawString((signal==null)?"":signal + " dBm", 270, 109);
        g.drawString((os==null)?unknownString:os, 270, 124);
        
        //hide signal caption if signal is null
        if (signal==null) {
        	g.setColor(new Color(89,178,0));
        	g.fillRect(195, 98, 71, 13);
        	
        }
        
    } 
    else { //en
        //left
        g.drawString((typ == null)?unknownString:typ, 83, 109);
        g.drawString((isp == null)?unknownString:isp, 60, 124);

        //right
        g.drawString((signal==null)?"":signal + " dBm", 290, 109);
        g.drawString((os==null)?unknownString:os, 290, 124);
        
        //hide signal caption if signal is null
        if (signal==null) {
        	g.setColor(new Color(89,178,0));
        	g.fillRect(195, 98, 90, 13);
        	
        }
    }
    
    g.dispose();
    
    return img;
}
 
Example 19
Project: FaceAnnotationTool   File: MyImagePanel.java   View Source Code Vote up 4 votes
public void drawElps(Graphics2D g2d, ArrayList<Ellipse> elpses) {
	if (!unfinished.isEmpty()){
		this.activedEllipseIdx = -1;
		mainFrame.coordListTH.deselect();
	}
		
	AffineTransform old = g2d.getTransform();
	// -----------------------------------------//

	int i = 0;
	for (Ellipse e : elpses) {

		ArrayList<Point_> keyPoints = e.getKeyPoints();
		BasicStroke bs = UniversalTool.getPreferableStroke(Math.max(e.major, e.minor));

		g2d.setStroke(bs);
		// do rotation
		g2d.rotate(e.angle, e.x, e.y);
		// draw elps

		if (i == this.activedEllipseIdx) {
			g2d.setColor(Color.magenta);
		} else {
			g2d.setColor(Color.GREEN);
		}

		Ellipse2D.Double ed = e.getErectedEllipse2D();
		g2d.draw(ed);

		// reset transform
		g2d.setTransform(old);
		// draw keypoints

		if (i == this.activedEllipseIdx) {
			g2d.setColor(Color.white);
		} else {
			g2d.setColor(Color.red);
		}
		for (Point_ p : keyPoints) {

			g2d.fill(UniversalTool.getPointOval(p, 2.2 * bs.getLineWidth()));
		}
		i++;
	}
}
 
Example 20
Project: freecol   File: TileViewer.java   View Source Code Vote up 4 votes
/**
 * Displays the Tile text for a Tile.
 * Shows tile names, coordinates and colony values.
 *
 * @param g The Graphics2D object on which the text gets drawn.
 * @param tile The Tile to draw the text on.
 */
void displayOptionalTileText(Graphics2D g, Tile tile) {
    String text = null;
    int op = getClientOptions().getInteger(ClientOptions.DISPLAY_TILE_TEXT);
    switch (op) {
    case ClientOptions.DISPLAY_TILE_TEXT_NAMES:
        text = Messages.getName(tile);
        break;
    case ClientOptions.DISPLAY_TILE_TEXT_OWNERS:
        if (tile.getOwner() != null) {
            text = Messages.message(tile.getOwner().getNationLabel());
        }
        break;
    case ClientOptions.DISPLAY_TILE_TEXT_REGIONS:
        if (tile.getRegion() != null) {
            if (FreeColDebugger.isInDebugMode(FreeColDebugger.DebugMode.MENUS)
                && tile.getRegion().getName() == null) {
                text = tile.getRegion().getSuffix();
            } else {
                text = Messages.message(tile.getRegion().getLabel());
            }
        }
        break;
    case ClientOptions.DISPLAY_TILE_TEXT_EMPTY:
        break;
    default:
        logger.warning("displayTileText option " + op + " out of range");
        break;
    }

    g.setColor(Color.BLACK);
    g.setFont(FontLibrary.createFont(FontLibrary.FontType.NORMAL,
        FontLibrary.FontSize.TINY, lib.getScaleFactor()));
    if (text != null) {
        int b = getBreakingPoint(text);
        if (b == -1) {
            g.drawString(text,
                (tileWidth - g.getFontMetrics().stringWidth(text)) / 2,
                (tileHeight - g.getFontMetrics().getAscent()) / 2);
        } else {
            g.drawString(text.substring(0, b),
                (tileWidth - g.getFontMetrics().stringWidth(text.substring(0, b)))/2,
                halfHeight - (g.getFontMetrics().getAscent()*2)/3);
            g.drawString(text.substring(b+1),
                (tileWidth - g.getFontMetrics().stringWidth(text.substring(b+1)))/2,
                halfHeight + (g.getFontMetrics().getAscent()*2)/3);
        }
    }

    if (FreeColDebugger.debugDisplayCoordinates()) {
        String posString = tile.getX() + ", " + tile.getY();
        if (tile.getHighSeasCount() >= 0) {
            posString += "/" + Integer.toString(tile.getHighSeasCount());
        }
        g.drawString(posString,
            (tileWidth - g.getFontMetrics().stringWidth(posString)) / 2,
            (tileHeight - g.getFontMetrics().getAscent()) / 2);
    }
    String value = DebugUtils.getColonyValue(tile);
    if (value != null) {
        g.drawString(value,
            (tileWidth - g.getFontMetrics().stringWidth(value)) / 2,
            (tileHeight - g.getFontMetrics().getAscent()) / 2);
    }
}