Skip to content

Instantly share code, notes, and snippets.

@max-l
Created March 29, 2018 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-l/72a04d10a974f91b23f5b7b54bb4f08f to your computer and use it in GitHub Desktop.
Save max-l/72a04d10a974f91b23f5b7b54bb4f08f to your computer and use it in GitHub Desktop.
PlasticDimwittedCable created by max_l - https://repl.it/@max_l/PlasticDimwittedCable
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
/*
* Basic geometric shape with an X,Y position
* and an abstract draw method to be implemented
* in subclasses.
* See: https://docs.google.com/document/d/1Ng_W-_yeYQt8FLpdbquHXqdNUUjQjUDbGONPxOSAp60/edit#heading=h.hsuuk4b0auly
*/
abstract class Shape {
private int x;
private int y;
public Shape(int x, int y) {
this.setX(x);
this.setY(y);
}
abstract public void draw(Graphics g);
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String toString() {
return "Shape(" + getX() + "," + getY() + ")";
}
}
/*
* The Insect class adds movement, it has an abstract move() method that updates
* it's x,y coordinate to it's new position, and a abstract draw(Graphics2D)
* method.
*
* Subclasses will implement move() and draw().
*/
abstract class Insect extends Shape {
protected double directionAngle;
protected double speed = 2;
protected Random randGen = new Random();
private double randomAngle() {
return randGen.nextFloat() * 2 * Math.PI;
}
public Insect(int x, int y) {
super(x, y);
directionAngle = randomAngle();
}
abstract public void move();
// see: https://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html
abstract public void draw(Graphics g);
}
class StraightLineMovingInsect extends Insect {
public StraightLineMovingInsect(int x, int y) {
super(x, y);
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillOval(getX(), getY(), 20, 20);
}
public void move() {
double speed = 10;
double deltaX = Math.cos(directionAngle) * speed;
double deltaY = Math.sin(directionAngle) * speed;
setX(getX() + ((int) deltaX));
setY(getY() + ((int) deltaY));
}
}
// class RandomAngleMovingInsect ...
class InsectSandboxPane extends JPanel {
private int width;
private int height;
private ArrayList<Insect> shapes = new ArrayList<Insect>();
public InsectSandboxPane(int width, int height) {
this.width = width;
this.height = height;
}
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Shape s : shapes) {
s.draw((Graphics2D) g);
}
}
public void newInsect(Insect i) {
shapes.add(i);
this.repaint();
}
public void moveAll(MessageListener ml) {
Iterator<Insect> ii = shapes.iterator();
while (ii.hasNext()) {
Insect s = ii.next();
s.move();
if (s.getX() >= width || s.getX() <= 0 || s.getY() >= height
|| s.getY() <= 0) {
ii.remove();
String msg = "removed: " + s + ", " + shapes.size()
+ " left in the sandbox !";
System.out.println(msg);
ml.newMessage(msg);
}
}
this.repaint();
}
}
interface MessageListener {
public void newMessage(String msg);
}
class InsectSandBox extends JFrame {
// Constants :
private final int HEIGHT = 550;
private final int WIDTH = 550;
private final String straightLineInsect = "straightLineInsect";
private final String randomAngleInsect = "randomAngleInsect";
private final String randomAngleSpeedInsect = "randomAngleSpeedInsect";
public InsectSandBox() {
super("Sand Box");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
JPanel rootPanel = new JPanel(new BorderLayout());
this.add(rootPanel);
final InsectSandboxPane sandBox = new InsectSandboxPane(HEIGHT, WIDTH);
rootPanel.add(sandBox, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new GridLayout(3, 1));
rootPanel.add(buttonPanel, BorderLayout.SOUTH);
JRadioButton b1 = new JRadioButton(straightLineInsect);
b1.setActionCommand(straightLineInsect);
JRadioButton b2 = new JRadioButton(randomAngleInsect);
b2.setActionCommand(randomAngleInsect);
JRadioButton b3 = new JRadioButton(randomAngleSpeedInsect);
b3.setActionCommand(randomAngleSpeedInsect);
final ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(b1);
buttonGroup.add(b2);
buttonGroup.add(b3);
JPanel radioPanel = new JPanel(new GridLayout(1, 3));
buttonPanel.add(radioPanel);
radioPanel.add(b1);
radioPanel.add(b2);
radioPanel.add(b3);
b1.setSelected(true);
final Random randGen = new Random();
JButton newInsectButton = new JButton("new insect");
buttonPanel.add(newInsectButton);
// IMPORTANT: Anonymous Class, we create an anonymous ActionListener :
// see:
// https://docs.google.com/document/d/1Ng_W-_yeYQt8FLpdbquHXqdNUUjQjUDbGONPxOSAp60/edit#heading=h.bxqgrwqthxes
newInsectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String command = buttonGroup.getSelection().getActionCommand();
if (command == straightLineInsect) {
System.out.println("ADD straightWalker");
sandBox.newInsect(new StraightLineMovingInsect(
(int) (randGen.nextFloat() * WIDTH),
(int) (randGen.nextFloat() * HEIGHT)
));
}
else if (command == randomAngleInsect) {
System.out.println("ADD randomAngleWalker");
//sandBox.newInsect(new RandomAngleMovingInsect(
// (int) (randGen.nextFloat() * WIDTH),
// (int) (randGen.nextFloat() * HEIGHT)
//));
}
else if (command == randomAngleSpeedInsect) {
System.out.println("ADD randomSpeedAngleWalker");
//sandBox.newInsect(new RandomAngleAndSpeedMovingInsect(
// (int) (randGen.nextFloat() * WIDTH),
// (int) (randGen.nextFloat() * HEIGHT)
//));
}
}
});
this.pack();
this.setVisible(true);
final JLabel mesagesLabel = new JLabel("no messages yet");
buttonPanel.add(mesagesLabel);
// Other anonymous class:
final MessageListener ml = new MessageListener() {
public void newMessage(String msg) {
mesagesLabel.setText(msg);
}
};
// This is where the "moving" is triggered:
Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
// Runs inside of the Swing UI thread
SwingUtilities.invokeLater(new Runnable() {
public void run() {
sandBox.moveAll(ml);
}
});
try {
Thread.sleep(100); // sleep between moves
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.setDaemon(true);
t.start();
}
}
public class Main {
public static void main(String[] args) throws Exception {
InsectSandBox b = new InsectSandBox();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment