Swing之图片水平翻转 垂直翻转 180度旋转

               
import java.awt.geom.AffineTransform;   import java.awt.image.AffineTransformOp;   import java.awt.image.BufferedImage;   import java.io.FileInputStream;   import java.io.IOException;   import javax.imageio.ImageIO;   import javax.swing.ImageIcon;   import javax.swing.JFrame;   import javax.swing.JLabel;   import javax.swing.JTabbedPane;     /**   * 使图片水平翻转、垂直翻转、旋转180度   * @author Administrator   */  public class TestPicture {         public static void main(String[] args) throws IOException {           BufferedImage sourceImage = ImageIO.read(new FileInputStream("title4.gif"));           BufferedImage dstImage = null;           AffineTransform transform = new   AffineTransform(-1,0,0,1,sourceImage.getWidth()-1,0);//水平翻转   //        AffineTransform transform = new   AffineTransform(1,0,0,-1,0,sourceImage.getHeight()-1);//垂直翻转   //         AffineTransform transform = new   AffineTransform(-1,0,0,-1,sourceImage.getWidth()-1,sourceImage.getHeight()-1);//旋转180度           AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);           dstImage = op.filter(sourceImage, null);             JTabbedPane tabbedPane = new JTabbedPane();           tabbedPane.add("Source Transform", new JLabel(new ImageIcon(sourceImage)));           tabbedPane.add("Affine Transform", new JLabel(new ImageIcon(dstImage)));             JFrame jframe = new JFrame();           jframe.setSize(800, 600);           jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           jframe.getContentPane().add(tabbedPane);           jframe.setVisible(true);       }   }  

AffineTransform 类表示 2D 仿射变换,它执行从 2D 坐标到其他 2D 坐标的线性映射,保留了线的“直线性”和“平行性”。可以使用一系列平移、缩放、翻转、旋转和剪切来构造仿射变换。           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Java Swing实现黑白翻转棋的示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Reversi extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JButton[][] board; private int[][] status; private int currentPlayer; private JLabel statusLabel; public Reversi() { setTitle("黑白翻转棋"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 400); setResizable(false); JPanel boardPanel = new JPanel(); boardPanel.setLayout(new GridLayout(8, 8)); board = new JButton[8][8]; status = new int[8][8]; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { board[i][j] = new JButton(); board[i][j].setPreferredSize(new Dimension(40, 40)); board[i][j].addActionListener(this); boardPanel.add(board[i][j]); status[i][j] = 0; } } board[3][3].setBackground(Color.WHITE); board[3][4].setBackground(Color.BLACK); board[4][3].setBackground(Color.BLACK); board[4][4].setBackground(Color.WHITE); status[3][3] = -1; status[3][4] = 1; status[4][3] = 1; status[4][4] = -1; JPanel statusPanel = new JPanel(); statusPanel.setLayout(new FlowLayout()); statusLabel = new JLabel("黑方先行"); statusPanel.add(statusLabel); Container container = getContentPane(); container.setLayout(new BorderLayout()); container.add(boardPanel, BorderLayout.CENTER); container.add(statusPanel, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { JButton button = (JButton) e.getSource(); int row = -1, col = -1; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (button == board[i][j]) { row = i; col = j; break; } } } if (row == -1 || col == -1) { return; } if (status[row][col] != 0) { return; } int count = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } int r = row + i, c = col + j; while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -currentPlayer) { r += i; c += j; } if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == currentPlayer) { count += Math.max(Math.abs(row - r), Math.abs(col - c)) - 1; } } } if (count == 0) { return; } status[row][col] = currentPlayer; if (currentPlayer == 1) { button.setBackground(Color.BLACK); currentPlayer = -1; statusLabel.setText("白方行动"); } else { button.setBackground(Color.WHITE); currentPlayer = 1; statusLabel.setText("黑方行动"); } for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } int r = row + i, c = col + j; while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -currentPlayer) { r += i; c += j; } if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == currentPlayer) { int nr = row + i, nc = col + j; while (nr != r || nc != c) { status[nr][nc] = currentPlayer; if (currentPlayer == 1) { board[nr][nc].setBackground(Color.BLACK); } else { board[nr][nc].setBackground(Color.WHITE); } nr += i; nc += j; } } } } int blackCount = 0, whiteCount = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (status[i][j] == 1) { blackCount++; } else if (status[i][j] == -1) { whiteCount++; } } } if (blackCount + whiteCount == 64) { if (blackCount > whiteCount) { JOptionPane.showMessageDialog(null, "黑方胜利!"); } else if (blackCount < whiteCount) { JOptionPane.showMessageDialog(null, "白方胜利!"); } else { JOptionPane.showMessageDialog(null, "平局!"); } System.exit(0); } if (currentPlayer == 1) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (status[i][j] == 0) { int cnt = 0; for (int k = -1; k <= 1; k++) { for (int l = -1; l <= 1; l++) { if (k == 0 && l == 0) { continue; } int r = i + k, c = j + l; while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -1) { r += k; c += l; } if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == 1) { cnt += Math.max(Math.abs(i - r), Math.abs(j - c)) - 1; } } } if (cnt > 0) { return; } } } } JOptionPane.showMessageDialog(null, "白方无子可下,黑方继续行动"); currentPlayer = -1; statusLabel.setText("黑方行动"); } else { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (status[i][j] == 0) { int cnt = 0; for (int k = -1; k <= 1; k++) { for (int l = -1; l <= 1; l++) { if (k == 0 && l == 0) { continue; } int r = i + k, c = j + l; while (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == 1) { r += k; c += l; } if (r >= 0 && r < 8 && c >= 0 && c < 8 && status[r][c] == -1) { cnt += Math.max(Math.abs(i - r), Math.abs(j - c)) - 1; } } } if (cnt > 0) { return; } } } } JOptionPane.showMessageDialog(null, "黑方无子可下,白方继续行动"); currentPlayer = 1; statusLabel.setText("白方行动"); } } public static void main(String[] args) { Reversi game = new Reversi(); game.setVisible(true); game.currentPlayer = 1; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值