From 885defeb4927fdf55b3ab0186bb91d742da98d89 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 20 Feb 2014 19:35:00 +0200 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=88=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D0=B8=20?= =?UTF-8?q?=D1=80=D0=B8=D1=81=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=B1=D0=BB=D0=B0=D1=81=D1=82=D0=B8=2010x7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/annimon/graphics/Application.java | 41 ++++++++++++++++++ src/com/annimon/graphics/GraphicsExt.java | 52 +++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/com/annimon/graphics/Application.java create mode 100644 src/com/annimon/graphics/GraphicsExt.java 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)); + } +}