Java 在由磁贴组成的缓冲图像中旋转特定磁贴
我试图在BuffereImage中旋转一个磁贴(BuffereImage由磁贴组成),但我被卡住了,似乎找不到答案。我在谷歌上搜索了几个小时,但找不到任何教程。所以我决定来这里寻求帮助 我想旋转缓冲图像右下角的交叉点(平铺)。我想在其他瓷砖不受影响的情况下将其旋转90度 我正在做一个机器人可视化,所以每当机器人到达铁路道口时,道口将旋转90度,并移动到另一条铁路上。我添加了按钮“旋转”,以便能够单独测试旋转。单击按钮时,我希望交叉点旋转90度 这是代码。您可以复制它并运行它 不要忘记将图像复制到开发环境(Eclipse等)的图像文件夹中。图像为130 X 130 在下面的“VisPanel”类中,图像的名称是:将图像另存为“railHorizontal.JPG”、“railVertical2.JPG”和“railCrossing2.JPG” 以下是图片 守则:Java 在由磁贴组成的缓冲图像中旋转特定磁贴,java,image,bufferedimage,Java,Image,Bufferedimage,我试图在BuffereImage中旋转一个磁贴(BuffereImage由磁贴组成),但我被卡住了,似乎找不到答案。我在谷歌上搜索了几个小时,但找不到任何教程。所以我决定来这里寻求帮助 我想旋转缓冲图像右下角的交叉点(平铺)。我想在其他瓷砖不受影响的情况下将其旋转90度 我正在做一个机器人可视化,所以每当机器人到达铁路道口时,道口将旋转90度,并移动到另一条铁路上。我添加了按钮“旋转”,以便能够单独测试旋转。单击按钮时,我希望交叉点旋转90度 这是代码。您可以复制它并运行它 不要忘记将图像复制到
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;
}
}
公共类tileImage扩展JFrame{
私有静态最终长serialVersionUID=1L;
公共TileImage(){
这个.setSize(700700);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
VisPanel vis=新的VisPanel();
JPanel pana=新的JPanel();
pana.setLayout(新的FlowLayout());
vis.setPreferredSize(新尺寸(500500));
添加(vis);
buffereImage sub=vis.getImg().getSubimage(261260、129、129);
JButton but=新JButton(新旋转交叉作用(sub,vis));
但是,setPreferredSize(新维度(170,40));
加上(但是);
这个.setContentPane(pana);
此.setVisible(true);
}
公共静态void main(字符串[]args){
新TileImage();
}
}
类RotateCrossingAction扩展了AbstractAction{
私有静态最终长serialVersionUID=1L;
私有最终组件;
私有最终缓冲图像;
私有最终缓冲区镜像操作;
公共旋转交叉操作(缓冲区图像、组件){
超级(“旋转”);
这个组件=组件;
这个图像=图像;
双x=0.5*image.getWidth();
双y=0.5*image.getHeight();
AffineTransform xfrm=新的AffineTransform();
xfrm=仿射变换。getQuadrantRotateInstance(2,x,y);
op=新的仿射Transformop(xfrm,仿射Transformop.TYPE_NEAREST_NEIGHBOR);
}
已执行的公共无效操作(操作事件e){
BuffereImage tmpImage=op.filter(图像,null);
setData(tmpImage.getRaster());
repaint();
}
}
公共类VisPanel扩展JPanel{
私有静态最终整数映像_TYPE=buffereImage.TYPE_int_ARGB;
专用缓冲图像img;
AffineTransform AffineTransform=新的AffineTransform();
公共面板(){
//在这里,您应该创建一个兼容的BuffereImage
//img=新的缓冲区映像(450350,映像类型);
img=新的缓冲区映像(500500,映像类型);
this.setSize(img.getWidth(),img.getHeight());
最终整块NB_瓷砖=4;
BuffereImage[]tiles=新的BuffereImage[NB_tiles];
瓷砖[0]=createHorizontalRail(新颜色(255、255、255));
瓷砖[1]=createVerticalRail(新颜色(255、255、255));
tiles[2]=createCrossing(新颜色(255,0255));
最终整数[][]映射=新整数[][]{
{4, 4, 1},
{4, 4, 1},
{0, 0, 2},
{4, 4, 4},
};
对于(int i=0;i