Last active
December 30, 2022 17:00
-
-
Save FlorianLatapie/e56b7ccb152d8851c8944339a60de1ca to your computer and use it in GitHub Desktop.
I got lost on Instagram and wanted to recreate what I saw.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.util.Random; | |
import static javax.swing.SwingConstants.CENTER; | |
public class AreYouDumb extends JFrame { | |
private final JLabel label; | |
private final Random random; | |
private static final int SIZE = 255; | |
private static final String FONT_NAME = "Comic Sans MS"; | |
public AreYouDumb() { | |
// JFrame | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
setLayout(new BorderLayout()); | |
setSize(SIZE, SIZE); | |
setLocationRelativeTo(null); | |
// Other attributes | |
random = new Random(); | |
// Label | |
label = new JLabel("Are you dumb?"); | |
label.setHorizontalAlignment(CENTER); | |
label.setFont(new Font(FONT_NAME, Font.BOLD, SIZE / 10)); | |
add(label, BorderLayout.CENTER); | |
// add the two button on south | |
JButton yes = new JButton("Yes"); | |
// change the size of the button | |
yes.setPreferredSize(new Dimension(SIZE / 5, SIZE / 10)); | |
yes.setFont(new Font(FONT_NAME, Font.BOLD, SIZE / 26)); | |
JButton no = new JButton("No"); | |
no.setPreferredSize(new Dimension(SIZE / 5, SIZE / 10)); | |
no.setFont(new Font(FONT_NAME, Font.BOLD, SIZE / 26)); | |
JPanel south = new JPanel(); | |
south.setLayout(new BorderLayout()); | |
south.add(yes, BorderLayout.WEST); | |
south.add(no, BorderLayout.EAST); | |
int borderSize = SIZE / 5; | |
south.setBorder(BorderFactory.createEmptyBorder(borderSize, borderSize, borderSize, borderSize)); | |
add(south, BorderLayout.SOUTH); | |
// add the listener to the buttons | |
no.addActionListener(e -> noAction(no, south)); | |
// the same thing but with the mouse hovering the button | |
no.addMouseListener(new MouseAdapter() { | |
@Override | |
public void mouseEntered(MouseEvent e) { | |
noAction(no, south); | |
} | |
}); | |
yes.addActionListener(e -> { | |
label.setText("I knew it!"); | |
south.setVisible(false); | |
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); | |
}); | |
// make the window visible | |
setVisible(true); | |
} | |
/** | |
* This method is called when the user click on the "no" button | |
* @param jButton the button to move | |
* @param jPanel the container of the button | |
*/ | |
private void noAction(JButton jButton, JPanel jPanel) { | |
var randomBound = (int) (SIZE / 2.5); | |
var offsetX = random.nextInt(-randomBound, randomBound); | |
var offsetY = random.nextInt(-randomBound, randomBound); | |
var newX = jButton.getX() + offsetX; | |
var newY = jButton.getY() + offsetY; | |
// make sure that the button is not out of the container | |
newX = Math.max(0, Math.min(jPanel.getWidth() - jButton.getWidth(), newX)); | |
newY = Math.max(0, Math.min(jPanel.getHeight() - jButton.getHeight(), newY)); | |
jButton.setLocation(newX, newY); | |
} | |
public static void main(String[] args) { | |
new AreYouDumb(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment