Matrix digital rain: Difference between revisions
m
Uploaded a new screenshot.
m (Added a comment.) |
m (Uploaded a new screenshot.) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1,349:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
Line 1,380 ⟶ 1,381:
setBackground(Color.BLACK);
columnCount = aWidth /
rowCount = aHeight /
setCursor(getToolkit().createCustomCursor(
Line 1,392 ⟶ 1,393:
public void start() {
requestFocus();
createBufferStrategy(2);
executiveService.execute( new DrawingCycle(rowCount) );
}
Line 1,400 ⟶ 1,401:
public DrawingCycle(int rowCount) {
columns = Stream.generate( () -> new Column(rowCount) )
.limit(columnCount
bufferStrategy = getBufferStrategy();
scheduler = Executors.newSingleThreadScheduledExecutor();
}
@Override
public void run() {
scheduler.scheduleAtFixedRate( () -> { draw(); update(); }, 0, 100, TimeUnit.MILLISECONDS);
}
Line 1,417 ⟶ 1,416:
graphics2D.fillRect(0, 0, getWidth(), getHeight());
for ( int col = 0; col < columnCount
for ( int row = 0; row < rowCount; row++ ) {
Symbol symbol = columns.get(col).symbols.get(row);
graphics2D.setFont(symbol.font);
graphics2D.setColor(symbol.color());
final int size = symbol.font.getSize();
graphics2D.drawString(
symbol.element,
2 * halfColumnWidth * col + halfColumnWidth + ( 6 * halfFontSize - size ) / 2,
row * 2 * halfFontSize);
}
}
Line 1,430 ⟶ 1,433:
}
private void update(
for ( Column column : columns ) {
if ( column.index >= 0 ) {
Line 1,443 ⟶ 1,446:
column.reset();
}
}
private final ScheduledExecutorService scheduler;
▲ private final BufferStrategy bufferStrategy;
▲ private final List<Column> columns;
} // End DrawingCycle class
Line 1,477 ⟶ 1,475:
private void setFont() {
final int fontSize = ( random.nextInt(2) == 0 ) ?
2 * halfFontSize : ( random.nextInt(2) == 0 ) ?
(int) ( 1.5 * halfFontSize ) : 3 * halfFontSize;
final int fontStyle = ( random.nextInt(3) == 0 ) ? Font.BOLD : Font.PLAIN;
font = new Font("Dialog", fontStyle, fontSize);
Line 1,524:
private final int rowCount;
private final ExecutorService executiveService;
private final int
private final int halfColumnWidth = 10;
private final ThreadLocalRandom random;
private final List<String> elements = List.of(
Line 1,550 ⟶ 1,551:
{{ out }}
A screenshot from the running program.
[[Media:
=={{header|Javascript}}==
|