hw4: init
This commit is contained in:
71
hw4/src/ShapeDisplayer.java
Normal file
71
hw4/src/ShapeDisplayer.java
Normal file
@@ -0,0 +1,71 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
|
||||
public class ShapeDisplayer {
|
||||
public static void main(String[] args) {
|
||||
// Create and configure the main frame on the Event Dispatch Thread
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JFrame frame = new JFrame("Mancala");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setSize(800, 400);
|
||||
|
||||
// Create buttons panel
|
||||
JPanel buttonPanel = new JPanel();
|
||||
buttonPanel.setLayout(new FlowLayout());
|
||||
|
||||
// Create placeholder shapes
|
||||
JButton carButton = new JButton(new PlaceholderIcon(Color.RED));
|
||||
JButton snowmanButton = new JButton(new PlaceholderIcon(Color.BLUE));
|
||||
JButton compositeButton = new JButton(new PlaceholderIcon(Color.GREEN));
|
||||
|
||||
|
||||
// Add buttons to the panel
|
||||
buttonPanel.add(carButton);
|
||||
buttonPanel.add(snowmanButton);
|
||||
buttonPanel.add(compositeButton);
|
||||
|
||||
// Add buttons panel to the top of the frame
|
||||
frame.add(buttonPanel, BorderLayout.NORTH);
|
||||
|
||||
// Add drawing panel
|
||||
JPanel drawingPanel = new JPanel();
|
||||
drawingPanel.setBackground(Color.WHITE);
|
||||
frame.add(drawingPanel, BorderLayout.CENTER);
|
||||
|
||||
// Center the frame on the screen and make it visible
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class PlaceholderIcon implements Icon {
|
||||
private final Color color;
|
||||
private static final int WIDTH = 60;
|
||||
private static final int HEIGHT = 40;
|
||||
|
||||
public PlaceholderIcon(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintIcon(Component c, Graphics g, int x, int y) {
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
Rectangle2D.Double rect = new Rectangle2D.Double(x + 5, y + 5, WIDTH - 10, HEIGHT - 10);
|
||||
g2.setColor(color);
|
||||
g2.fill(rect);
|
||||
g2.setColor(Color.BLACK);
|
||||
g2.draw(rect);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIconWidth() {
|
||||
return WIDTH;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIconHeight() {
|
||||
return HEIGHT;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user