Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Java Lab 12 : Graphics
- package house;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.image.BufferedImage;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class House {
- public static void main(String[] args) {
- XGraphic Canvas = new XGraphic();
- JFrame Main = new JFrame();
- Main.setResizable(false);
- Main.setTitle("House");
- Main.setSize(486, 389);
- Main.setVisible(true);
- Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Main.add(Canvas);
- }
- }
- class XGraphic extends JPanel {
- // Color codes, used color picker to find values
- private final Color ROOF = Color.decode("#c0504d");
- private final Color CHIMNEY_DOOR = Color.decode("#c00000");
- private final Color TREE = Color.decode("#92d050");
- private final Color HOUSE = Color.decode("#953735");
- private final Color MOON = Color.decode("#d9d9d9");
- private final Color TRUNK = Color.decode("#948a54");
- private final Color YELLOW_WINDOW = Color.decode("#ffff00");
- private final Color GRAY_WINDOW = Color.decode("#bfbfbf");
- private final Color OPENNING_FRAME = Color.decode("#2e6293");
- // Special color for some stars
- private final Color LIGHT_BLUE_STAR = Color.decode("#00ccff");
- private final Color BLUE_STAR = Color.decode("#5e9ae1");
- private final Color GREEN_STAR = Color.decode("#acf55f");
- // Paint with Anti-Aliasing
- private void AApaint(Graphics2D g) {
- // Fill background black
- g.setColor(Color.BLACK);
- g.fillRect(0, 0, 480, 360);
- // Gray moon
- g.setColor(MOON);
- g.fillOval(56, 56, 36, 36);
- g.setColor(Color.BLACK);
- g.fillOval(67, 52, 35, 35);
- // Red chimney
- g.setColor(CHIMNEY_DOOR);
- g.fillRect(246, 82, 24, 44);
- // Light Red Roof
- g.setColor(ROOF);
- g.fillPolygon(new int[]{120, 306, 213}, new int[]{152, 152, 82}, 3);
- // Brown House
- g.setColor(HOUSE);
- g.fillRect(120, 152, 186, 150);
- // Draw windows
- DrawOpenning(g, YELLOW_WINDOW, 146, 176, 26, 30);
- DrawOpenning(g, GRAY_WINDOW, 200, 176, 26, 30);
- DrawOpenning(g, GRAY_WINDOW, 254, 176, 26, 30);
- DrawOpenning(g, GRAY_WINDOW, 146, 232, 26, 30);
- DrawOpenning(g, GRAY_WINDOW, 254, 232, 26, 30);
- // Draw door
- DrawOpenning(g, CHIMNEY_DOOR, 192, 228, 43, 70);
- // Plot some trees
- PlotTree(g, 076, 300, 43, 124);
- PlotTree(g, 358, 296, 60, 194);
- PlotTree(g, 422, 296, 43, 124);
- // Draw some stars
- Star(g, 023, 26, Color.YELLOW);
- Star(g, 175, 10, Color.YELLOW);
- Star(g, 182, 46, Color.YELLOW);
- Star(g, 317, 20, Color.YELLOW);
- Star(g, 296, 80, Color.YELLOW);
- Star(g, 384, 34, Color.YELLOW);
- Star(g, 432, 52, Color.YELLOW);
- Star(g, 116, 70, LIGHT_BLUE_STAR);
- Star(g, 354, 20, LIGHT_BLUE_STAR);
- Star(g, 148, 22, BLUE_STAR);
- Star(g, 234, 28, Color.RED);
- Star(g, 394, 60, Color.RED);
- Star(g, 035, 68, Color.WHITE);
- Star(g, 420, 16, Color.WHITE);
- Star(g, 336, 20, GREEN_STAR);
- }
- // Sets up paint event with antialiasing enabled
- public void paint(Graphics graphics) {
- Graphics2D g = (Graphics2D) graphics;
- BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
- Graphics2D ig = image.createGraphics();
- ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- AApaint(ig);
- g.drawImage(image, null, this);
- }
- // Draws Opennings on a graphic
- private void DrawOpenning(Graphics graphics, Color color, int X, int Y, int Width, int Height) {
- graphics.setColor(OPENNING_FRAME);
- graphics.fillRect(X, Y, Width, Height);
- graphics.setColor(color);
- graphics.fillRect(X + 2, Y + 2, Width - 4, Height - 4);
- }
- // Draws trees on a graphic, constructed an algo to calculate demensions
- private void PlotTree(Graphics graphics, int X, int Y, int Width, int Height) {
- int trunk = (Width / 5) - 1, Y36 = Y - 36, W2 = Width / 2;
- if (trunk % 2 == 0 || trunk < 1) trunk += 1;
- graphics.setColor(TRUNK);
- graphics.fillRect(X - (trunk / 2), Y36, trunk, 36);
- graphics.setColor(TREE);
- graphics.fillPolygon(new int[]{X - (W2), X + (W2), X}, new int[]{Y36, Y36, Y - Height}, 3);
- }
- // Draws stars at specified point
- private void Star(Graphics graphics, int X, int Y, Color color) {
- graphics.setColor(color);
- graphics.fillOval(X, Y, 4, 4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement