Добавлены шаблоны классов для поддержки рисования на области 10x7
This commit is contained in:
parent
72562bfe3f
commit
885defeb49
41
src/com/annimon/graphics/Application.java
Normal file
41
src/com/annimon/graphics/Application.java
Normal file
@ -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);
|
||||||
|
}
|
52
src/com/annimon/graphics/GraphicsExt.java
Normal file
52
src/com/annimon/graphics/GraphicsExt.java
Normal file
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user