Guest User

Untitled

a guest
Jan 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.JScrollPane;
  9. import javax.swing.SwingUtilities;
  10. import javax.swing.Timer;
  11.  
  12. public class Test {
  13.  
  14. public static void main(String[] args) throws Exception {
  15. SwingUtilities.invokeLater(new Runnable() {
  16.  
  17. @Override
  18. public void run() {
  19. new Test().createAndShowUI();
  20. }
  21. });
  22. }
  23.  
  24. private void createAndShowUI() {
  25. JFrame frame = new JFrame();
  26. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27. initComponents(frame);
  28. frame.setPreferredSize(new Dimension(400, 400));
  29. frame.pack();
  30. frame.setVisible(true);
  31. }
  32.  
  33. private void initComponents(JFrame frame) {
  34. JScrollPane jsp = new JScrollPane();
  35. jsp.setViewportView(new Panel(800, 800, jsp));
  36. frame.getContentPane().add(jsp);
  37. }
  38. }
  39.  
  40. class Panel extends JPanel {
  41.  
  42. private int across, down;
  43. private Panel.Tile[][] tiles;
  44. private Color color = Color.black;
  45. private final JScrollPane jScrollPane;
  46.  
  47. public Panel(int width, int height, JScrollPane jScrollPane) {
  48. this.setPreferredSize(new Dimension(width, height));
  49. this.jScrollPane = jScrollPane;
  50. createTiles();
  51. changePanelColorTimer();//just something to do to check if its repaints fine
  52. }
  53.  
  54. @Override
  55. protected void paintComponent(Graphics g) {
  56. super.paintComponent(g);
  57. for (int i = 0; i < across; i++) {
  58. for (int j = 0; j < down; j++) {
  59. g.setColor(color);
  60. for (int k = 0; k < 5; k++) {
  61. g.drawRect(tiles[i][j].x + k, tiles[i][j].y + k, tiles[i][j].side - k * 2, tiles[i][j].side - 2 * k);
  62. }
  63. }
  64. }
  65. updateScrollPane();//refresh the pane after every paint
  66. }
  67.  
  68. //calls repaint on the scrollPane instance
  69. private void updateScrollPane() {
  70. jScrollPane.repaint();
  71. }
  72.  
  73. private void createTiles() {
  74. across = 13;
  75. down = 9;
  76. tiles = new Panel.Tile[across][down];
  77.  
  78. for (int i = 0; i < across; i++) {
  79. for (int j = 0; j < down; j++) {
  80. tiles[i][j] = new Panel.Tile((i * 50), (j * 50), 50);
  81. }
  82. }
  83. }
  84.  
  85. //change the color of the grid lines from black to red and vice versa every 2s
  86. private void changePanelColorTimer() {
  87. Timer timer = new Timer(2000, new ActionListener() {
  88.  
  89. @Override
  90. public void actionPerformed(ActionEvent e) {
  91. if (color == Color.black) {
  92. color = Color.red;
  93. } else {
  94. color = Color.black;
  95. }
  96. }
  97. });
  98. timer.setInitialDelay(2000);
  99. timer.start();
  100. }
  101.  
  102. private class Tile {
  103.  
  104. int x, y, side;
  105.  
  106. public Tile(int inX, int inY, int inSide) {
  107. x = inX;
  108. y = inY;
  109. side = inSide;
  110. }
  111. }
  112. }
Add Comment
Please, Sign In to add comment