package com.github.kc_7.activityplus;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public abstract class Activity extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	
	private Timer timer; 
	
	public Activity(int width, int height, int rate, Color color) {
		
		setFocusable(true);
		setPreferredSize(new Dimension(width, height));
		setBackground(color);
		addKeyListener(new KeyHandler());
		
		timer = new Timer(rate, this);
	}
	
	// Activates the activity
	protected final void activate() {
		
		load();
		start();
		
		timer.start();
		
	}
	
	// Deactivates the activity
	protected void deactivate() {
		
		stop();
		
		timer.stop();
		
		final ActivityPlus plus = (ActivityPlus) SwingUtilities.getWindowAncestor(this);
		plus.endActivity();
		
	}
	
	protected abstract void load();
	
	protected abstract void start();
	
	protected abstract void stop();
	
	protected abstract void process();
	
	protected abstract void draw(Graphics g);
	
	protected abstract void handleKey(Set<Integer> pressedKeys);
	
	@Override
	public final void actionPerformed(ActionEvent e) {
		
		process();
		repaint();
		
	}
	
	@Override
	protected final void paintComponent(Graphics g) {
		
		super.paintComponent(g);
		draw(g);
		
	}
	
	private class KeyHandler extends KeyAdapter {
		
		final Set<Integer> pressedKeys = new HashSet<>();
		
		@Override
		public synchronized void keyPressed(KeyEvent e) {
			
			pressedKeys.add(e.getKeyCode());
			handleKey(pressedKeys);
			
		}
		
		@Override
		public synchronized void keyReleased(KeyEvent e) {
			
			pressedKeys.remove(e.getKeyCode());
			
		}
		
	}
	
}

@@ -206,23 +206,21 @@
 vent.Key
-Adapter
+Event
 ;%0Aimport
@@ -230,37 +230,40 @@
 va.awt.event.Key
-Event
+Listener
 ;%0Aimport java.ut
@@ -292,24 +292,25 @@
 a.util.Set;%0A
+%0A
 import javax
@@ -457,16 +457,29 @@
 Listener
+, KeyListener
  %7B%0A%0A%09pri
@@ -529,21 +529,23 @@
 L;%0A%09%0A%09pr
-ivate
+otected
  Timer t
@@ -551,16 +551,75 @@
 timer; %0A
+%09private final Set%3CInteger%3E pressedKeys = new HashSet%3C%3E();%0A
 %09%0A%09publi
@@ -797,24 +797,12 @@
 ner(
-new KeyHandler()
+this
 );%0A%09
@@ -951,24 +951,146 @@
 ();%0A%09%09%0A%09%7D%0A%09%0A
+%09protected final void suspend() %7B%0A%09%09%0A%09%09timer.stop();%0A%09%09%0A%09%7D%0A%09%0A%09protected final void resume() %7B%0A%09%09%0A%09%09timer.start();%0A%09%09%0A%09%7D%0A%09%0A
 %09// Deactiva
@@ -1117,16 +1117,22 @@
 otected 
+final 
 void dea
@@ -1760,113 +1760,70 @@
 %0A%09%0A%09
-private class KeyHandler extends KeyAdapter %7B%0A%09%09%0A%09%09final Set%3CInteger%3E press
+@Override%0A%09public synchronized void keyTyp
 ed
+(
 Key
-s = new HashSet%3C%3E();%0A%09
+Event e) %7B%7D%0A
 %09%0A
-%09
 %09@Ov
@@ -1821,33 +1821,32 @@
 %7B%7D%0A%09%0A%09@Override%0A
-%09
 %09public synchron
@@ -1874,35 +1874,33 @@
 KeyEvent e) %7B%0A%09%09
-%09%0A%09
+%0A
 %09%09pressedKeys.ad
@@ -1920,17 +1920,16 @@
 e());%0A%09%09
-%09
 handleKe
@@ -1946,26 +1946,22 @@
 eys);%0A%09%09
-%09%0A%09
+%0A
 %09%7D%0A%09
-%09%0A%09
+%0A
 %09@Overri
@@ -1963,17 +1963,16 @@
 verride%0A
-%09
 %09public 
@@ -2017,19 +2017,17 @@
  e) %7B%0A%09%09
-%09%0A%09
+%0A
 %09%09presse
@@ -2060,16 +2060,8 @@
 ));%0A
-%09%09%09%0A%09%09%7D%0A
 %09%09%0A%09

Add pause and resume functionality