Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tutorial.drawing1;
- import java.awt.Color;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Polygon;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import tutorial.common.AbstractDrawing;
- import tutorial.common.DrawingSurface;
- /**
- * The class where the Application is contained and run. It may differ each Tutorial.
- * @author Jesper Eriksson AKA ChJees\Applebloom
- * @since ©2012 Mars 29
- */
- public class Application implements AbstractDrawing, WindowListener {
- //Important variables to keep track of.
- Frame window; //Window which draws our DrawingSurface component.
- DrawingSurface gfx; //Here we draw all the things we want.
- //Tutorial Specific:
- Polygon polygon; //A Polygon intended for storing a simple one with 3 vertexes\vertices.
- /**
- * Construct the Application.
- */
- public Application() {
- //Create Window.
- window = new Frame("Simple Drawing Test");
- //Add the component to do the drawing on.
- //Note that we specified this Class where we should do our drawing on.
- window.add(gfx = new DrawingSurface(this));
- //Add Listeners.
- window.addWindowListener(this); //Makes this Application listen for window events from window.
- //Pack the Window, center and show it.
- window.pack(); //Packs all components neatly according to their wished preferred sizes.
- window.setLocationRelativeTo(null); //If the component is null then it centers the window.
- window.setVisible(true); //Show this window.
- //User should not be able to resize it.
- window.setResizable(false);
- //Tutorial Specific:
- //Create our Polygon so we can reuse it without having to create a new one all the time.
- //A Polygon needs at least 3 points AKA vertexes\vertices to be shown.
- // o 2
- // / \
- //1 o---o 3
- polygon = new Polygon();
- polygon.addPoint(-16, -16); //Adds the first point to the Polygon.
- polygon.addPoint(64, 0); //Adds the second point to the Polygon.
- polygon.addPoint(16, 32); //Adds the third point to the Polygon.
- }
- /**
- * All applications\games are entering through here.
- * @param args Arguments passed from the commandline. i.e -novideo -destination <..>
- */
- public static void main(String[] args) {
- //Create our Application.
- new Application();
- }
- // NOTE:
- // We do the actual painting here.
- // Inherited from our AbstractPainter interface.
- @Override
- public void paint(Graphics g) {
- //Cast a superior drawing interface.
- Graphics2D p = (Graphics2D) g;
- //Draw a few simple shapes.
- p.setColor(Color.blue); //Set the Color to draw with to Blue.
- p.fillRect(128, 128, 64, 64); //Draw a Rectangle at position X 128, Y 128
- //with the size of X 64, Y 64
- p.setColor(Color.red); //Set the Color to draw with to Red.
- p.fillOval(128+32, 128-32, 64, 64); //Draw a Oval, in this case it becomes
- //a circle when both the width and height
- //are the same.
- p.setColor(Color.green);
- p.translate(128, 128+48); //Translate the Polygon so we can draw it where we want.
- p.fillPolygon(polygon); //When you Translate you actually shift the 0,0 point
- //in the coordinate system. bioooooooooookkkö
- //Reset Coordinate system
- p.translate(-(128), -(128+48));
- // TODO Play around with Drawing functions here :).
- }
- @Override
- public void windowActivated(WindowEvent e) {
- //Not used in this Tutorial.
- }
- @Override
- public void windowClosed(WindowEvent e) {
- //Not used in this Tutorial.
- }
- @Override
- public void windowClosing(WindowEvent e) {
- //Force the Window to close.
- e.getWindow().dispose();
- }
- @Override
- public void windowDeactivated(WindowEvent e) {
- //Not used in this Tutorial.
- }
- @Override
- public void windowDeiconified(WindowEvent e) {
- //Not used in this Tutorial.
- }
- @Override
- public void windowIconified(WindowEvent e) {
- //Not used in this Tutorial.
- }
- @Override
- public void windowOpened(WindowEvent e) {
- //Not used in this Tutorial.
- }
- }
Add Comment
Please, Sign In to add comment