Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import javax.imageio.ImageIO;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JPanel;
  11.  
  12. public class JFrameTest {
  13.  
  14. static BufferedImage buf;
  15. static JPanel panel;
  16.  
  17.  
  18. public static void main(String[] args) {
  19. createWindow();
  20. loadImage();
  21. showImage();
  22. }
  23.  
  24.  
  25.  
  26. public static void createWindow() {
  27. JFrame frame = new JFrame();
  28. panel = new JPanel();
  29. frame.setSize(1000, 1000);
  30. frame.setTitle("Tester");
  31. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32. frame.setResizable(false);
  33. frame.setLocationRelativeTo(null);
  34. frame.add(panel);
  35. frame.setVisible(true);
  36. panel.setLayout(null);
  37.  
  38. }
  39.  
  40.  
  41.  
  42.  
  43. public static void loadImage() {
  44. try {
  45. buf = ImageIO.read(new File("res/test.png"));
  46. } catch (IOException ex) {
  47. System.out.println("NOT FOUND");
  48. }
  49.  
  50. }
  51.  
  52. public static void showImage() {
  53.  
  54. final int[][] MAP =
  55. {
  56. {1, 1, 1, 1, 1, 1, 1, 1, 1},
  57. {1, 1, 1, 1, 1, 1, 1, 1, 1},
  58. {1, 1, 1, 1, 1, 1, 1, 1, 1},
  59. {1, 1, 1, 1, 1, 1, 1, 1, 1},
  60. {1, 1, 1, 1, 1, 1, 1, 1, 1},
  61. {1, 1, 1, 1, 1, 1, 1, 1, 1},
  62. {1, 1, 1, 1, 1, 1, 1, 1, 1},
  63. {1, 1, 1, 1, 1, 1, 1, 1, 1},
  64. {1, 1, 1, 1, 1, 1, 1, 1, 1},
  65. {1, 1, 1, 1, 1, 1, 1, 1, 1}
  66. };
  67.  
  68. ImageIcon test = new ImageIcon(buf);
  69.  
  70. panel.setBackground(Color.WHITE);
  71. System.out.println(test.getIconWidth());
  72. System.out.println(test.getIconHeight());
  73.  
  74. JLabel[][] labelGrid = new JLabel[MAP.length][MAP[0].length];
  75.  
  76. for (int r = 0; r < labelGrid.length; r++) {
  77. for (int c = 0; c < labelGrid[r].length; c++) {
  78. labelGrid[r][c] = new JLabel();
  79. labelGrid[r][c].setSize(test.getIconWidth(), test.getIconWidth());
  80. labelGrid[r][c].setLocation(test.getIconWidth() * r, test.getIconHeight() * c);
  81. labelGrid[r][c].setIcon(test);
  82. panel.add(labelGrid[r][c]);
  83. }
  84. }
  85. panel.revalidate();
  86. }
  87.  
  88. }
  89.  
  90. final int[][] MAP =
  91. {
  92. {1, 1, 1},
  93. {1, 1, 1},
  94. {1, 1, 1}
  95. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement