Skip to content

Instantly share code, notes, and snippets.

@ornirus
Last active January 17, 2017 12:08
Show Gist options
  • Save ornirus/4676d91c7a759bcfea98a4efb9bb8702 to your computer and use it in GitHub Desktop.
Save ornirus/4676d91c7a759bcfea98a4efb9bb8702 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
public class GUI extends JFrame
{
private JButton upButton;
private JButton leftButton;
private JButton rightButton;
private JButton downButton;
private JPanel panel;
public GUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout());
upButton = new JButton("Up");
window.add(upButton, BorderLayout.PAGE_START);
upButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent actionEvent) {
draw("up");
}
});
leftButton = new JButton("Left");
window.add(leftButton, BorderLayout.LINE_START);
leftButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent actionEvent) {
draw("left");
}
});
panel = new JPanel();
panel.setPreferredSize(new Dimension(701, 701));
panel.setBackground(Color.lightGray);
window.add(panel, BorderLayout.CENTER);
rightButton = new JButton("Right");
window.add(rightButton, BorderLayout.LINE_END);
rightButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent actionEvent) {
draw("right");
}
});
downButton = new JButton("Down");
window.add(downButton, BorderLayout.PAGE_END);
downButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent actionEvent) {
draw("down");
}
});
}
public void draw(String d) {
BufferedImage text = new BufferedImage(701, 701, BufferedImage.TYPE_INT_ARGB);
Graphics paper = text.getGraphics();
paper.setColor(Color.lightGray);
paper.fillRect(0,0,701,701);
paper.setFont(new Font("TimesRoman", Font.PLAIN, 20));
paper.setColor(Color.black);
paper.drawString("Heading " + d, 280, 360);
panel.getGraphics().drawImage(text, 0, 0, this);
}
}
public class Main {
public static void main(String[] args) {
GUI frame = new GUI();
frame.setSize(800, 800);
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment