Advertisement
TizzyT

Lab 12 - TizzyT

Apr 16th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.80 KB | None | 0 0
  1. // Java Lab 12 : Graphics
  2. package house;
  3.  
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.RenderingHints;
  8. import java.awt.image.BufferedImage;
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11.  
  12. public class House {
  13.     public static void main(String[] args) {
  14.         XGraphic Canvas = new XGraphic();
  15.         JFrame Main = new JFrame();
  16.         Main.setResizable(false);
  17.         Main.setTitle("House");
  18.         Main.setSize(486, 389);
  19.         Main.setVisible(true);
  20.         Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21.         Main.add(Canvas);
  22.     }
  23. }
  24.  
  25. class XGraphic extends JPanel {
  26.     // Color codes, used color picker to find values
  27.     private final Color ROOF = Color.decode("#c0504d");
  28.     private final Color CHIMNEY_DOOR = Color.decode("#c00000");
  29.     private final Color TREE = Color.decode("#92d050");
  30.     private final Color HOUSE = Color.decode("#953735");
  31.     private final Color MOON = Color.decode("#d9d9d9");
  32.     private final Color TRUNK = Color.decode("#948a54");
  33.     private final Color YELLOW_WINDOW = Color.decode("#ffff00");
  34.     private final Color GRAY_WINDOW = Color.decode("#bfbfbf");
  35.     private final Color OPENNING_FRAME = Color.decode("#2e6293");
  36.     // Special color for some stars
  37.     private final Color LIGHT_BLUE_STAR = Color.decode("#00ccff");
  38.     private final Color BLUE_STAR = Color.decode("#5e9ae1");
  39.     private final Color GREEN_STAR = Color.decode("#acf55f");
  40.     // Paint with Anti-Aliasing
  41.     private void AApaint(Graphics2D g) {
  42.         // Fill background black
  43.         g.setColor(Color.BLACK);
  44.         g.fillRect(0, 0, 480, 360);
  45.         // Gray moon
  46.         g.setColor(MOON);
  47.         g.fillOval(56, 56, 36, 36);
  48.         g.setColor(Color.BLACK);
  49.         g.fillOval(67, 52, 35, 35);
  50.         // Red chimney
  51.         g.setColor(CHIMNEY_DOOR);
  52.         g.fillRect(246, 82, 24, 44);
  53.         // Light Red Roof
  54.         g.setColor(ROOF);
  55.         g.fillPolygon(new int[]{120, 306, 213}, new int[]{152, 152, 82}, 3);
  56.         // Brown House
  57.         g.setColor(HOUSE);
  58.         g.fillRect(120, 152, 186, 150);
  59.         // Draw windows
  60.         DrawOpenning(g, YELLOW_WINDOW, 146, 176, 26, 30);
  61.         DrawOpenning(g, GRAY_WINDOW, 200, 176, 26, 30);
  62.         DrawOpenning(g, GRAY_WINDOW, 254, 176, 26, 30);
  63.         DrawOpenning(g, GRAY_WINDOW, 146, 232, 26, 30);
  64.         DrawOpenning(g, GRAY_WINDOW, 254, 232, 26, 30);
  65.         // Draw door
  66.         DrawOpenning(g, CHIMNEY_DOOR, 192, 228, 43, 70);
  67.         // Plot some trees
  68.         PlotTree(g, 076, 300, 43, 124);
  69.         PlotTree(g, 358, 296, 60, 194);
  70.         PlotTree(g, 422, 296, 43, 124);
  71.         // Draw some stars
  72.         Star(g, 023, 26, Color.YELLOW);
  73.         Star(g, 175, 10, Color.YELLOW);
  74.         Star(g, 182, 46, Color.YELLOW);
  75.         Star(g, 317, 20, Color.YELLOW);
  76.         Star(g, 296, 80, Color.YELLOW);
  77.         Star(g, 384, 34, Color.YELLOW);
  78.         Star(g, 432, 52, Color.YELLOW);        
  79.         Star(g, 116, 70, LIGHT_BLUE_STAR);
  80.         Star(g, 354, 20, LIGHT_BLUE_STAR);        
  81.         Star(g, 148, 22, BLUE_STAR);        
  82.         Star(g, 234, 28, Color.RED);
  83.         Star(g, 394, 60, Color.RED);        
  84.         Star(g, 035, 68, Color.WHITE);
  85.         Star(g, 420, 16, Color.WHITE);        
  86.         Star(g, 336, 20, GREEN_STAR);
  87.     }
  88.     // Sets up paint event with antialiasing enabled
  89.     public void paint(Graphics graphics) {
  90.         Graphics2D g = (Graphics2D) graphics;
  91.         BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
  92.         Graphics2D ig = image.createGraphics();
  93.         ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  94.         AApaint(ig);
  95.         g.drawImage(image, null, this);
  96.     }
  97.     // Draws Opennings on a graphic
  98.     private void DrawOpenning(Graphics graphics, Color color, int X, int Y, int Width, int Height) {
  99.         graphics.setColor(OPENNING_FRAME);
  100.         graphics.fillRect(X, Y, Width, Height);
  101.         graphics.setColor(color);
  102.         graphics.fillRect(X + 2, Y + 2, Width - 4, Height - 4);
  103.     }
  104.     // Draws trees on a graphic, constructed an algo to calculate demensions
  105.     private void PlotTree(Graphics graphics, int X, int Y, int Width, int Height) {
  106.         int trunk = (Width / 5) - 1, Y36 = Y - 36, W2 = Width / 2;
  107.         if (trunk % 2 == 0 || trunk < 1) trunk += 1;
  108.         graphics.setColor(TRUNK);
  109.         graphics.fillRect(X - (trunk / 2), Y36, trunk, 36);
  110.         graphics.setColor(TREE);
  111.         graphics.fillPolygon(new int[]{X - (W2), X + (W2), X}, new int[]{Y36, Y36, Y - Height}, 3);
  112.     }
  113.     // Draws stars at specified point
  114.     private void Star(Graphics graphics, int X, int Y, Color color) {
  115.         graphics.setColor(color);
  116.         graphics.fillOval(X, Y, 4, 4);
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement