Skip to content

Commit

Permalink
#56 Adds image panel which is going to be needed in NewsCorp view.
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomount committed Jul 20, 2017
1 parent 7cd5d24 commit 84d0f8e
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
122 changes: 122 additions & 0 deletions src/main/java/org/openRealmOfStars/gui/panels/ImagePanel.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
52 changes: 52 additions & 0 deletions src/test/java/org/openRealmOfStars/gui/panels/ImagePanelTest.java
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 84d0f8e

Please sign in to comment.