Skip to content

Instantly share code, notes, and snippets.

@orekyuu
Created November 28, 2016 11:06
Show Gist options
  • Save orekyuu/c9ec42748dfb10340a0620ad5cd9e223 to your computer and use it in GitHub Desktop.
Save orekyuu/c9ec42748dfb10340a0620ad5cd9e223 to your computer and use it in GitHub Desktop.
回転寿司
package net.orekyuu.sushi;
import java.awt.*;
public class Sushi {
private final Image image;
private int x;
private int y = 0;
private int size = 20;
private int laneWidth;
private int speed = 1;
public Sushi(int x, int laneWidth, Image image) {
this.x = x;
this.image = image;
this.laneWidth = laneWidth;
}
public void drawSushi(Graphics2D g) {
g.drawImage(image, x, y, size, size, null);
}
public void update() {
x += speed;
//端までいったら-sizeまで戻す
if (laneWidth < x) {
x = -size;
}
}
}
package net.orekyuu.sushi;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.StatusBarCustomComponentFactory;
import org.jetbrains.annotations.NotNull;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.LinkedList;
import java.util.List;
public class SushiStatusBarComponentFactory extends StatusBarCustomComponentFactory {
private Image image;
private final int size = 20;
private final int laneWidth = 300;
private List<Sushi> sushiList = new LinkedList<>();
private static Thread thread = null;
private JPanel root;
public SushiStatusBarComponentFactory() {
try (InputStream sushiStream = this.getClass().getResourceAsStream("/sushi.png")){
this.image = ImageIO.read(sushiStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
int sushiCount = (laneWidth / size / 2) + 1;
for (int i = 0; i < sushiCount; i++) {
sushiList.add(new Sushi(i * size * 2 - size, laneWidth, image));
}
if (thread == null) {
thread = new Thread(() -> {
while (true) {
try {
if (root != null) {
sushiList.forEach(Sushi::update);
root.repaint();
}
thread.sleep(16);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.setDaemon(true);
thread.start();
}
}
@Override
public JComponent createComponent(@NotNull StatusBar statusBar) {
root = new JPanel() {
@Override
protected void paintComponent(Graphics graphics) {
Graphics2D g = (Graphics2D) graphics;
g.setBackground(statusBar.getComponent().getBackground());
g.clearRect(0, 0, getWidth(), getHeight());
sushiList.forEach(sushi -> sushi.drawSushi(g));
}
};
root.setPreferredSize(new Dimension(laneWidth, statusBar.getComponent().getHeight()));
return root;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment