From 3268268dc2d24e0d07473c53a75a7a71b27ce653 Mon Sep 17 00:00:00 2001 From: ericnerdo Date: Sat, 7 May 2016 19:41:57 +0200 Subject: [PATCH] Drawers refactored. Imageloading is now in the Drawers instead of inheriting from ImagePanel. ImagePanel.java deleted. --- cicaprojekt/FieldDrawer.java | 21 +++++++++++++++++++-- cicaprojekt/GapDrawer.java | 16 ++++++++++++++-- cicaprojekt/GateDrawer.java | 21 +++++++++++++++++++-- cicaprojekt/ImagePanel.java | 29 ----------------------------- cicaprojekt/ScaleDrawer.java | 21 +++++++++++++++++++-- cicaprojekt/WallDrawer.java | 21 +++++++++++++++++++-- 6 files changed, 90 insertions(+), 39 deletions(-) delete mode 100644 cicaprojekt/ImagePanel.java diff --git a/cicaprojekt/FieldDrawer.java b/cicaprojekt/FieldDrawer.java index 7d86b68..c7d54b4 100644 --- a/cicaprojekt/FieldDrawer.java +++ b/cicaprojekt/FieldDrawer.java @@ -1,13 +1,19 @@ package cicaprojekt; +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.io.File; import java.io.IOException; -public class FieldDrawer extends ImagePanel implements Drawer { +import javax.imageio.ImageIO; +import javax.swing.JPanel; +public class FieldDrawer extends JPanel implements Drawer { + private BufferedImage image; Field field; public FieldDrawer(Field f) throws IOException { - super("Field.png"); + image = ImageIO.read(new File("Field.png")); field = f; setVisible(false); } @@ -33,5 +39,16 @@ public class FieldDrawer extends ImagePanel implements Drawer { public int getY() { return field.getY(); } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters + } + + public void changeImage(String path) throws IOException { + image = ImageIO.read(new File(path)); + repaint(); + } } diff --git a/cicaprojekt/GapDrawer.java b/cicaprojekt/GapDrawer.java index b5248a5..486a628 100644 --- a/cicaprojekt/GapDrawer.java +++ b/cicaprojekt/GapDrawer.java @@ -1,13 +1,19 @@ package cicaprojekt; +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.io.File; import java.io.IOException; -public class GapDrawer extends ImagePanel implements Drawer{ +import javax.imageio.ImageIO; +import javax.swing.JPanel; +public class GapDrawer extends JPanel implements Drawer{ + private BufferedImage image; private Gap gap; public GapDrawer(Gap g) throws IOException { - super("Gap.png"); + image = ImageIO.read(new File("Gap.png")); gap = g; setVisible(false); } @@ -27,4 +33,10 @@ public class GapDrawer extends ImagePanel implements Drawer{ return gap.getY(); } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters + } } diff --git a/cicaprojekt/GateDrawer.java b/cicaprojekt/GateDrawer.java index a841e26..f01a171 100644 --- a/cicaprojekt/GateDrawer.java +++ b/cicaprojekt/GateDrawer.java @@ -1,13 +1,19 @@ package cicaprojekt; +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.io.File; import java.io.IOException; -public class GateDrawer extends ImagePanel implements Drawer{ +import javax.imageio.ImageIO; +import javax.swing.JPanel; +public class GateDrawer extends JPanel implements Drawer{ + private BufferedImage image; Gate gate; public GateDrawer(Gate g) throws IOException { - super("ClosedGate.png"); + image = ImageIO.read(new File("ClosedGate.png")); gate = g; setVisible(false); } @@ -32,4 +38,15 @@ public class GateDrawer extends ImagePanel implements Drawer{ return gate.getY(); } + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters + } + + public void changeImage(String path) throws IOException { + image = ImageIO.read(new File(path)); + repaint(); + } + } diff --git a/cicaprojekt/ImagePanel.java b/cicaprojekt/ImagePanel.java deleted file mode 100644 index 243a779..0000000 --- a/cicaprojekt/ImagePanel.java +++ /dev/null @@ -1,29 +0,0 @@ -package cicaprojekt; - -import java.awt.Graphics; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; - -import javax.imageio.ImageIO; -import javax.swing.JPanel; - -public class ImagePanel extends JPanel{ - - private BufferedImage image; - - public ImagePanel(String path) throws IOException { - image = ImageIO.read(new File(path)); - } - - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters - } - - public void changeImage(String path) throws IOException { - image = ImageIO.read(new File(path)); - repaint(); - } -} \ No newline at end of file diff --git a/cicaprojekt/ScaleDrawer.java b/cicaprojekt/ScaleDrawer.java index d0d5244..ac18510 100644 --- a/cicaprojekt/ScaleDrawer.java +++ b/cicaprojekt/ScaleDrawer.java @@ -1,13 +1,19 @@ package cicaprojekt; +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.io.File; import java.io.IOException; -public class ScaleDrawer extends ImagePanel implements Drawer { +import javax.imageio.ImageIO; +import javax.swing.JPanel; +public class ScaleDrawer extends JPanel implements Drawer { + private BufferedImage image; Scale scale; public ScaleDrawer(Scale s) throws IOException { - super("Scale.png"); + image = ImageIO.read(new File("Scale.png")); scale = s; setVisible(false); } @@ -32,4 +38,15 @@ public class ScaleDrawer extends ImagePanel implements Drawer { return scale.getY(); } + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters + } + + public void changeImage(String path) throws IOException { + image = ImageIO.read(new File(path)); + repaint(); + } + } diff --git a/cicaprojekt/WallDrawer.java b/cicaprojekt/WallDrawer.java index 4e8ad7a..9864ed7 100644 --- a/cicaprojekt/WallDrawer.java +++ b/cicaprojekt/WallDrawer.java @@ -1,13 +1,19 @@ package cicaprojekt; +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.io.File; import java.io.IOException; -public class WallDrawer extends ImagePanel implements Drawer { +import javax.imageio.ImageIO; +import javax.swing.JPanel; +public class WallDrawer extends JPanel implements Drawer { + private BufferedImage image; Wall wall; public WallDrawer(Wall w) throws IOException { - super("Wall.png"); + image = ImageIO.read(new File("Wall.png")); wall = w; setVisible(false); } @@ -45,5 +51,16 @@ public class WallDrawer extends ImagePanel implements Drawer { public int getY() { return wall.getY(); } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters + } + public void changeImage(String path) throws IOException { + image = ImageIO.read(new File(path)); + repaint(); + } + }