Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the abillity to export the rendered image to a file for being used as a sprite #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions Panel.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Panel extends JPanel {

int scale = 10; // Factor by which to scale books
int w = 4; // Number of columns of books
int h = 3; // Number of rows of books
int margin = 5; // Number of pixels between each book (will be scaled)

int scale = 10; // Factor by which to scale books
int w = 12; // Number of columns of books
int h = 12; // Number of rows of books
int margin = 5; // Number of pixels between each book (will be scaled)

BufferedImage bufferImage;
Graphics2D bufferGraphics;

public Panel() {
setPreferredSize(new Dimension((Buch.width+margin)*scale*w, (Buch.height+margin)*scale*h));
final int frameWidth = (Buch.width + margin) * scale * w;
final int frameHeight = (Buch.height + margin) * scale * h;
setPreferredSize(new Dimension(frameWidth, frameHeight));
bufferImage = new BufferedImage(frameWidth, frameHeight, BufferedImage.TYPE_INT_ARGB);
bufferGraphics = bufferImage.createGraphics();
}

//Draw all the books
@Override
public void paintComponent(Graphics g) {
g.setColor(new Color(108, 83, 83));
g.fillRect(0, 0, getWidth(), getHeight());
for (int i = 0; i<h; i++) {
for (int j = 0; j<w; j++) {
buchZeichnen(margin*scale/2+((Buch.width+margin)*scale)*j, margin*scale/2+((Buch.height+margin)*scale)*i, g);
Graphics2D g2 = (Graphics2D)g;
g2.fillRect(0, 0, getWidth(), getHeight());

for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
buchZeichnen(margin * scale / 2 + ((Buch.width + margin) * scale) * j, margin * scale / 2 + ((Buch.height + margin) * scale) * i);
}
}
exportToFile(g2);
}

private void exportToFile(Graphics2D g2){
g2.drawImage(bufferImage, null, 0, 0);
try {
ImageIO.write(bufferImage, "png", new File("frameImage.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
//Draw a book
public void buchZeichnen(int x, int y, Graphics g) {
public void buchZeichnen(int x, int y) {
Buch buch = new Buch();
g.drawImage(buch.getImage(), x, y, x+Buch.width*scale, y+Buch.height*scale, 0, 0, Buch.width, Buch.height, null);
bufferGraphics.drawImage(buch.getImage(), x, y, x + Buch.width * scale, y + Buch.height * scale, 0, 0, Buch.width, Buch.height, null);

}

}