Download simpleDrawingFramework-0.1.09.1.jar
Aufgabe 7 Zeichne eine Sinuskurve auf ein Gitter.
Panel
17 18 19 20 21 22 23 24 25 26 | public static void main(String[] args) { JFrame frame = new JFrame("Painter"); final Controller controller = new Controller(); Container container = frame.getContentPane(); container.add(controller.getView()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(Config.WINDOW_WIDTH, Config.WINDOW_HEIGHT); frame.setVisible(true); frame.setBackground(Color.WHITE); } |
Model
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class Model { List<Drawable> list; public Model() { this.list = new LinkedList<>(); } public List<Drawable> getAll() { return list; } public Drawable get(int index) { return list.get(index); } public void add(Drawable figure) { list.add(figure); } } |
View
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Canvas extends JPanel { protected Model model; public Canvas(final Model model) { super(); this.model = model; setBackground(Config.BACKGROUND_COLOR); } @Override public void paintComponent(final Graphics g) { super.paintComponent(g); for (int i = 0; i < model.getAll().size(); ++i) { model.getAll().get(i).draw(g); } } } |
Controller
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public class Controller { protected Model model; protected Canvas view; public Controller() { this.model = new Model(); this.view = new Canvas(model); view.setFocusable(true); sinus(); } private void sinus() { Gitter gitter = new Gitter(100, 100, Color.YELLOW); double amplitude = 200; double length = 2 * Math.PI * 100; Sinuskurve sinus = new Sinuskurve(amplitude, length, Color.RED); model.add(gitter); model.add(sinus); } public JPanel getView() { return view; } } |
Drawables
Gitter
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | final public class Gitter implements Drawable { final protected double dx; final protected double dy; protected Color color; protected Gitter() { this.dx = 0; this.dy = 0; color = Color.BLACK; } public Gitter(double dx, double dy, Color color) { this.dx = dx; this.dy = dy; this.color = new Color(color.getRGB()); } public Gitter(Gitter other) { this(other.dx, other.dy, other.color); } @Override public void draw(Graphics g) { Color oldColor = g.getColor(); g.setColor(Color.BLACK); for (int x = 0; x < 1100; x += dx) { g.drawLine(x, 0, x, 1100); g.drawLine(0, x, 1100, x); } g.setColor(oldColor); } public double getDx() { return dx; } public double getDy() { return dy; } public Color getColor() { return color; } public void setColor(Color color) { this.color = new Color(color.getRGB()); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((color == null) ? 0 : color.hashCode()); long temp; temp = Double.doubleToLongBits(dx); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(dy); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof Gitter)) { return false; } Gitter other = (Gitter) obj; if (color == null) { if (other.color != null) { return false; } } else if (!color.equals(other.color)) { return false; } if (Double.doubleToLongBits(dx) != Double.doubleToLongBits(other.dx)) { return false; } if (Double.doubleToLongBits(dy) != Double.doubleToLongBits(other.dy)) { return false; } return true; } public String toString() { return "dx=" + dx + ",dy=" + dy; } } |
Sinuskurve
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | final public class Gitter implements Drawable { final protected double dx; final protected double dy; protected Color color; protected Gitter() { this.dx = 0; this.dy = 0; color = Color.BLACK; } public Gitter(double dx, double dy, Color color) { this.dx = dx; this.dy = dy; this.color = new Color(color.getRGB()); } public Gitter(Gitter other) { this(other.dx, other.dy, other.color); } @Override public void draw(Graphics g) { Color oldColor = g.getColor(); g.setColor(Color.BLACK); for (int x = 0; x < 1100; x += dx) { g.drawLine(x, 0, x, 1100); g.drawLine(0, x, 1100, x); } g.setColor(oldColor); } public double getDx() { return dx; } public double getDy() { return dy; } public Color getColor() { return color; } public void setColor(Color color) { this.color = new Color(color.getRGB()); |
Leave a Reply