September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,131 +0,0 @@
|
|||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class Honeycombs extends JFrame {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JFrame f = new Honeycombs();
|
||||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
f.setVisible(true);
|
||||
});
|
||||
}
|
||||
|
||||
public Honeycombs() {
|
||||
add(new HoneycombsPanel(), BorderLayout.CENTER);
|
||||
setTitle("Honeycombs");
|
||||
setResizable(false);
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
}
|
||||
}
|
||||
|
||||
class HoneycombsPanel extends JPanel {
|
||||
|
||||
Hexagon[] comb;
|
||||
|
||||
public HoneycombsPanel() {
|
||||
setPreferredSize(new Dimension(600, 500));
|
||||
setBackground(Color.white);
|
||||
setFocusable(true);
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
for (Hexagon hex : comb)
|
||||
if (hex.contains(e.getX(), e.getY())) {
|
||||
hex.setSelected();
|
||||
break;
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
addKeyListener(new KeyAdapter() {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
for (Hexagon hex : comb)
|
||||
if (hex.letter == Character.toUpperCase(e.getKeyChar())) {
|
||||
hex.setSelected();
|
||||
break;
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
char[] letters = "LRDGITPFBVOKANUYCESM".toCharArray();
|
||||
comb = new Hexagon[20];
|
||||
|
||||
int x1 = 150, y1 = 100, x2 = 225, y2 = 143, w = 150, h = 87;
|
||||
for (int i = 0; i < comb.length; i++) {
|
||||
int x, y;
|
||||
if (i < 12) {
|
||||
x = x1 + (i % 3) * w;
|
||||
y = y1 + (i / 3) * h;
|
||||
} else {
|
||||
x = x2 + (i % 2) * w;
|
||||
y = y2 + ((i - 12) / 2) * h;
|
||||
}
|
||||
comb[i] = new Hexagon(x, y, w / 3, letters[i]);
|
||||
}
|
||||
|
||||
requestFocus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics gg) {
|
||||
super.paintComponent(gg);
|
||||
Graphics2D g = (Graphics2D) gg;
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
g.setFont(new Font("SansSerif", Font.BOLD, 30));
|
||||
g.setStroke(new BasicStroke(3));
|
||||
|
||||
for (Hexagon hex : comb)
|
||||
hex.draw(g);
|
||||
}
|
||||
}
|
||||
|
||||
class Hexagon extends Polygon {
|
||||
final Color baseColor = Color.yellow;
|
||||
final Color selectedColor = Color.magenta;
|
||||
final char letter;
|
||||
|
||||
private boolean hasBeenSelected;
|
||||
|
||||
Hexagon(int x, int y, int halfWidth, char c) {
|
||||
letter = c;
|
||||
for (int i = 0; i < 6; i++)
|
||||
addPoint((int) (x + halfWidth * Math.cos(i * Math.PI / 3)),
|
||||
(int) (y + halfWidth * Math.sin(i * Math.PI / 3)));
|
||||
getBounds();
|
||||
}
|
||||
|
||||
void setSelected() {
|
||||
hasBeenSelected = true;
|
||||
}
|
||||
|
||||
void draw(Graphics2D g) {
|
||||
g.setColor(hasBeenSelected ? selectedColor : baseColor);
|
||||
g.fillPolygon(this);
|
||||
|
||||
g.setColor(Color.black);
|
||||
g.drawPolygon(this);
|
||||
|
||||
g.setColor(hasBeenSelected ? Color.black : Color.red);
|
||||
drawCenteredString(g, String.valueOf(letter));
|
||||
}
|
||||
|
||||
void drawCenteredString(Graphics2D g, String s) {
|
||||
FontMetrics fm = g.getFontMetrics();
|
||||
int asc = fm.getAscent();
|
||||
int dec = fm.getDescent();
|
||||
|
||||
int x = bounds.x + (bounds.width - fm.stringWidth(s)) / 2;
|
||||
int y = bounds.y + (asc + (bounds.height - (asc + dec)) / 2);
|
||||
|
||||
g.drawString(s, x, y);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,184 +0,0 @@
|
|||
package graphics;
|
||||
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Polygon;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import static java.util.Arrays.parallelSetAll;
|
||||
import static java.util.Arrays.stream;
|
||||
import static java.util.stream.IntStream.range;
|
||||
|
||||
public class Honeycombs extends JFrame {
|
||||
HoneycombsPanel panel;
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame f = new Honeycombs();
|
||||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
f.setVisible(true);
|
||||
}
|
||||
|
||||
public Honeycombs() {
|
||||
Container content = getContentPane();
|
||||
content.setLayout(new BorderLayout());
|
||||
panel = new HoneycombsPanel();
|
||||
content.add(panel, BorderLayout.CENTER);
|
||||
setTitle("Honeycombs");
|
||||
setResizable(false);
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
}
|
||||
}
|
||||
|
||||
class HoneycombsPanel extends JPanel {
|
||||
Hexagon[] comb;
|
||||
|
||||
@FunctionalInterface
|
||||
private static interface MousePressed extends MouseListener {
|
||||
public static MousePressed new_(MousePressed mousePressed) {
|
||||
return mousePressed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public default void mouseClicked(MouseEvent mouseEvent) {}
|
||||
|
||||
@Override
|
||||
public default void mouseEntered(MouseEvent mouseEvent) {}
|
||||
|
||||
@Override
|
||||
public default void mouseExited(MouseEvent mouseEvent) {}
|
||||
|
||||
@Override
|
||||
public default void mouseReleased(MouseEvent mouseEvent) {}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private static interface KeyPressed extends KeyListener {
|
||||
public static KeyPressed new_(KeyPressed keyPressed) {
|
||||
return keyPressed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public default void keyReleased(KeyEvent keyEvent) {}
|
||||
|
||||
@Override
|
||||
public default void keyTyped(KeyEvent keyEvent) {}
|
||||
}
|
||||
|
||||
public HoneycombsPanel() {
|
||||
setPreferredSize(new Dimension(600, 500));
|
||||
setBackground(Color.white);
|
||||
setFocusable(true);
|
||||
|
||||
addMouseListener(MousePressed.new_(e -> {
|
||||
stream(comb)
|
||||
.filter(hex -> hex.contains(e.getX(), e.getY()))
|
||||
.findFirst()
|
||||
.ifPresent(hex -> hex.setSelected())
|
||||
;
|
||||
repaint();
|
||||
}));
|
||||
|
||||
addKeyListener(KeyPressed.new_(e -> {
|
||||
stream(comb)
|
||||
.filter(hex -> hex.letter == Character.toUpperCase(e.getKeyChar()))
|
||||
.findFirst()
|
||||
.ifPresent(hex -> hex.setSelected())
|
||||
;
|
||||
repaint();
|
||||
}));
|
||||
|
||||
char[] letters = "LRDGITPFBVOKANUYCESM".toCharArray();
|
||||
comb = new Hexagon[20];
|
||||
|
||||
int x1 = 150, y1 = 100, x2 = 225, y2 = 143, w = 150, h = 87;
|
||||
parallelSetAll(comb, i -> {
|
||||
int x, y;
|
||||
if (i < 12) {
|
||||
x = x1 + (i % 3) * w;
|
||||
y = y1 + (i / 3) * h;
|
||||
} else {
|
||||
x = x2 + (i % 2) * w;
|
||||
y = y2 + ((i - 12) / 2) * h;
|
||||
}
|
||||
return new Hexagon(x, y, w / 3, letters[i]);
|
||||
});
|
||||
|
||||
requestFocus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics gg) {
|
||||
super.paintComponent(gg);
|
||||
Graphics2D g = (Graphics2D) gg;
|
||||
g.setRenderingHint(
|
||||
RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON
|
||||
);
|
||||
|
||||
g.setFont(new Font("SansSerif", Font.BOLD, 30));
|
||||
g.setStroke(new BasicStroke(3));
|
||||
|
||||
stream(comb)
|
||||
.forEach(hex -> hex.draw(g))
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
class Hexagon extends Polygon {
|
||||
final Color baseColor = Color.yellow;
|
||||
final Color selectedColor = Color.magenta;
|
||||
final char letter;
|
||||
|
||||
private boolean hasBeenSelected;
|
||||
|
||||
Hexagon(int x, int y, int halfWidth, char c) {
|
||||
letter = c;
|
||||
range(0, 6)
|
||||
.forEach(i ->
|
||||
addPoint((int) (x + halfWidth * Math.cos(i * Math.PI / 3)),
|
||||
(int) (y + halfWidth * Math.sin(i * Math.PI / 3)))
|
||||
)
|
||||
;
|
||||
getBounds();
|
||||
}
|
||||
|
||||
void setSelected() {
|
||||
hasBeenSelected = true;
|
||||
}
|
||||
|
||||
void draw(Graphics2D g) {
|
||||
g.setColor(hasBeenSelected ? selectedColor : baseColor);
|
||||
g.fillPolygon(this);
|
||||
|
||||
g.setColor(Color.black);
|
||||
g.drawPolygon(this);
|
||||
|
||||
g.setColor(hasBeenSelected ? Color.black : Color.red);
|
||||
drawCenteredString(g, String.valueOf(letter));
|
||||
}
|
||||
|
||||
void drawCenteredString(Graphics2D g, String s) {
|
||||
FontMetrics fm = g.getFontMetrics();
|
||||
int asc = fm.getAscent();
|
||||
int dec = fm.getDescent();
|
||||
|
||||
int x = bounds.x + (bounds.width - fm.stringWidth(s)) / 2;
|
||||
int y = bounds.y + (asc + (bounds.height - (asc + dec)) / 2);
|
||||
|
||||
g.drawString(s, x, y);
|
||||
}
|
||||
}
|
||||
147
Task/Honeycombs/Kotlin/honeycombs.kotlin
Normal file
147
Task/Honeycombs/Kotlin/honeycombs.kotlin
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
// version 1.1.4
|
||||
|
||||
import java.awt.BasicStroke
|
||||
import java.awt.BorderLayout
|
||||
import java.awt.Color
|
||||
import java.awt.Dimension
|
||||
import java.awt.Font
|
||||
import java.awt.Graphics
|
||||
import java.awt.Graphics2D
|
||||
import java.awt.Polygon
|
||||
import java.awt.RenderingHints
|
||||
import java.awt.event.KeyAdapter
|
||||
import java.awt.event.KeyEvent
|
||||
import java.awt.event.MouseAdapter
|
||||
import java.awt.event.MouseEvent
|
||||
import java.awt.event.WindowEvent
|
||||
import javax.swing.JFrame
|
||||
import javax.swing.JPanel
|
||||
import javax.swing.SwingUtilities
|
||||
|
||||
class Honeycombs : JPanel() {
|
||||
private val comb: Array<Hexagon?> = arrayOfNulls(20)
|
||||
|
||||
init {
|
||||
preferredSize = Dimension(600, 500)
|
||||
background = Color.white
|
||||
isFocusable = true
|
||||
|
||||
addMouseListener(object : MouseAdapter() {
|
||||
override fun mousePressed(e: MouseEvent) {
|
||||
for (hex in comb)
|
||||
if (hex!!.contains(e.x, e.y)) {
|
||||
hex.setSelected()
|
||||
checkForClosure()
|
||||
break
|
||||
}
|
||||
repaint()
|
||||
}
|
||||
})
|
||||
|
||||
addKeyListener(object : KeyAdapter() {
|
||||
override fun keyPressed(e: KeyEvent) {
|
||||
for (hex in comb)
|
||||
if (hex!!.letter == e.keyChar.toUpperCase()) {
|
||||
hex.setSelected()
|
||||
checkForClosure()
|
||||
break
|
||||
}
|
||||
repaint()
|
||||
}
|
||||
})
|
||||
|
||||
val letters = "LRDGITPFBVOKANUYCESM".toCharArray()
|
||||
val x1 = 150
|
||||
val y1 = 100
|
||||
val x2 = 225
|
||||
val y2 = 143
|
||||
val w = 150
|
||||
val h = 87
|
||||
|
||||
for (i in 0 until comb.size) {
|
||||
var x: Int
|
||||
var y: Int
|
||||
if (i < 12) {
|
||||
x = x1 + (i % 3) * w
|
||||
y = y1 + (i / 3) * h
|
||||
}
|
||||
else {
|
||||
x = x2 + (i % 2) * w
|
||||
y = y2 + ((i - 12) / 2) * h
|
||||
}
|
||||
comb[i] = Hexagon(x, y, w / 3, letters[i])
|
||||
}
|
||||
|
||||
requestFocus()
|
||||
}
|
||||
|
||||
override fun paintComponent(gg: Graphics) {
|
||||
super.paintComponent(gg)
|
||||
val g = gg as Graphics2D
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON)
|
||||
g.font = Font("SansSerif", Font.BOLD, 30)
|
||||
g.stroke = BasicStroke(3.0f)
|
||||
for (hex in comb) hex!!.draw(g)
|
||||
}
|
||||
|
||||
private fun checkForClosure() {
|
||||
if (comb.all { it!!.hasBeenSelected } ) {
|
||||
val f = SwingUtilities.getWindowAncestor(this) as JFrame
|
||||
f.dispatchEvent(WindowEvent(f, WindowEvent.WINDOW_CLOSING))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Hexagon(x: Int, y: Int, halfWidth: Int, c: Char) : Polygon() {
|
||||
private val baseColor = Color.yellow
|
||||
private val selectedColor = Color.magenta
|
||||
var hasBeenSelected = false
|
||||
val letter = c
|
||||
|
||||
init {
|
||||
for (i in 0..5)
|
||||
addPoint((x + halfWidth * Math.cos(i * Math.PI / 3.0)).toInt(),
|
||||
(y + halfWidth * Math.sin(i * Math.PI / 3.0)).toInt())
|
||||
getBounds()
|
||||
}
|
||||
|
||||
fun setSelected() {
|
||||
hasBeenSelected = true
|
||||
}
|
||||
|
||||
fun draw(g: Graphics2D) {
|
||||
with(g) {
|
||||
color = if (hasBeenSelected) selectedColor else baseColor
|
||||
fillPolygon(this@Hexagon)
|
||||
color = Color.black
|
||||
drawPolygon(this@Hexagon)
|
||||
color = if (hasBeenSelected) Color.black else Color.red
|
||||
drawCenteredString(g, letter.toString())
|
||||
}
|
||||
}
|
||||
|
||||
private fun drawCenteredString(g: Graphics2D, s: String) {
|
||||
val fm = g.fontMetrics
|
||||
val asc = fm.ascent
|
||||
val dec = fm.descent
|
||||
val x = bounds.x + (bounds.width - fm.stringWidth(s)) / 2
|
||||
val y = bounds.y + (asc + (bounds.height - (asc + dec)) / 2)
|
||||
g.drawString(s, x, y)
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
SwingUtilities.invokeLater {
|
||||
val f = JFrame()
|
||||
with(f) {
|
||||
defaultCloseOperation = JFrame.EXIT_ON_CLOSE
|
||||
add(Honeycombs(), BorderLayout.CENTER)
|
||||
title = "Honeycombs"
|
||||
isResizable = false
|
||||
pack()
|
||||
setLocationRelativeTo(null)
|
||||
isVisible = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -63,7 +63,7 @@ class Honeycombs(
|
|||
}
|
||||
|
||||
method display(title) {
|
||||
fork {
|
||||
{
|
||||
var mw = %s'MainWindow'.new('-title' => title)
|
||||
var canvas = mw.Canvas('-width' => 8*size,
|
||||
'-height' => 8*size).pack
|
||||
|
|
@ -77,7 +77,7 @@ class Honeycombs(
|
|||
).pack
|
||||
mw.bind('<Alt-q>', { btn.invoke })
|
||||
tk.MainLoop()
|
||||
}
|
||||
}.fork
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue