diff --git a/src/com/annimon/graphics/Application.java b/src/com/annimon/graphics/Application.java new file mode 100644 index 0000000..453f5d8 --- /dev/null +++ b/src/com/annimon/graphics/Application.java @@ -0,0 +1,41 @@ +package com.annimon.graphics; + +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.WindowConstants; + +/** + * + * @author aNNiMON + */ +public abstract class Application extends JFrame { + + private final GraphicsExt graphics; + + public Application(int width, int height) { + setLocationByPlatform(true); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + + final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + graphics = new GraphicsExt(image.getGraphics(), width, height); + + final JPanel panel = new JPanel() { + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Application.this.paint(graphics); + g.drawImage(image, 0, 0, null); + } + }; + panel.setPreferredSize(new Dimension(width, height)); + add(panel); + pack(); + + setVisible(true); + } + + protected abstract void paint(GraphicsExt g); +} diff --git a/src/com/annimon/graphics/GraphicsExt.java b/src/com/annimon/graphics/GraphicsExt.java new file mode 100644 index 0000000..5b68b81 --- /dev/null +++ b/src/com/annimon/graphics/GraphicsExt.java @@ -0,0 +1,52 @@ +package com.annimon.graphics; + +import java.awt.Color; +import java.awt.Graphics; + +/** + * + * @author aNNiMON + */ +public class GraphicsExt { + + public static final double X_MAX = 10d; + public static final double Y_MAX = 7d; + + private final Graphics g; + private final int width, height; + + private double oldX, oldY; + + public GraphicsExt(Graphics g, int width, int height) { + this.g = g; + g.setColor(Color.WHITE); + g.fillRect(0, 0, width, height); + g.setColor(Color.BLACK); + this.width = width; + this.height = height; + oldX = oldY = 0; + } + + public void move(double x, double y) { + oldX = x; + oldY = y; + } + + public void draw(double x, double y) { + line(oldX, oldY, x, y); + oldX = x; + oldY = y; + } + + public void line(double x1, double y1, double x2, double y2) { + g.drawLine(convertX(x1), convertY(y1), convertX(x2), convertY(y2)); + } + + private int convertX(double x) { + return (int) (x * (width / X_MAX) + 0.5d); + } + + private int convertY(double y) { + return (int) (height - (y * (height / Y_MAX) + 0.5d)); + } +}