Skip to content

Commit

Permalink
New methods to take and save screenshots!
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbuzatto committed Feb 15, 2025
1 parent c07ea17 commit a0b586f
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 4 deletions.
2 changes: 0 additions & 2 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
Implementar "print screen" de tela inteira e de recorte;

Realizar testes com joysticks do playstation.
2 changes: 1 addition & 1 deletion src/br/com/davidbuzatto/jsge/Bundle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
JSGE.version=v1.5.5
JSGE.version=v1.5.6
204 changes: 204 additions & 0 deletions src/br/com/davidbuzatto/jsge/core/engine/EngineFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Robot;
Expand Down Expand Up @@ -83,7 +84,9 @@
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderableImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
Expand All @@ -98,6 +101,8 @@
import java.util.Map;
import java.util.Set;
import java.util.logging.LogManager;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
Expand Down Expand Up @@ -4180,6 +4185,205 @@ public void g2DrawString( String string, float x, float y, Paint paint ) {



//**************************************************************************
// Métodos para captura de tela.
//**************************************************************************

/**
* Tira a captura de tela da engine corrente, respeitando um limite, retornando uma imagem.
*
* @param x Coordenada x do vértice superior esquerdo do retângulo de captura.
* @param y Coordenada y do vértice superior esquerdo do retângulo de captura.
* @param width Largura do retângulo de captura.
* @param height Altura do retângulo de captura.
* @return Uma imagem com a captura.
*/
public Image takeScreenshot( double x, double y, double width, double height ) {

try {

java.awt.Rectangle fBounds = getBounds();
Insets insets = getInsets();

int xStart = fBounds.x + insets.left + (int) x;
int yStart = fBounds.y + insets.top + (int) y;

Robot rb = new Robot();
Image image = new Image( rb.createScreenCapture( new java.awt.Rectangle( xStart, yStart, (int) width, (int) height ) ) );

return image;

} catch ( AWTException exc ) {
traceLogError( CoreUtils.stackTraceToString( exc ) );
}

return ImageUtils.createTextImage( "error", 20, Font.BOLD, EngineFrame.WHITE, EngineFrame.BLACK );

}

/**
* Tira a captura de tela da engine corrente, respeitando um limite, retornando uma imagem.
*
* @param source O retângulo de captura.
* @return Uma imagem com a captura.
*/
public Image takeScreenshot( Rectangle source ) {
return takeScreenshot( source.x, source.y, source.width, source.height );
}

/**
* Tira a captura de toda a tela da engine corrente, retornando uma imagem.
*
* @return Uma imagem com a captura.
*/
public Image takeScreenshot() {
Dimension dpSize = drawingPanel.getSize();
return takeScreenshot( 0, 0, dpSize.width, dpSize.height );
}

/**
* Salva uma captura de tela da engine corrente, respeitando um limite.
*
* @param formatName Nome do formato (bmp, BMP, gif, GIF, jpg, JPG, jpeg, JPEG, png, PNG, tif, TIF, tiff, TIFF, wbmp, WBMP).
* @param outputFile Arquivo de saída.
* @param x Coordenada x do vértice superior esquerdo do retângulo de captura.
* @param y Coordenada y do vértice superior esquerdo do retângulo de captura.
* @param width Largura do retângulo de captura.
* @param height Altura do retângulo de captura.
*/
public void saveScreenshot( String formatName, File outputFile, double x, double y, double width, double height ) {
try {
ImageIO.write( takeScreenshot( x, y, width, height ).buffImage, formatName, outputFile );
} catch ( IOException exc ) {
traceLogError( CoreUtils.stackTraceToString( exc ) );
}
}

/**
* Salva uma captura de tela da engine corrente, respeitando um limite.
*
* @param formatName Nome do formato (bmp, BMP, gif, GIF, jpg, JPG, jpeg, JPEG, png, PNG, tif, TIF, tiff, TIFF, wbmp, WBMP).
* @param outputFile Arquivo de saída.
* @param source O retângulo de captura.
*/
public void saveScreenshot( String formatName, File outputFile, Rectangle source ) {
try {
ImageIO.write( takeScreenshot( source ).buffImage, formatName, outputFile );
} catch ( IOException exc ) {
traceLogError( CoreUtils.stackTraceToString( exc ) );
}
}

/**
* Salva uma captura da tela toda da engine corrente.
*
* @param formatName Nome do formato (bmp, BMP, gif, GIF, jpg, JPG, jpeg, JPEG, png, PNG, tif, TIF, tiff, TIFF, wbmp, WBMP).
* @param outputFile Arquivo de saída.
*/
public void saveScreenshot( String formatName, File outputFile ) {
try {
ImageIO.write( takeScreenshot().buffImage, formatName, outputFile );
} catch ( IOException exc ) {
traceLogError( CoreUtils.stackTraceToString( exc ) );
}
}

/**
* Salva uma captura de tela da engine corrente, respeitando um limite.
*
* @param formatName Nome do formato (bmp, BMP, gif, GIF, jpg, JPG, jpeg, JPEG, png, PNG, tif, TIF, tiff, TIFF, wbmp, WBMP).
* @param imageOutputStream Stream de saída de imagem.
* @param x Coordenada x do vértice superior esquerdo do retângulo de captura.
* @param y Coordenada y do vértice superior esquerdo do retângulo de captura.
* @param width Largura do retângulo de captura.
* @param height Altura do retângulo de captura.
*/
public void saveScreenshot( String formatName, ImageOutputStream imageOutputStream, double x, double y, double width, double height ) {
try {
ImageIO.write( takeScreenshot( x, y, width, height ).buffImage, formatName, imageOutputStream );
} catch ( IOException exc ) {
traceLogError( CoreUtils.stackTraceToString( exc ) );
}
}

/**
* Salva uma captura de tela da engine corrente, respeitando um limite.
*
* @param formatName Nome do formato (bmp, BMP, gif, GIF, jpg, JPG, jpeg, JPEG, png, PNG, tif, TIF, tiff, TIFF, wbmp, WBMP).
* @param imageOutputStream Stream de saída de imagem.
* @param source O retângulo de captura.
*/
public void saveScreenshot( String formatName, ImageOutputStream imageOutputStream, Rectangle source ) {
try {
ImageIO.write( takeScreenshot( source ).buffImage, formatName, imageOutputStream );
} catch ( IOException exc ) {
traceLogError( CoreUtils.stackTraceToString( exc ) );
}
}

/**
* Salva uma captura da tela toda da engine corrente.
*
* @param formatName Nome do formato (bmp, BMP, gif, GIF, jpg, JPG, jpeg, JPEG, png, PNG, tif, TIF, tiff, TIFF, wbmp, WBMP).
* @param imageOutputStream Stream de saída de imagem.
*/
public void saveScreenshot( String formatName, ImageOutputStream imageOutputStream ) {
try {
ImageIO.write( takeScreenshot().buffImage, formatName, imageOutputStream );
} catch ( IOException exc ) {
traceLogError( CoreUtils.stackTraceToString( exc ) );
}
}

/**
* Salva uma captura de tela da engine corrente, respeitando um limite.
*
* @param formatName Nome do formato (bmp, BMP, gif, GIF, jpg, JPG, jpeg, JPEG, png, PNG, tif, TIF, tiff, TIFF, wbmp, WBMP).
* @param outputStream Stream de saída.
* @param x Coordenada x do vértice superior esquerdo do retângulo de captura.
* @param y Coordenada y do vértice superior esquerdo do retângulo de captura.
* @param width Largura do retângulo de captura.
* @param height Altura do retângulo de captura.
*/
public void saveScreenshot( String formatName, OutputStream outputStream, double x, double y, double width, double height ) {
try {
ImageIO.write( takeScreenshot( x, y, width, height ).buffImage, formatName, outputStream );
} catch ( IOException exc ) {
traceLogError( CoreUtils.stackTraceToString( exc ) );
}
}

/**
* Salva uma captura de tela da engine corrente, respeitando um limite.
*
* @param formatName Nome do formato (bmp, BMP, gif, GIF, jpg, JPG, jpeg, JPEG, png, PNG, tif, TIF, tiff, TIFF, wbmp, WBMP).
* @param outputStream Stream de saída.
* @param source O retângulo de captura.
*/
public void saveScreenshot( String formatName, OutputStream outputStream, Rectangle source ) {
try {
ImageIO.write( takeScreenshot( source ).buffImage, formatName, outputStream );
} catch ( IOException exc ) {
traceLogError( CoreUtils.stackTraceToString( exc ) );
}
}

/**
* Salva uma captura da tela toda da engine corrente.
*
* @param formatName Nome do formato (bmp, BMP, gif, GIF, jpg, JPG, jpeg, JPEG, png, PNG, tif, TIF, tiff, TIFF, wbmp, WBMP).
* @param outputStream Stream de saída.
*/
public void saveScreenshot( String formatName, OutputStream outputStream ) {
try {
ImageIO.write( takeScreenshot().buffImage, formatName, outputStream );
} catch ( IOException exc ) {
traceLogError( CoreUtils.stackTraceToString( exc ) );
}
}



//**************************************************************************
// Métodos para carga de fontes.
//**************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* 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 br.com.davidbuzatto.jsge.core.engine;
package br.com.davidbuzatto.jsge.core.engine.legacy;

import br.com.davidbuzatto.jsge.collision.aabb.AABB;
import br.com.davidbuzatto.jsge.core.Camera2D;
Expand Down

0 comments on commit a0b586f

Please sign in to comment.