Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- public class Window extends JPanel implements ActionListener {
- public int width, height;
- public Window(String title, int width, int height) {
- this.width = width;
- this.height = height;
- JFrame frame = new JFrame(title);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(width, height);
- frame.setLocationRelativeTo(null);
- frame.setResizable(false);
- frame.setVisible(true);
- setPreferredSize(new Dimension(width, height));
- frame.add(this);
- frame.pack();
- // Add inputs
- Mouse m = new Mouse();
- addMouseListener(m);
- addMouseMotionListener(m);
- Keys k = new Keys();
- frame.addKeyListener(k);
- init();
- Timer timer = new Timer(10,this);
- timer.start();
- }
- int[][] grid = new int[60][60];
- int x = 10,y = 10;
- boolean pressed;
- //Initialize
- public void init() {
- grid[x][y] = 1;
- pressed = false;
- }
- public void updateGrid() {
- for (int y = 0; y < 60; y++) {
- for (int x = 0; x< 60; x++) {
- if(grid[x][y] != 0) {
- grid[x][y] += 250;//number adjusts the length of fade
- if(grid[x][y] > 255) {
- grid[x][y] = 255;
- }
- }
- }
- }
- }
- public void paint(Graphics g) {
- super.paint(g); //Clears the Screen
- setBackground(Color.WHITE);
- if(Keys.up) {
- if(!pressed) {
- pressed = true;
- y--;
- updateGrid();
- }
- }else if(Keys.down) {
- if(!pressed) {
- pressed = true;
- y++;
- updateGrid();
- }
- }else if(Keys.left) {
- if(!pressed) {
- pressed = true;
- x--;
- updateGrid();
- }
- }else if(Keys.right) {
- if(!pressed) {
- pressed = true;
- x++;
- updateGrid();
- }
- }else {
- pressed = false;
- }
- //Bound check
- if(x < 0)
- x = 0;
- if(x > 59)
- x = 59;
- if(y < 0)
- y = 0;
- if(y > 59)
- y = 59;
- grid[x][y] = 1;
- //fresh fade
- for(int y = 0; y < 60; y++) {
- for(int x = 0; x < 60; x++) {
- if(grid[x][y] != 0) {
- int c = grid[x][y];
- g.setColor(new Color(c,c,c));
- g.fillRect(32*x, 32*y, 4, 4);
- }
- }
- }
- repaint();
- }
- //Update
- public void actionPerformed(ActionEvent arg0) {
- repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement