diff --git a/src/main/java/org/openRealmOfStars/gui/panels/ImagePanel.java b/src/main/java/org/openRealmOfStars/gui/panels/ImagePanel.java new file mode 100644 index 000000000..43268d85b --- /dev/null +++ b/src/main/java/org/openRealmOfStars/gui/panels/ImagePanel.java @@ -0,0 +1,122 @@ +package org.openRealmOfStars.gui.panels; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.GradientPaint; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +import javax.swing.JPanel; + +import org.openRealmOfStars.gui.GuiStatics; +import org.openRealmOfStars.gui.borders.SimpleBorder; + +/** + * + * Open Realm of Stars game project + * Copyright (C) 2017 Tuomo Untinen + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see http://www.gnu.org/licenses/ + * + * + * Image panel of any generic image + * + */ + +public class ImagePanel extends JPanel { + + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + * picture to draw + */ + private BufferedImage image; + + /** + * Text to show in picture + */ + private String text; + + /** + * Create picture frame + * @param picture to show + */ + public ImagePanel(final BufferedImage picture) { + super(); + image = picture; + text = null; + Dimension size = new Dimension(image.getWidth(), image.getHeight()); + this.setMinimumSize(size); + this.setPreferredSize(size); + this.setBorder(new SimpleBorder()); + } + + /** + * Change image in frame + * @param picture to show + */ + public void setImage(final BufferedImage picture) { + image = picture; + } + + /** + * Text to show instead of picture. Set to null to show picture instead. + * @param textToShow In picture frame + */ + public void setText(final String textToShow) { + text = textToShow; + } + + /** + * Get Text to set to show. If null then image is shown. + * @return Text or null + */ + public String getText() { + return text; + } + /** + * Get Image from frame + * @return Image from frame + */ + public BufferedImage getImage() { + return image; + } + @Override + protected void paintComponent(final Graphics g) { + GradientPaint gradient = new GradientPaint(this.getWidth() / 2, 0, + Color.BLACK, this.getWidth() / 2, this.getHeight(), + GuiStatics.COLOR_GREY_40, true); + + Graphics2D g2d = (Graphics2D) g; + + g2d.setPaint(gradient); + g.fillRect(0, 0, this.getWidth(), this.getHeight()); + if (text != null) { + g2d.setFont(GuiStatics.getFontCubellan()); + g2d.setColor(GuiStatics.COLOR_COOL_SPACE_BLUE); + int textWidth = GuiStatics.getTextWidth(GuiStatics.getFontCubellan(), + text); + int offsetX = this.getWidth() / 2 - textWidth / 2; + g2d.drawString(text, offsetX, this.getHeight() / 2); + + } else { + g.drawImage(image, this.getWidth() / 2 - image.getWidth() / 2, + this.getHeight() - image.getHeight(), null); + } + } +} diff --git a/src/test/java/org/openRealmOfStars/gui/panels/ImagePanelTest.java b/src/test/java/org/openRealmOfStars/gui/panels/ImagePanelTest.java new file mode 100644 index 000000000..5bdf6e058 --- /dev/null +++ b/src/test/java/org/openRealmOfStars/gui/panels/ImagePanelTest.java @@ -0,0 +1,52 @@ +package org.openRealmOfStars.gui.panels; + +import static org.junit.Assert.*; + +import java.awt.image.BufferedImage; + +import org.junit.Test; +import org.mockito.Mockito; + +/** + * + * Open Realm of Stars game project + * Copyright (C) 2017 Tuomo Untinen + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see http://www.gnu.org/licenses/ + * + * + * Image Panel Test + * + */ +public class ImagePanelTest { + + @Test + public void testBasic() { + BufferedImage picture = Mockito.mock(BufferedImage.class); + Mockito.when(picture.getWidth()).thenReturn(100); + Mockito.when(picture.getHeight()).thenReturn(100); + BufferedImage picture2 = Mockito.mock(BufferedImage.class); + Mockito.when(picture2.getWidth()).thenReturn(100); + Mockito.when(picture2.getHeight()).thenReturn(100); + ImagePanel panel = new ImagePanel(picture); + assertEquals(picture, panel.getImage()); + assertEquals(null, panel.getText()); + panel.setText("Test"); + assertEquals("Test", panel.getText()); + panel.setImage(picture2); + assertEquals(picture2, panel.getImage()); + panel.repaint(); + } + +}