Java 상속이 있는 예제

1 개요[ | ]

Java 상속이 있는 예제
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;

class Shape {
    protected int x;
    protected int y;
    protected int width;
    protected int height;
    private int textSize;
    protected Color backgroundColor;
    private Color textColor;
    protected String text = null;

    public Shape(int x, int y, int width, int height, Color color) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.backgroundColor = color;
    }

    public Shape setText(String text, int size, Color color) {
        this.text = text;
        this.textSize = size;
        this.textColor = color;
        return this;
    }

    public Shape setText(String text, int size) {
        return setText(text, size, Color.BLACK);
    }

    public void draw(Graphics2D g) {
        drawShape(g);
        drawText(g);
    }

    protected void drawShape(Graphics2D g) {
        // 직사각형
        g.setColor(backgroundColor);
        g.fillRect(x, y, width, height);
    }

    private void drawText(Graphics2D g) {
        if (text == null)
            return;
        g.setColor(textColor);
        Point point = getTextCenter(g);
        g.setFont(g.getFont().deriveFont(textSize));
        g.drawString(text, point.x, point.y);
    }

    protected Point getTextCenter(Graphics2D g) {
        // 정중앙
        FontMetrics metrics = g.getFontMetrics();
        int textX = x + (width - metrics.stringWidth(text)) / 2;
        int textY = y + ((height - metrics.getHeight()) / 2) + metrics.getAscent();
        return new Point(textX, textY);
    }
}
import java.awt.Color;

class MyRectangle extends Shape {
    public MyRectangle(int x, int y, int width, int height, Color color) {
        super(x, y, width, height, color);
    }
}
import java.awt.Color;
import java.awt.Graphics2D;

class MyOval extends Shape {
    public MyOval(int x, int y, int width, int height, Color color) {
        super(x, y, width, height, color);
    }

    protected void drawShape(Graphics2D g) {
        // 타원
        g.setColor(backgroundColor);
        g.fillOval(x, y, width, height);
    }
}
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;

class MyTriangle extends Shape {
    public MyTriangle(int x, int y, int width, int height, Color color) {
        super(x, y, width, height, color);
    }

    protected void drawShape(Graphics2D g) {
        // 삼각형
        g.setColor(backgroundColor);
        int xpoints[] = { x, x + width, x + width / 2 };
        int ypoints[] = { y + height, y + height, y };
        g.fillPolygon(xpoints, ypoints, 3);
    }

    protected Point getTextCenter(Graphics2D g) {
        // 약간 아래로 보정
        FontMetrics metrics = g.getFontMetrics();
        int textX = this.x + (this.width - metrics.stringWidth(this.text)) / 2;
        int textY = this.y + ((this.height - metrics.getHeight()) / 2) + metrics.getAscent() + this.height / 6;
        return new Point(textX, textY);
    }
}
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

import javax.imageio.ImageIO;

class GraphicsController {
    private BufferedImage bufferedImage;
    private Graphics2D g;
    private List<Shape> shapes;

    public GraphicsController(int width, int height, Color color) {
        bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        g = bufferedImage.createGraphics();
        g.fillRect(0, 0, width, height);
        shapes = new ArrayList<Shape>();
    }

    public void publish() {
        for (Shape shape : shapes) {
            shape.draw(g);
        }
        g.dispose();
        try {
            ImageIO.write(bufferedImage, "png", new File("myimage.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addShape(Shape shape) {
        shapes.add(shape);
    }

    public void listShapes() {
        System.out.println(shapes);
    }
}
import java.awt.Color;

public class App {
    public static void main(String[] args) {
        GraphicsController graphicsController = new GraphicsController(640, 480, Color.WHITE);

        // 태양
        graphicsController.addShape(new MyOval(50, 50, 100, 100, Color.ORANGE));

        // 집
        graphicsController.addShape(new MyTriangle(200, 150, 200, 100, Color.decode("#998970")).setText("지붕", 12));
        graphicsController.addShape(new MyRectangle(220, 250, 160, 150, Color.decode("#d4c5ad")));
        graphicsController.addShape(new MyRectangle(250, 270, 80, 80, Color.decode("#abcdef")).setText("창문", 12));

        // 빌딩
        for (int i = 1; i <= 10; i++) {
            Color color = (i % 2 == 0) ? Color.GRAY : Color.DARK_GRAY;
            graphicsController.addShape(
                    (new MyRectangle(500, 400 - (i * 30), 100, 30, color)).setText(String.valueOf(i) + "층", 12));
        }

        // 땅
        graphicsController.addShape(new MyRectangle(0, 400, 640, 80, Color.decode("#804f1d")).setText("제목: 상속이 있는 예제", 12, Color.WHITE));

        // For DEBUG
        // GraphicsController.listShapes();

        graphicsController.publish();
    }
}

2 같이 보기[ | ]