Guest User

Untitled

a guest
Jul 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. package tutorial.drawing1;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Frame;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Polygon;
  8. import java.awt.event.WindowEvent;
  9. import java.awt.event.WindowListener;
  10.  
  11. import tutorial.common.AbstractDrawing;
  12. import tutorial.common.DrawingSurface;
  13.  
  14. /**
  15.  * The class where the Application is contained and run. It may differ each Tutorial.
  16.  * @author Jesper Eriksson AKA ChJees\Applebloom
  17.  * @since ©2012 Mars 29
  18.  */
  19. public class Application implements AbstractDrawing, WindowListener {
  20.     //Important variables to keep track of.
  21.     Frame window; //Window which draws our DrawingSurface component.
  22.     DrawingSurface gfx; //Here we draw all the things we want.
  23.    
  24.     //Tutorial Specific:
  25.     Polygon polygon; //A Polygon intended for storing a simple one with 3 vertexes\vertices.
  26.    
  27.     /**
  28.      * Construct the Application.
  29.      */
  30.     public Application() {
  31.         //Create Window.
  32.         window = new Frame("Simple Drawing Test");
  33.        
  34.         //Add the component to do the drawing on.
  35.         //Note that we specified this Class where we should do our drawing on.
  36.         window.add(gfx = new DrawingSurface(this));
  37.        
  38.         //Add Listeners.
  39.         window.addWindowListener(this); //Makes this Application listen for window events from window.
  40.        
  41.         //Pack the Window, center and show it.
  42.         window.pack(); //Packs all components neatly according to their wished preferred sizes.
  43.         window.setLocationRelativeTo(null); //If the component is null then it centers the window.
  44.         window.setVisible(true); //Show this window.
  45.        
  46.         //User should not be able to resize it.
  47.         window.setResizable(false);
  48.        
  49.         //Tutorial Specific:
  50.         //Create our Polygon so we can reuse it without having to create a new one all the time.
  51.         //A Polygon needs at least 3 points AKA vertexes\vertices to be shown.
  52.         //    o 2
  53.         //   / \
  54.         //1 o---o 3
  55.         polygon = new Polygon();
  56.         polygon.addPoint(-16, -16); //Adds the first point to the Polygon.
  57.         polygon.addPoint(64, 0);    //Adds the second point to the Polygon.
  58.         polygon.addPoint(16, 32);   //Adds the third point to the Polygon.
  59.     }
  60.    
  61.     /**
  62.      * All applications\games are entering through here.
  63.      * @param args Arguments passed from the commandline. i.e -novideo -destination <..>
  64.      */
  65.     public static void main(String[] args) {
  66.         //Create our Application.
  67.         new Application();
  68.     }
  69.    
  70.     // NOTE:
  71.     // We do the actual painting here.
  72.     // Inherited from our AbstractPainter interface.
  73.     @Override
  74.     public void paint(Graphics g) {
  75.         //Cast a superior drawing interface.
  76.         Graphics2D p = (Graphics2D) g;
  77.        
  78.         //Draw a few simple shapes.
  79.         p.setColor(Color.blue); //Set the Color to draw with to Blue.
  80.         p.fillRect(128, 128, 64, 64); //Draw a Rectangle at position X 128, Y 128
  81.                                       //with the size of X 64, Y 64
  82.        
  83.         p.setColor(Color.red); //Set the Color to draw with to Red.
  84.         p.fillOval(128+32, 128-32, 64, 64); //Draw a Oval, in this case it becomes
  85.                                             //a circle when both the width and height
  86.                                             //are the same.
  87.        
  88.         p.setColor(Color.green);
  89.         p.translate(128, 128+48); //Translate the Polygon so we can draw it where we want.
  90.         p.fillPolygon(polygon);   //When you Translate you actually shift the 0,0 point
  91.                                   //in the coordinate system. bioooooooooookkkö
  92.        
  93.         //Reset Coordinate system
  94.         p.translate(-(128), -(128+48));
  95.        
  96.         // TODO Play around with Drawing functions here :).
  97.     }
  98.  
  99.     @Override
  100.     public void windowActivated(WindowEvent e) {
  101.         //Not used in this Tutorial.
  102.     }
  103.  
  104.     @Override
  105.     public void windowClosed(WindowEvent e) {
  106.         //Not used in this Tutorial.
  107.     }
  108.  
  109.     @Override
  110.     public void windowClosing(WindowEvent e) {
  111.         //Force the Window to close.
  112.         e.getWindow().dispose();
  113.     }
  114.  
  115.     @Override
  116.     public void windowDeactivated(WindowEvent e) {
  117.         //Not used in this Tutorial.
  118.     }
  119.  
  120.     @Override
  121.     public void windowDeiconified(WindowEvent e) {
  122.         //Not used in this Tutorial.
  123.     }
  124.  
  125.     @Override
  126.     public void windowIconified(WindowEvent e) {
  127.         //Not used in this Tutorial.
  128.     }
  129.  
  130.     @Override
  131.     public void windowOpened(WindowEvent e) {
  132.         //Not used in this Tutorial.
  133.     }
  134. }
Add Comment
Please, Sign In to add comment