我正在尝试在BufferedImage中旋转一个tile(bufferedimage由tile组成)但是我卡住了,似乎找不到答案。我用谷歌搜索了几个小时但找不到任何教程。所以我决定来这里寻求帮助。
我想旋转缓冲图像右下角的交叉(平铺)。我想将其旋转90度,而其他瓷砖不受影响。
我正在进行机器人可视化,因此每当机器人到达铁路交叉口时,交叉口将转动90度,它将移动到另一个轨道。我添加了“旋转”按钮,以便能够单独测试旋转。单击按钮时,我希望将交叉点旋转90度。
这是代码。您可以复制并运行它。
不要忘记将图像复制到开发环境的图像文件夹中(Eclipse等)。图像为130 X 130。
以下“VisPanel”类中的图像名称为:将图像保存为“railHorizontal.JPG”,“railVertical2.JPG”和“railCrossing2.JPG”。
以下是图片。
alt text http://www.freeimagehosting.net/uploads/ed6f294d32.jpg
alt text http://www.freeimagehosting.net/uploads/dba1a7f996.jpg
alt text http://www.freeimagehosting.net/uploads/f9d114ac1e.jpg
代码:
public class TilesImage extends JFrame{
private static final long serialVersionUID = 1L;
public TilesImage(){
this.setSize(700,700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
VisPanel vis = new VisPanel();
JPanel pana = new JPanel();
pana.setLayout(new FlowLayout());
vis.setPreferredSize(new Dimension(500, 500));
pana.add(vis);
BufferedImage sub = vis.getImg().getSubimage(261, 260, 129, 129);
JButton but = new JButton(new RotateCrossingAction(sub, vis));
but.setPreferredSize(new Dimension(170, 40));
pana.add(but);
this.setContentPane(pana);
this.setVisible(true);
}
public static void main( String[] args ) {
new TilesImage();
}
}
class RotateCrossingAction extends AbstractAction{
private static final long serialVersionUID = 1L;
private final Component component;
private final BufferedImage image;
private final BufferedImageOp op;
public RotateCrossingAction(BufferedImage image, Component component) {
super("Rotate");
this.component = component;
this.image = image;
double x = 0.5 * image.getWidth();
double y = 0.5 * image.getHeight();
AffineTransform xfrm = new AffineTransform();
xfrm = AffineTransform.getQuadrantRotateInstance(2, x, y);
op = new AffineTransformOp(xfrm, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
}
public void actionPerformed(ActionEvent e) {
BufferedImage tmpImage = op.filter(image, null);
image.setData(tmpImage.getRaster());
component.repaint();
}
}
public class VisPanel extends JPanel{
private static final int IMAGE_TYPE = BufferedImage.TYPE_INT_ARGB;
private BufferedImage img;
AffineTransform affineTransform = new AffineTransform();
public VisPanel() {
// here you should create a compatible BufferedImage
//img = new BufferedImage( 450, 350, IMAGE_TYPE );
img = new BufferedImage( 500, 500, IMAGE_TYPE );
this.setSize(img.getWidth(), img.getHeight());
final int NB_TILES = 4;
BufferedImage[] tiles = new BufferedImage[NB_TILES];
tiles[0] = createHorizontalRail( new Color( 255, 255, 255 ) );
tiles[1] = createVerticalRail( new Color( 255, 255, 255 ) );
tiles[2] = createCrossing( new Color( 255, 0, 255 ) );
final int[][] map = new int[][] {
{4, 4, 1},
{4, 4, 1},
{0, 0, 2},
{4, 4, 4},
};
for (int i = 0; i < map[0].length; i++) {
BufferedImage tile = null;
for (int j = 0; j < map.length; j++) {
if(map[j][i] == 0){
tile = tiles[0];
for (int x = 0; x < tile.getWidth(); x++) {
for (int y = 0; y < tile.getHeight(); y++) {
img.setRGB( x + i * 130, y + j * 130, tile.getRGB(x,y) );
//img.setRGB( x + i * 45, y + j * 32, tile.getRGB(x,y) );
}
}
} if(map[j][i] == 1){
tile = tiles[1];
for (int x = 0; x < tile.getWidth(); x++) {
for (int y = 0; y < tile.getHeight(); y++) {
img.setRGB( x + i * 130, y + j * 130, tile.getRGB(x,y) );
}
}
}
if(map[j][i] == 2){
tile = tiles[2];
for (int x = 0; x < tile.getWidth(); x++) {
for (int y = 0; y < tile.getHeight(); y++) {
img.setRGB( x + i * 130, y + j * 130, tile.getRGB(x,y) );
}
}
}
}
}
this.setVisible( true );
}
private BufferedImage createHorizontalRail( final Color c ) {
final Random r = new Random();
BufferedImage img = null;
try {
img = ImageIO.read(new File("images/railHorizontal.JPG"));
} catch (IOException e) {
}
return img;
}
private BufferedImage createVerticalRail( final Color c ) {
final Random r = new Random();
BufferedImage img = null;
try {
img = ImageIO.read(new File("images/railVertical2.JPG"));
} catch (IOException e) {
}
return img;
}
private BufferedImage createCrossing( final Color c ) {
final Random r = new Random();
BufferedImage img = null;
try {
img = ImageIO.read(new File("images/railCrossing2.JPG"));
} catch (IOException e) {
}
return img;
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(img, 0, 0, null);
}
public BufferedImage getImg() {
return img;
}
public void setImg(BufferedImage img) {
this.img = img;
}
public AffineTransform getAffineTransform() {
return affineTransform;
}
public void setAffineTransform(AffineTransform affineTransform) {
this.affineTransform = affineTransform;
}
}
感谢您的帮助。
答案 0 :(得分:0)
在VisPanel中,不要使用一个大的BufferedImage,而是创建一个与您的int[][] map
数组对应的BufferedImages数组。
BufferedImage[][] img;
您可以更轻松地旋转单张图像。
您可以在getImg方法中构建一个大图像。
答案 1 :(得分:0)
只需将您的代码xfrm = AffineTransform.getQuadrantRotateInstance(2, x, y);
替换为xfrm = AffineTransform.getRotateInstance(Math.toRadians(90), x, y);