Skip to content

Instantly share code, notes, and snippets.

@MasterAlish
Created February 16, 2018 09:39
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 MasterAlish/0efc33ce4a20b449aac43ea37b85e5ef to your computer and use it in GitHub Desktop.
Save MasterAlish/0efc33ce4a20b449aac43ea37b85e5ef to your computer and use it in GitHub Desktop.
Primitives Animation on Java
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
public class DrawerPanel extends JPanel {
ArrayList<Shape> shapes = new ArrayList<Shape>();
public boolean animating = false;
@Override
public void paint(Graphics g) {
super.paint(g);
setDoubleBuffered(true);
for (Shape shape : shapes) {
shape.draw(g);
}
}
public void addRectangle() {
Rect rect = new Rect();
rect.randomFill();
shapes.add(rect);
}
public void addCircle() {
Circle circle = new Circle();
circle.randomFill();
shapes.add(circle);
}
public void nextState() {
for (Shape shape : shapes) {
shape.y += shape.speed;
if (shape.y <= 0 || shape.y >= 180) {
shape.speed = -shape.speed;
}
}
repaint();
}
}
class Shape {
public int x, y;
public int width, height;
public int speed = +2;
public Color color = Color.RED;
public int randint(int min, int max) {
Random rnd = new Random();
return min + rnd.nextInt(max - min);
}
public Color randomColor() {
Color[] colors = new Color[]{new Color(0, 255, 0), new Color(0, 255, 255), new Color(0, 169, 255),
new Color(106, 0, 255), new Color(200, 50, 255), new Color(255, 60, 200), new Color(255, 80, 80)};
return colors[randint(0, colors.length)];
}
public void randomFill() {
this.x = randint(20, 380);
this.y = randint(20, 180);
this.width = randint(10, 40);
this.height = randint(10, 40);
this.color = randomColor();
}
public void draw(Graphics g) {
}
}
class Rect extends Shape {
@Override
public void draw(Graphics g) {
g.setColor(color);
g.fillRect(x, y, width, height);
}
}
class Circle extends Shape {
@Override
public void randomFill() {
super.randomFill();
this.width = this.height;
}
@Override
public void draw(Graphics g) {
g.setColor(color);
g.fillOval(x, y, width, height);
}
}
import javax.swing.*;
import javax.swing.text.html.Option;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.*;
import java.util.Timer;
public class PrimitivesApp {
private final Button animateButton;
private final Button addButton;
private final JComboBox shapesList;
private JFrame frame;
private DrawerPanel drawerPanel;
public static void main(String[] args) {
PrimitivesApp app = new PrimitivesApp();
app.start();
}
public PrimitivesApp(){
frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(400, 260);
drawerPanel = new DrawerPanel();
drawerPanel.setSize(400, 200);
Container mainContainer = frame.getContentPane();
mainContainer.setLayout(new BorderLayout());
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 3));
bottomPanel.setSize(400, 30);
mainContainer.add(bottomPanel, BorderLayout.SOUTH);
mainContainer.add(drawerPanel, BorderLayout.CENTER);
shapesList = new JComboBox(new String[]{"Rectangle", "Circle"});
shapesList.setSelectedIndex(0);
bottomPanel.add(shapesList);
addButton = new Button("Add");
addButton.addActionListener(e -> onAddClick());
bottomPanel.add(addButton);
animateButton = new Button("Animate");
animateButton.addActionListener(e -> onAnimateClick());
bottomPanel.add(animateButton);
}
private void onAddClick() {
String selected = shapesList.getSelectedItem().toString();
if(selected.equals("Rectangle")){
drawerPanel.addRectangle();
}else if(selected.equals("Circle")){
drawerPanel.addCircle();
}
drawerPanel.repaint();
}
private void onAnimateClick() {
drawerPanel.animating = !drawerPanel.animating;
animateButton.setLabel(drawerPanel.animating?"Stop":"Animate");
}
private void start() {
frame.setVisible(true);
startTimer();
}
private void startTimer() {
TimerTask task = new TimerTask() {
@Override
public void run() {
if(drawerPanel.animating) {
drawerPanel.nextState();
}
}
};
Timer timer = new Timer();
timer.schedule(task, 0, 10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment