Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 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.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9. import javax.swing.Timer;
  10.  
  11. public class Window extends JPanel implements ActionListener {
  12.  
  13. public int width, height;
  14.  
  15. public Window(String title, int width, int height) {
  16. this.width = width;
  17. this.height = height;
  18. JFrame frame = new JFrame(title);
  19. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20. frame.setSize(width, height);
  21. frame.setLocationRelativeTo(null);
  22. frame.setResizable(false);
  23. frame.setVisible(true);
  24.  
  25. setPreferredSize(new Dimension(width, height));
  26. frame.add(this);
  27. frame.pack();
  28.  
  29. // Add inputs
  30. Mouse m = new Mouse();
  31. addMouseListener(m);
  32. addMouseMotionListener(m);
  33. Keys k = new Keys();
  34. frame.addKeyListener(k);
  35.  
  36. init();
  37. Timer timer = new Timer(10,this);
  38. timer.start();
  39. }
  40.  
  41. int[][] grid = new int[60][60];
  42. int x = 10,y = 10;
  43. boolean pressed;
  44.  
  45. //Initialize
  46. public void init() {
  47. grid[x][y] = 1;
  48. pressed = false;
  49.  
  50. }
  51. public void updateGrid() {
  52. for (int y = 0; y < 60; y++) {
  53. for (int x = 0; x< 60; x++) {
  54. if(grid[x][y] != 0) {
  55. grid[x][y] += 250;//number adjusts the length of fade
  56. if(grid[x][y] > 255) {
  57. grid[x][y] = 255;
  58. }
  59. }
  60. }
  61.  
  62. }
  63. }
  64.  
  65. public void paint(Graphics g) {
  66. super.paint(g); //Clears the Screen
  67. setBackground(Color.WHITE);
  68.  
  69.  
  70. if(Keys.up) {
  71. if(!pressed) {
  72. pressed = true;
  73. y--;
  74. updateGrid();
  75. }
  76. }else if(Keys.down) {
  77. if(!pressed) {
  78. pressed = true;
  79. y++;
  80. updateGrid();
  81. }
  82. }else if(Keys.left) {
  83. if(!pressed) {
  84. pressed = true;
  85. x--;
  86. updateGrid();
  87. }
  88. }else if(Keys.right) {
  89. if(!pressed) {
  90. pressed = true;
  91. x++;
  92. updateGrid();
  93. }
  94.  
  95. }else {
  96. pressed = false;
  97. }
  98.  
  99. //Bound check
  100. if(x < 0)
  101. x = 0;
  102. if(x > 59)
  103. x = 59;
  104. if(y < 0)
  105. y = 0;
  106. if(y > 59)
  107. y = 59;
  108.  
  109.  
  110.  
  111. grid[x][y] = 1;
  112. //fresh fade
  113. for(int y = 0; y < 60; y++) {
  114. for(int x = 0; x < 60; x++) {
  115. if(grid[x][y] != 0) {
  116. int c = grid[x][y];
  117. g.setColor(new Color(c,c,c));
  118. g.fillRect(32*x, 32*y, 4, 4);
  119.  
  120. }
  121. }
  122. }
  123.  
  124.  
  125.  
  126.  
  127. repaint();
  128. }
  129.  
  130.  
  131.  
  132. //Update
  133. public void actionPerformed(ActionEvent arg0) {
  134.  
  135. repaint();
  136. }
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement