Skip to content

Instantly share code, notes, and snippets.

@JustAnotherJavaProgrammer
Last active January 6, 2023 21:13
Show Gist options
  • Save JustAnotherJavaProgrammer/daf36211f1f53fb4ce723becb94b361f to your computer and use it in GitHub Desktop.
Save JustAnotherJavaProgrammer/daf36211f1f53fb4ce723becb94b361f to your computer and use it in GitHub Desktop.
JBackgroundPanel (GPL)
/*
* 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 3 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/>.
*/
package gui;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class JBackgroundPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private Image background = null;
public static final int FULL_IMAGE = 0;
public static final int FILL_PANEL = 1;
public static final int DISTORT_IMAGE = 2;
private int backgroundType = FULL_IMAGE;
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
drawImage(background, g);
}
}
public void keyPressed(int keyCode) {
}
public void keyReleased(int keyCode) {
}
private void drawImage(Image image, Graphics g) {
double scale;
switch (backgroundType) {
case 0:
scale = (double) (this.getHeight()) / (double) (image.getHeight(null));
if (image.getWidth(null) * scale > getWidth()) {
scale = (double) (this.getWidth()) / (double) (image.getWidth(null));
}
g.drawImage(image, (int) ((getWidth() - (image.getWidth(null) * scale)) / 2),
(int) ((getHeight() - (image.getHeight(null) * scale)) / 2), (int) (image.getWidth(null) * scale),
(int) (image.getHeight(null) * scale), getBackground(), null);
break;
case 1:
scale = (double) (this.getHeight()) / (double) (image.getHeight(null));
if (image.getWidth(null) * scale < getWidth()) {
scale = (double) (this.getWidth()) / (double) (image.getWidth(null));
}
g.drawImage(image, (int) ((getWidth() - (image.getWidth(null) * scale)) / 2),
(int) ((getHeight() - (image.getHeight(null) * scale)) / 2), (int) (image.getWidth(null) * scale),
(int) (image.getHeight(null) * scale), getBackground(), null);
break;
default:
g.drawImage(image, 0, 0, getWidth(), getHeight(), getBackground(), null);
break;
}
}
public void setBackground(Image backgroundImage) {
background = backgroundImage;
EventQueue.invokeLater(new Runnable() {
public void run() {
repaint();
}
});
}
public Image getBackgroundImage() {
return background;
}
public void setBackgroundType(int backgroundType) {
if (this.backgroundType != backgroundType) {
this.backgroundType = backgroundType;
repaint();
}
}
// Mutates the supplied Point
public void mapToBackgroundMutate(Point position) {
if (background == null)
return;
double xscale;
double yscale;
int xOffset;
int yOffset;
switch (backgroundType) {
case 0:
xscale = yscale = (double) (this.getHeight()) / (double) (background.getHeight(null));
if (background.getWidth(null) * xscale > getWidth()) {
xscale = yscale = (double) (this.getWidth()) / (double) (background.getWidth(null));
}
xOffset = (int) ((getWidth() - (background.getWidth(null) * xscale)) / 2);
yOffset = (int) ((getHeight() - (background.getHeight(null) * yscale)) / 2);
break;
case 1:
xscale = yscale = (double) (this.getHeight()) / (double) (background.getHeight(null));
if (background.getWidth(null) * xscale < getWidth()) {
xscale = yscale = (double) (this.getWidth()) / (double) (background.getWidth(null));
}
xOffset = (int) ((getWidth() - (background.getWidth(null) * xscale)) / 2);
yOffset = (int) ((getHeight() - (background.getHeight(null) * yscale)) / 2);
break;
default:
xOffset = yOffset = 0;
xscale = (double) getWidth() / background.getWidth(null);
yscale = (double) getHeight() / background.getHeight(null);
break;
}
position.setLocation((int) ((position.getX() - xOffset) / xscale),
(int) ((position.getY() - yOffset) / yscale));
// System.out.println(
// "xscale: " + xscale + "\nyscale: " + yscale + "\nxOffset: " + xOffset + "\nyOffset: " + yOffset);
// System.out.println(position);
// System.out.println("position.x: " + position.x + "\nposition.y: " + position.y);
}
// Clones the supplied Point before passing it to mapToBackgroundMutate
public Point mapToBackground(Point position) {
Point result = new Point(position);
mapToBackgroundMutate(result);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment