Skip to content

Instantly share code, notes, and snippets.

@mrmemmo
Last active July 2, 2019 16:35
Show Gist options
  • Select an option

  • Save mrmemmo/229eb2ddf6d680ed50ccafb96834c6b5 to your computer and use it in GitHub Desktop.

Select an option

Save mrmemmo/229eb2ddf6d680ed50ccafb96834c6b5 to your computer and use it in GitHub Desktop.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Font;
import java.awt.FontMetrics;
public class DrawingApp {
private JFrame frame;
public DrawingApp() {
frame = new JFrame("Mine Sweeper");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(frame.getSize());
frame.add(new DrawFace(frame.getSize()));
frame.pack();
frame.setVisible(true);
}
public static void main(String... argv) {
new DrawingApp();
}
public static class DrawFace extends JPanel implements MouseListener {
/*
* Declare Class Variables Here
*/
int x = 20;
int y = 20;
public DrawFace(Dimension dimension) {
setSize(dimension);
setPreferredSize(dimension);
addMouseListener(this);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;//g2 is the graphics object that we need to use
//to draw things to the screen
Dimension d = getSize();
//create a background
g2.setColor(Color.white);
g2.fillRect(0, 0, d.width, d.height);
/**/
//face
g2.setColor(Color.yellow); // yellow is a field of class Color
g2.fillOval(100,100, 350, 350);
//eyes
g2.setColor(Color.black);// ...so is black...
g2.fillOval(175,175, 50, 50);
g2.setColor(Color.black);
g2.fillOval(320,175, 50, 50);
//mouth
g2.setColor(Color.red);// ...and red.
g2.fillOval(190,275, 175, 125);
g2.setColor(Color.yellow);
g2.fillOval(190,250, 175, 125);
//hair
//brown becmones a new instance of the Color class...
Color brown = new Color(101,0,0);
//...& brown can now be used as parameter in method setColor
g2.setColor(brown);
g2.fillOval(60,130, 75, 75);
g2.fillOval(100,100, 75, 75);
g2.fillOval(140,70, 75, 75);
g2.fillOval(180,55, 75, 75);
g2.fillOval(220,55, 75, 75);
g2.fillOval(260,60, 75, 75);
g2.fillOval(300,80, 75, 75);
g2.fillOval(340,100, 75, 75);
g2.fillOval(380,110, 75, 75);
//nose
Color orange = new Color(255,128,0);//instance variable orange
g2.setColor(orange);
g2.fillOval(275,280,10, 10);
//hat
Color purple= new Color(102,0, 102); //instance variable purple
g2.setColor(purple);
g2.fillRect(75, 75, 375, 50);
g2.fillRect(150, 25, 225, 100);
//display Text
g2.setColor(Color.red);
g2.setFont (new Font("TimesRoman", Font.PLAIN, 20));
g2.drawString("Hello, World" , x,y);//text to display, x and y coordinates
}
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();//updates the paint method
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
//DO NOT USE THE METHOD BELOW
public void mouseClicked(MouseEvent e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment