Java Code Examples for java.awt.Graphics#drawImage()

The following examples show how to use java.awt.Graphics#drawImage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestCompressionBI_BITFIELDS.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
protected void compareImages(BufferedImage src, BufferedImage dst) {
    ColorSpace srcCS = src.getColorModel().getColorSpace();
    ColorSpace dstCS = dst.getColorModel().getColorSpace();
    if (!srcCS.equals(dstCS) && srcCS.getType() == ColorSpace.TYPE_GRAY) {
        System.out.println("Workaround color difference with GRAY.");
        BufferedImage tmp  =
            new BufferedImage(src.getWidth(), src.getHeight(),
                              BufferedImage.TYPE_INT_RGB);
        Graphics g = tmp.createGraphics();
        g.drawImage(src, 0, 0, null);
        src = tmp;
    }
    int y = h / 2;
    for (int i = 0; i < colors.length; i++) {
        int x = dx * i + dx / 2;
        int srcRgb = src.getRGB(x, y);
        int dstRgb = dst.getRGB(x, y);

        if (srcRgb != dstRgb) {
            throw new RuntimeException("Test failed due to color difference: " +
                                       "src_pixel=" + Integer.toHexString(srcRgb) +
                                       "dst_pixel=" + Integer.toHexString(dstRgb));
        }
    }
}
 
Example 2
Source File: Map.java    From javagame with MIT License 6 votes vote down vote up
/**
 * �}�b�v��`�悷��
 * 
 * @param g �`��I�u�W�F�N�g
 * @param offsetX X�����I�t�Z�b�g
 * @param offsetY Y�����I�t�Z�b�g
 */
public void draw(Graphics g, int offsetX, int offsetY) {
    // �I�t�Z�b�g�����ɕ`��͈͂����߂�
    int firstTileX = pixelsToTiles(-offsetX);
    int lastTileX = firstTileX + pixelsToTiles(MainPanel.WIDTH) + 1;
    // �`��͈͂��}�b�v�̑傫�����傫���Ȃ�Ȃ��悤�ɒ���
    lastTileX = Math.min(lastTileX, col);

    int firstTileY = pixelsToTiles(-offsetY);
    int lastTileY = firstTileY + pixelsToTiles(MainPanel.HEIGHT) + 1;
    // �`��͈͂��}�b�v�̑傫�����傫���Ȃ�Ȃ��悤�ɒ���
    lastTileY = Math.min(lastTileY, row);

    for (int i = firstTileY; i < lastTileY; i++) {
        for (int j = firstTileX; j < lastTileX; j++) {
            // map�̒l�ɉ����ĉ摜��`��
            switch (map[i][j]) {
                case 'B' : // �u���b�N
                    g.drawImage(blockImage, tilesToPixels(j) + offsetX, tilesToPixels(i) + offsetY, null);
                    break;
            }
        }
    }
}
 
Example 3
Source File: RuntimeBuiltinLeafInfoImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private BufferedImage convertToBufferedImage(Image image) throws IOException {
    if (image instanceof BufferedImage) {
        return (BufferedImage)image;

    } else {
        MediaTracker tracker = new MediaTracker(new Component(){}); // not sure if this is the right thing to do.
        tracker.addImage(image, 0);
        try {
            tracker.waitForAll();
        } catch (InterruptedException e) {
            throw new IOException(e.getMessage());
        }
        BufferedImage bufImage = new BufferedImage(
                image.getWidth(null),
                image.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);

        Graphics g = bufImage.createGraphics();
        g.drawImage(image, 0, 0, null);
        return bufImage;
    }
}
 
Example 4
Source File: DragAndDropStepPanel.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void paintComponent(Graphics g) {
    //Rectangle clip = g.getClipBounds();

    //g.setColor(getBackground());
    //g.fillRect(clip.x, clip.y, clip.width, clip.height);

    int x = (getWidth() - dropHere.getWidth()) / 2;
    int y = (getHeight() - dropHere.getHeight()) / 2;

    g.drawImage(dropHere, x, y, null);
}
 
Example 5
Source File: RedialButton.java    From Spark with Apache License 2.0 5 votes vote down vote up
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int width = getWidth();
    int height = getHeight();

    int x = (width - backgroundImage.getWidth(null)) / 2;
    int y = (height - backgroundImage.getHeight(null)) / 2;
    g.drawImage(backgroundImage, x, y - 5, null);

    if (isEnabled()) {
        g.setColor(new Color(158, 32, 10));
    }
    else {
        g.setColor(Color.lightGray);
    }


    g.setFont(new Font("Tahoma", Font.BOLD, 11));

    String endCall = "Redial";
    int stringWidth = g.getFontMetrics().stringWidth(endCall);

    x = (width - stringWidth) / 2;
    y = height - 12;
    g.drawString(endCall, x, y);

}
 
Example 6
Source File: TabPanelJournal.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
public void paint(Graphics g)  {  
 //Make JTextArea transparent  
 setOpaque(false);  
  
 //add wrap  
 setLineWrap(true);  
  
 //Make JTextArea word wrap  
 setWrapStyleWord(true);  
  
 //Get image that we use as JTextArea background
 //Choose your own image directory in parameters.
 //ImageIcon ii=new ImageIcon("one.jpg");  
 
 //Image i=ii.getImage();  
 
  Image img;
  String IMAGE_DIR = "/images/";
        String fullImageName = "LanderHab.png";
         String fileName = fullImageName.startsWith("/") ?
             	fullImageName :
             	IMAGE_DIR + fullImageName;
         URL resource = ImageLoader.class.getResource(fileName);
Toolkit kit = Toolkit.getDefaultToolkit();
img = kit.createImage(resource);
  
 //Draw JTextArea background image  
 g.drawImage(img,0,0,null,this);  
  
 //Call super.paint to see TextArea
 super.paint(g);  
}
 
Example 7
Source File: TestFrame5.java    From JavaGame with MIT License 5 votes vote down vote up
@Override
public void paint(Graphics g) {
	g.drawImage(background, x, y, null);
	int t = (int) (Math.sin(theta + Math.PI / 2) / Math.cos(theta + Math.PI / 2));
	x = (Constant.GAME_WIDTH - width) / 2 + (int) (2 * p * t);
	y = 30 + (int) (t * t);

	theta += speed;

}
 
Example 8
Source File: XYZApp.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void paint(Graphics gc, int x, int y, int r) {
    Image ba[] = balls;
    if (ba == null) {
        Setup();
        ba = balls;
    }
    Image i = ba[r];
    int size = 10 + r;
    gc.drawImage(i, x - (size >> 1), y - (size >> 1), size, size, applet);
}
 
Example 9
Source File: Thumb.java    From HongsCORE with MIT License 5 votes vote down vote up
/**
 * 创建图层
 * @param img 源图
 * @param col 背景颜色
 * @param w   目标宽
 * @param h   目标高
 * @return    新的图层
 */
private BufferedImage draw(BufferedImage img, Color col, int w, int h) {
    BufferedImage  buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics grp = buf.createGraphics();
    if (col != null) {
        grp.setColor(col);
        grp.fillRect(0,0, w,h);
    }
    grp.drawImage(img, 0,0, null);
    grp.dispose();
    return buf;
}
 
Example 10
Source File: Lands.java    From JAVA-MVC-Swing-Monopoly with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * ���ػ���
 * 
 */
private void paintLands(Graphics g) {
	int x = 0;
	int y = 0;
	for (int i = 0; i < land.getLand().length; i++) {
		for (int j = 0; j < land.getLand()[i].length; j++) {
			if (land.getLand()[i][j] != 0) {
				// ͼƬ������ʾ
				g.drawImage(landsIMG, x + j * 60, y + i * 60, x
						+ (j + 1) * 60, y + (i + 1) * 60,
						(land.getLand()[i][j] - 1) * 60, 0, land.getLand()[i][j] * 60, 60, null);
			}
		}
	}
}
 
Example 11
Source File: ImageScalingHelper.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws a portion of an image, stretched or tiled.
 *
 * @param image Image to render.
 * @param g Graphics to render to
 * @param stretch Whether the image should be stretched or timed in the
 *                provided space.
 * @param dx1 X origin to draw to
 * @param dy1 Y origin to draw to
 * @param dx2 End x location to draw to
 * @param dy2 End y location to draw to
 * @param sx1 X origin to draw from
 * @param sy1 Y origin to draw from
 * @param sx2 Max x location to draw from
 * @param sy2 Max y location to draw from
 * @param xDirection Used if the image is not stretched. If true it
 *        indicates the image should be tiled along the x axis.
 */
private static void drawChunk(Image image, Graphics g, boolean stretch,
                       int dx1, int dy1, int dx2, int dy2, int sx1,
                       int sy1, int sx2, int sy2,
                       boolean xDirection) {
    if (dx2 - dx1 <= 0 || dy2 - dy1 <= 0 || sx2 - sx1 <= 0 ||
                          sy2 - sy1 <= 0) {
        // Bogus location, nothing to paint
        return;
    }
    if (stretch) {
        g.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
    }
    else {
        int xSize = sx2 - sx1;
        int ySize = sy2 - sy1;
        int deltaX;
        int deltaY;

        if (xDirection) {
            deltaX = xSize;
            deltaY = 0;
        }
        else {
            deltaX = 0;
            deltaY = ySize;
        }
        while (dx1 < dx2 && dy1 < dy2) {
            int newDX2 = Math.min(dx2, dx1 + xSize);
            int newDY2 = Math.min(dy2, dy1 + ySize);

            g.drawImage(image, dx1, dy1, newDX2, newDY2,
                        sx1, sy1, sx1 + newDX2 - dx1,
                        sy1 + newDY2 - dy1, null);
            dx1 += deltaX;
            dy1 += deltaY;
        }
    }
}
 
Example 12
Source File: AboutContentPanel.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
private void drawMagarenaImage(Graphics g) {
    BufferedImage image = MagicImages.ABOUT_LOGO;
    int scaledW = (int) (image.getWidth() * imageScale);
    int scaledH = (int) (image.getHeight() * imageScale);
    BufferedImage scaled = ImageHelper.scale(image, scaledW, scaledH);
    int posX = (getWidth() - scaled.getWidth()) / 2;
    int posY = (getHeight() - scaled.getHeight()) / 2;
    g.drawImage(scaled, posX, posY, null);
}
 
Example 13
Source File: MyFrame.java    From JavaGame with MIT License 5 votes vote down vote up
@Override
public void update(Graphics g) {
	if (backImg == null) {
		backImg = createImage(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
	}
	Graphics backg = backImg.getGraphics();
	Color c = backg.getColor();
	backg.setColor(Color.BLACK);
	backg.fillRect(0, 0, Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
	backg.setColor(c);
	paint(backg);		
	g.drawImage(backImg, 0, 0, null);
}
 
Example 14
Source File: DatePicker.java    From DroidUIBuilder with Apache License 2.0 4 votes vote down vote up
public void paint(Graphics g)
	{
		// 绘制背景
		super.paintBackground(g);
		
		// * 移动坐标原点(该 原点是排除padding和margin之后的结果)
		g.translate(getX(), getY());
		// 要绘制的ui图
		Image ig = // 处于工具面板时用小图标,节省空间
				this.isInDesignViewer()?getBgImg():getBgImg_small();
		// widget的宽度是否小于要绘的图片宽度,true表示是
		boolean isWidthLowerToImage = ig.getWidth(null)>getWidth();
		// widget的高度是否小于要绘的图片高度,true表示是
		boolean isHeightLowerToImage = ig.getHeight(null)>getHeight();
		
		// 变形缩放方式
//		g.drawImage(ig
//			, getX()+(isWidthLowerToImage?0:(getWidth() - ig.getWidth(null))/2)
//			, getY(), isWidthLowerToImage?getWidth():ig.getWidth(null)
//					, isHeightLowerToImage?getHeight():ig.getHeight(null)
//					, null);
		
		// 如果widget的宽度小于要绘的图片宽度,则最终的图像绘制宽度是widget宽度而非图像原宽度
		// ,否则在widget区域不够的情况下,图片会绘制到widget外面去,它就不好看了
		int drawW = isWidthLowerToImage?getWidth():ig.getWidth(null);
		// 如果widget的高度小于要绘的图片高度,则最终的图像绘制高度是widget高度而非图像原高度
		// ,否则在widget区域不够的情况下,图片会绘制到widget外面去,它就不好看了
		int drawH = isHeightLowerToImage?getHeight():ig.getHeight(null);
		// 如果widget的宽度小于要绘的图片宽度,则绘制目标坐标原
		// 点从0开始,否则在widget水平区域内居中绘制图片
		int drawX = isWidthLowerToImage?0:(getWidth() - ig.getWidth(null))/2;
		// 不变形裁剪方式
		g.drawImage(ig
				// 要绘制的目的地区域(坐标原点是相对于widget所处的整体位置)
				, drawX, 0, drawX+drawW, drawH // 从坐标1到坐标2
				// 要裁剪的源图像区域(坐标原点是相对于图片本身,所以要是(0,0))
				, 0, 0, drawW, drawH           // 从坐标1到坐标2
				, null);
		
		// * 恢复坐标原点
		g.translate(-getX(), -getY());
		
//		Date d = new Date();
//		Calendar c = Calendar.getInstance();
//		c.setTime(d);
//		int month = c.get(Calendar.MONTH);
//
//		g.setColor(Color.black);
//		SimpleDateFormat df = new SimpleDateFormat("MMMM yyyy");
//		g.drawString(df.format(d), getX() + 2, getY() + 14);
//
//		int day = c.get(Calendar.DATE);
//		c.add(Calendar.DATE, -day + 1);
//
//		while (c.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY)
//			c.add(Calendar.DATE, -1);
//
//		g.setColor(Color.darkGray);
//		g.drawString("S", getX() + 2, getY() + 34);
//		g.drawString("M", getX() + 22, getY() + 34);
//		g.drawString("T", getX() + 42, getY() + 34);
//		g.drawString("W", getX() + 62, getY() + 34);
//		g.drawString("T", getX() + 82, getY() + 34);
//		g.drawString("F", getX() + 102, getY() + 34);
//		g.drawString("S", getX() + 122, getY() + 34);
//
//		for (int j = 0; j < 6; j++)
//		{
//			for (int i = 0; i < 7; i++)
//			{
//				if (c.get(Calendar.MONTH) == month)
//					g.setColor(Color.black);
//				else
//					g.setColor(Color.lightGray);
//				g.drawString("" + c.get(Calendar.DATE), getX() + 2 + i * 20,
//						getY() + 54 + j * 20);
//				c.add(Calendar.DATE, 1);
//			}
//		}
	}
 
Example 15
Source File: Item.java    From New-Beginner-Java-Game-Programming-Src with Creative Commons Zero v1.0 Universal 4 votes vote down vote up
public void render(Graphics g, int x, int y){
	g.drawImage(texture, x, y, ITEMWIDTH, ITEMHEIGHT, null);
}
 
Example 16
Source File: IntermediateImages.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Draws both the direct and intermediate-image versions of a 
 * scaled-image, timing both variations.
 */
private void drawScaled(Graphics g) {
    long startTime, endTime, totalTime;
    
    // Scaled image
    ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    startTime = System.nanoTime();
    for (int i = 0; i < 100; ++i) {
        g.drawImage(picture, SCALE_X, DIRECT_Y, scaleW, scaleH, null);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    g.setColor(Color.BLACK);
    g.drawString("Direct: " + ((float)totalTime/100) + " ms", 
            SCALE_X, DIRECT_Y + scaleH + 20);
    System.out.println("scaled: " + totalTime);
    
    // Intermediate Scaled
    // First, create the intermediate image
    if (scaledImage == null ||
        scaledImage.getWidth() != scaleW ||
        scaledImage.getHeight() != scaleH)
    {
        GraphicsConfiguration gc = getGraphicsConfiguration();
        scaledImage = gc.createCompatibleImage(scaleW, scaleH);
        Graphics gImg = scaledImage.getGraphics();
        ((Graphics2D)gImg).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        gImg.drawImage(picture, 0, 0, scaleW, scaleH, null);
    }
    // Now, copy the intermediate image into place
    startTime = System.nanoTime();
    for (int i = 0; i < 100; ++i) {
        g.drawImage(scaledImage, SCALE_X, INTERMEDIATE_Y, null);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    g.drawString("Intermediate: " + ((float)totalTime/100) + " ms", 
            SCALE_X, INTERMEDIATE_Y + scaleH + 20);
    System.out.println("Intermediate scaled: " + totalTime);
}
 
Example 17
Source File: Player.java    From New-Beginner-Java-Game-Programming-Src with Creative Commons Zero v1.0 Universal 4 votes vote down vote up
@Override
public void render(Graphics g) {
	g.drawImage(Assets.player, (int) x, (int) y, width, height, null);
}
 
Example 18
Source File: MessageSequenceVisualizer.java    From osmo with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void paintComponent(Graphics g) {
  g.drawImage(image, 0, 0, null);
}
 
Example 19
Source File: CheckBox.java    From DroidUIBuilder with Apache License 2.0 4 votes vote down vote up
@Override
public void paint(Graphics g)
{
	// 绘制背景
	super.paintBackground(g);// 注意:不能调用super.paint()哦
	
	Image img;
	int off_x;
	int off_y;

	if (on == null || off == null)
	{
		g.setColor(Color.white);
		g.fillRect(getX() + 2, getY() + 2, 16, 16);

		g.setColor(Color.black);
		g.drawRect(getX() + 2, getY() + 2, 16, 16);

		if ("true".equals(this.getPropertyByAttName("android:checked")
				.getValue()))
		{
			g.drawLine(getX() + 2, getY() + 2, getX() + 18, getY() + 18);
			g.drawLine(getX() + 2, getY() + 18, getX() + 18, getY() + 2);
		}
		off_x = 20;
		off_y = 18;
	}
	else
	{
		if ("true".equals(this.getPropertyByAttName("android:checked")
				.getValue()))
		{
			img = on;
		}
		else
		{
			img = off;
		}
		g.drawImage(img, getX(), getY(), null);
		g.setColor(Color.black);
		off_x = img.getWidth(null);
		off_y = img.getHeight(null);
	}
	
	baseline = (off_y + fontSize) / 2;
	setTextColor(g);
	g.setFont(f);
	// 开启字体绘制反走样
	Utils.setTextAntiAliasing((Graphics2D)g, true);
	g.drawString(text.getStringValue(), getX() + off_x, getY() + baseline - 4);
	// 绘制完成后并闭字体绘制反走样
	Utils.setTextAntiAliasing((Graphics2D)g, false);
}
 
Example 20
Source File: MessageEngine.java    From javagame with MIT License 2 votes vote down vote up
/**
 * ������`�悷��
 * @param x X���W
 * @param y Y���W
 * @param c ����
 * @param g �`��I�u�W�F�N�g
 */
public void drawCharacter(int x, int y, char c, Graphics g) {
    Point pos = (Point)kana2Pos.get(new Character(c));
    g.drawImage(fontImage, x, y, x + FONT_WIDTH, y + FONT_HEIGHT,
            pos.x + color, pos.y, pos.x + color + FONT_WIDTH, pos.y + FONT_HEIGHT, null);
}