Java将图像插入椭圆

时间:2015-05-19 17:33:56

标签: java swing

我的java程序中有一个圆圈。它只是一个简单的圆圈:

private BufferedImage img;
Graphics2D g = img.createGraphics();
g.fill(new Area(new Ellipse2D.Double(x, y, diam, diam)));

现在我想在圆圈中插入图像。我知道如何用drawImage创建一个像这样的图像:

image = Toolkit.getDefaultToolkit().getImage(url);
g2.drawImage(image, x, y, WIDTH, HEIGHT, null);

但我如何在圆圈内创建图像呢?

编辑: 我试图按照ControlAltDel的建议进行剪辑,但它不起作用,我不知道为什么。我的代码是吼叫。 我的程序只用简单的黄色圆圈就可以正常工作在Pang.java文件中,我试图替换该行:     g.fill(气泡[I] .getBubbleArea()); 通过:     Image image = new ImageIcon(“bubble.png”)。getImage();     g.clip(new Ellipse2D.Double(100,100,50,5));     g.drawImage(image,150,180,null); 但它没有工作,我没有得到任何错误。 我做错了什么?

Pang.java:

package pang.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.awt.TexturePaint;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

class ShapeCollision {

	private BufferedImage img;
	private Area limits;

	private int w = 800;// width of the game window
	private int h = 400;// heigth of the game window

	// private Bubble bubble = new Bubble(w, h);
	private Bubble[] bubbles = new Bubble[4];



	ShapeCollision() {

		img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		/**
		 * global image of the game shown in the window. This image will be
		 * added to the JLabel (imageLabel)
		 */

		final JLabel imageLabel = new JLabel(new ImageIcon(img));
		/**
		 * JLabel that contains the global image of the game
		 */



		for (int i = 0; i < bubbles.length; i++) {
			bubbles[i] = new Bubble(40 + i * 80, 40 + i * 80,i);
		}

		limits = new Area(new Rectangle(0, 0, w, h));
		/**
		 * game limits or painted area
		 */

		ActionListener animate = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				animate();
				imageLabel.repaint();
			}
		};
		Timer timer = new Timer(50, animate);
		/**
		 * Repaint rate
		 */

		timer.start();
		JOptionPane.showMessageDialog(null, imageLabel);
		timer.stop();
	}

	public void animate() {
		Graphics2D g = img.createGraphics();
		/**
		 * Creates a Graphics2D, which can be used to draw into this
		 * BufferedImage (img).
		 */

		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		/**
		 * removes edges from the shape, makes rounder objects
		 */

		g.setColor(Color.BLUE);
		/**
		 * color of the game windows background
		 */

		g.fillRect(0, 0, img.getWidth(), img.getHeight());
		/**
		 * size of the Graphics2D object (g). In this case the game window is
		 * filled
		 */

		for (Bubble bubble : bubbles) {
			bubble.IncrementPos();
			bubbleWallCollision(bubble);

		}

		for (Bubble bubble : bubbles) {
			detectBubblesCollisions(bubble);
		}

		g.setColor(Color.YELLOW);

		for (int i = 0; i < bubbles.length; i++) {
//Image image = new ImageIcon("bubble.png").getImage();
//g.clip(new Ellipse2D.Double(100, 100, 50,
//		5));
//g.drawImage(image, 150, 180, null);
			g.fill(bubbles[i].getBubbleArea());
			/**
			 * the bubble is filled *after* the obstacles, so the bubble is
			 * allways shown. If the bubble is on top of an abstacle that
			 * obstacle is covered by the bubble
			 */
		}

		g.dispose();
	}



	private void detectBubblesCollisions(Bubble bubble1) {
		for (Bubble bubble : bubbles) {
			if (bubble1 != bubble) {
				if (bubble1.getX() + bubble1.getBubbleDiam() / 2
						+ bubble.getBubbleDiam() / 2 > bubble.getX()
						&& bubble1.getX() < bubble.getX()
								+ bubble1.getBubbleDiam() / 2
								+ bubble.getBubbleDiam() / 2
						&& bubble1.getY() + bubble1.getBubbleDiam() / 2
								+ bubble.getBubbleDiam() / 2 > bubble.getY()
						&& bubble1.getY() < bubble1.getY()
								+ bubble1.getBubbleDiam() / 2
								+ bubble.getBubbleDiam() / 2) {
					if (PangUtilities.areasDistance(bubble1, bubble) < bubble1
							.getBubbleDiam() / 2 + bubble.getBubbleDiam() / 2) {
						calculateNewVelocities(bubble1, bubble);
					}
				}
			}

		}

	}

	private void calculateNewVelocities(Bubble bubble1, Bubble bubble) {
		double mass1 = bubble1.getBubbleDiam()/2;
		double mass2 = bubble.getBubbleDiam()/2;
		double velX1 = bubble1.get_xDelta();
		double velX2 = bubble.get_xDelta();
		double velY1 = bubble1.get_yDelta();
		double velY2 = bubble.get_yDelta();
    
		double newVelX1 = (velX1 * (mass1 - mass2) + (2 * mass2 * velX2)) / (mass1 + mass2);
		double newVelX2 = (velX2 * (mass2 - mass1) + (2 * mass1 * velX1)) / (mass1 + mass2);
		double newVelY1 = (velY1 * (mass1 - mass2) + (2 * mass2 * velY2)) / (mass1 + mass2);
		double newVelY2 = (velY2 * (mass2 - mass1) + (2 * mass1 * velY1)) / (mass1 + mass2);
	
	
	bubble1.set_xDelta(newVelX1);
	
	bubble.set_xDelta(newVelX2);
bubble1.set_yDelta(newVelY1);
	
	bubble.set_yDelta(newVelY2);
    
    bubble1.setX(bubble1.getX() + newVelX1);
    bubble1.setY(bubble1.getY() + newVelY1);
	
    bubble.setX(bubble.getX() + newVelX2);
    bubble.setY(bubble.getY() + newVelY2);


	}

	private void bubbleWallCollision(Bubble bubble) {
		if (bubble.getX() + bubble.getBubbleDiam()/2 >= w
				&& bubble.get_xDelta() > 0)
			bubble.invert_xDelta();
		if (bubble.getX()-bubble.getBubbleDiam()/2 <= 0 && bubble.get_xDelta() < 0)
			bubble.invert_xDelta();
		if (bubble.getY() + bubble.getBubbleDiam()/2 >= h
				&& bubble.get_yDelta() > 0)
			bubble.invert_yDelta();
		if (bubble.getY()-bubble.getBubbleDiam()/2 <= 0 && bubble.get_yDelta() < 0)
			bubble.invert_yDelta();

	}

	public static void main(String[] args) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				new ShapeCollision();
			}
		};
		SwingUtilities.invokeLater(r);
	}
}

Bubble.java:

package pang.gui;

import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;

public class Bubble {
	private Area bubbleArea = new Area();
	private double bubbleDiam = 20, bubbleSpeed = 2;// bubble diameter
private Shape shape;
	private double x;
	private double y;

	private int initialDirectionX, initialDirectionY;
	private double xDelta = bubbleSpeed;// bubble move increments
	private double yDelta = bubbleSpeed;// bubble move increments

	public Bubble(int x, int y,int i) {

		this.x = x;// Bubble position
		this.y = y;// Bubble position
		this.bubbleDiam *=i+1;
		System.out.println(bubbleDiam);

		genDirection();

	}

	public void IncrementPos() {
		x += xDelta;// new position of the bubble
		y += yDelta;// new position of the bubble
		this.bubbleArea = new Area(new Ellipse2D.Double(x-bubbleDiam/2, y-bubbleDiam/2, bubbleDiam,
				bubbleDiam));// bubble area definition
		shape=new Ellipse2D.Double(x-bubbleDiam/2, y-bubbleDiam/2, bubbleDiam,
				bubbleDiam);
	}

	
	
	public Shape getShape() {
		return shape;
	}

	public void setX(double d) {
		this.x = d;
	}

	public void setY(double d) {
		this.y = d;
	}

	public Area getBubbleArea() {
		return bubbleArea;
	}

	public double getBubbleDiam() {
		return bubbleDiam;
	}

	public double get_xDelta() {
		return xDelta;
	}

	public double get_yDelta() {
		return yDelta;
	}

	public void set_xDelta(double d) {
		this.xDelta = d;
	}

	public void set_yDelta(double d) {
		this.yDelta = d;
	}

	public void invert_yDelta() {
		yDelta *= -1;

	}

	public void invert_xDelta() {
		xDelta *= -1;

	}

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}

	private void genDirection() {
		do {
			initialDirectionX = PangUtilities.genRandomInt(-3, 3);
		} while (initialDirectionX == 0);

		do {
			initialDirectionY = PangUtilities.genRandomInt(-3, 3);
		} while (initialDirectionX == initialDirectionY
				|| initialDirectionY == 0);

		xDelta *= initialDirectionX;
		yDelta *= initialDirectionY;
	}

}

PangUtilities.java:

package pang.gui;

import java.util.Random;

public class PangUtilities {

	public static int genRandomInt(int min, int max) {

		Random rand = new Random();

		return min + rand.nextInt((max - min) + 1);

	}

	public static double areasDistance(Bubble b1, Bubble b2) {

		double distance = Math
				.sqrt(((b1.getX() - b2.getX()) * (b1.getX() - b2.getX()))
						+ ((b1.getY() - b2.getY()) * (b1.getY() - b2.getY())));
		return distance;
	}

}

1 个答案:

答案 0 :(得分:1)

有两种方法可以做到这一点

  1. (更简单)使用您的圈Graphics
  2. Shape对象上设置剪辑
  3. 将您的Image变为TexturePaint,然后执行
  4. Graphics2D g = ...;
    g.setPaint(myImagePaint);
    g.fill(yourCircle);