Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
Now the mathematical and geometrical classes are serializable;
Method to activate and deactivate  antialiasing;
Implementation of Chaikin and Catmull-Rom algorithms for curve smoothing.
Implementation of colorInvert and colorGrayscale methods.
  • Loading branch information
davidbuzatto committed Nov 29, 2024
1 parent e992a54 commit 05bc090
Show file tree
Hide file tree
Showing 29 changed files with 514 additions and 36 deletions.
12 changes: 0 additions & 12 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
Implementar inversão de cor;
Complementar exemplo das cores mostrando as transformações do lado esquerdo;
Tornar as classes matemáticas e de formas serializáveis;

Antialiasing em textos:
g2d.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON );

Suavização da linhas:
https://www.codeproject.com/Articles/1093960/2D-Polyline-Vertex-Smoothing

Separar o exemplos?
Separar a engine do componente?
Traduzir documentação para inglês?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2771,6 +2771,24 @@ public Graphics2D getGraphics2D() {
return g2d;
}

/**
* Retorna se a suavização do contexto gráfico está ativa.
*
* @return Verdadeiro caso a suavização esteja ativada, falso caso contrário.
*/
public boolean isAntialiasing() {
return antialiasing;
}

/**
* Altera a flag de controle da suavização do contexto gráfico.
*
* @param antialiasing O estado da flag.
*/
public void setAntialiasing( boolean antialiasing ) {
this.antialiasing = antialiasing;
}

/**
* Rotaciona o contexto gráfico atual a partir da coordenada (0, 0).
* Observação: Utilize apenas no método draw!
Expand Down
18 changes: 18 additions & 0 deletions src/br/com/davidbuzatto/jsge/core/engine/EngineFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2742,6 +2742,24 @@ public Graphics2D getGraphics2D() {
return g2d;
}

/**
* Retorna se a suavização do contexto gráfico está ativa.
*
* @return Verdadeiro caso a suavização esteja ativada, falso caso contrário.
*/
public boolean isAntialiasing() {
return antialiasing;
}

/**
* Altera a flag de controle da suavização do contexto gráfico.
*
* @param antialiasing O estado da flag.
*/
public void setAntialiasing( boolean antialiasing ) {
this.antialiasing = antialiasing;
}

/**
* Rotaciona o contexto gráfico atual a partir da coordenada (0, 0).
* Observação: Utilize apenas no método draw!
Expand Down
26 changes: 26 additions & 0 deletions src/br/com/davidbuzatto/jsge/core/utils/ColorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ public static Color colorTint( Color color, Color tint ) {
color.getAlpha() * tint.getAlpha() / 255
);
}

/**
* Gera a cor invertida da cor passada.
*
* @param color A cor.
* @return A cor invertida.
*/
public static Color colorInvert( Color color ) {
return new Color(
255 - color.getRed(),
255 - color.getGreen(),
255 - color.getBlue(),
color.getAlpha()
);
}

/**
* Gera a cor em escala de cinza da cor passada.
*
* @param color A cor.
* @return A cor em escala de cinza.
*/
public static Color colorGrayscale( Color color ) {
int gray = ( color.getRed() + color.getGreen() + color.getBlue() ) / 3;
return new Color( gray, gray, gray, color.getAlpha() );
}

/**
* Obtém uma cor com correção em relação ao brilho. O fator de brilho vai de
Expand Down
11 changes: 11 additions & 0 deletions src/br/com/davidbuzatto/jsge/examples/JSGEShowcaseWindow.form
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Component id="btnPaintingCapabilities" alignment="0" max="32767" attributes="0"/>
<Component id="btnAnimations" alignment="0" max="32767" attributes="0"/>
<Component id="btnGamepads" alignment="0" max="32767" attributes="0"/>
<Component id="btnCurveSmoothing" alignment="0" max="32767" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
Expand All @@ -60,6 +61,8 @@
<EmptySpace max="-2" attributes="0"/>
<Component id="btnCollisionDetection" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="btnCurveSmoothing" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="btnImageLoadingAndProcessing" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="btnUserInteraction" min="-2" max="-2" attributes="0"/>
Expand Down Expand Up @@ -124,6 +127,14 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnCollisionDetectionActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="btnCurveSmoothing">
<Properties>
<Property name="text" type="java.lang.String" value="Curve Smoothing"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnCurveSmoothingActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="btnImageLoadingAndProcessing">
<Properties>
<Property name="text" type="java.lang.String" value="Image Loading and Processing"/>
Expand Down
19 changes: 18 additions & 1 deletion src/br/com/davidbuzatto/jsge/examples/JSGEShowcaseWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import br.com.davidbuzatto.jsge.examples.basic.UserInteractionExample;
import br.com.davidbuzatto.jsge.examples.ball.BouncingBallExample;
import br.com.davidbuzatto.jsge.examples.animation.AnimationsExample;
import br.com.davidbuzatto.jsge.examples.basic.CurveSmoothingExample;
import br.com.davidbuzatto.jsge.examples.basic.GamepadsExample;
import br.com.davidbuzatto.jsge.examples.camera.CameraExample;
import br.com.davidbuzatto.jsge.examples.particles.ParticlesExample;
Expand Down Expand Up @@ -62,6 +63,7 @@ private void initComponents() {
btnDrawingPrimitiveObjects = new javax.swing.JButton();
btnPaintingCapabilities = new javax.swing.JButton();
btnCollisionDetection = new javax.swing.JButton();
btnCurveSmoothing = new javax.swing.JButton();
btnImageLoadingAndProcessing = new javax.swing.JButton();
btnUserInteraction = new javax.swing.JButton();
btnColorMethods = new javax.swing.JButton();
Expand Down Expand Up @@ -107,6 +109,13 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});

btnCurveSmoothing.setText("Curve Smoothing");
btnCurveSmoothing.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCurveSmoothingActionPerformed(evt);
}
});

btnImageLoadingAndProcessing.setText("Image Loading and Processing");
btnImageLoadingAndProcessing.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Expand Down Expand Up @@ -190,7 +199,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addComponent(btnSoundAndMusic, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnPaintingCapabilities, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnAnimations, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnGamepads, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(btnGamepads, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnCurveSmoothing, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
Expand All @@ -207,6 +217,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnCollisionDetection)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnCurveSmoothing)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnImageLoadingAndProcessing)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnUserInteraction)
Expand Down Expand Up @@ -283,6 +295,10 @@ private void btnGamepadsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-F
new GamepadsExample().setDefaultCloseOperation( DISPOSE_ON_CLOSE );
}//GEN-LAST:event_btnGamepadsActionPerformed

private void btnCurveSmoothingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCurveSmoothingActionPerformed
new CurveSmoothingExample().setDefaultCloseOperation( DISPOSE_ON_CLOSE );
}//GEN-LAST:event_btnCurveSmoothingActionPerformed

/**
* Executa o Showcase.
*
Expand Down Expand Up @@ -327,6 +343,7 @@ public void run() {
private javax.swing.JButton btnCamera;
private javax.swing.JButton btnCollisionDetection;
private javax.swing.JButton btnColorMethods;
private javax.swing.JButton btnCurveSmoothing;
private javax.swing.JButton btnDrawingPrimitiveMethods;
private javax.swing.JButton btnDrawingPrimitiveObjects;
private javax.swing.JButton btnGamepads;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public ColorMethodsExample() {
@Override
public void create() {
baseColor = LIME;
setDefaultFontSize( 20 );
}

@Override
Expand Down Expand Up @@ -80,13 +81,22 @@ public void draw() {
}

fillRectangle( 10, 30, 50, 50, baseColor );
drawText( "base", 70, 50, BLACK );
fillRectangle( 10, 80, 50, 50, ColorUtils.colorAlpha( baseColor, 0.5 ) );
drawText( "50% alpha", 70, 100, BLACK );
fillRectangle( 10, 130, 50, 50, ColorUtils.colorTint( baseColor, WHITE ) );
fillRectangle( 10, 180, 50, 50, ColorUtils.colorBrightness( baseColor, -0.5 ) );
fillRectangle( 10, 230, 50, 50, ColorUtils.colorContrast( baseColor, -0.5 ) );
drawText( "white tint", 70, 150, BLACK );
fillRectangle( 10, 180, 50, 50, ColorUtils.colorInvert( baseColor ) );
drawText( "inverted", 70, 200, BLACK );
fillRectangle( 10, 230, 50, 50, ColorUtils.colorGrayscale( baseColor ) );
drawText( "grayscale", 70, 250, BLACK );
fillRectangle( 10, 280, 50, 50, ColorUtils.colorBrightness( baseColor, -0.5 ) );
drawText( "-0.5 brightness", 70, 300, BLACK );
fillRectangle( 10, 330, 50, 50, ColorUtils.colorContrast( baseColor, -0.5 ) );
drawText( "-0.5 contrast", 70, 350, BLACK );

String message = "right click me ;)";
drawText( message, 10, getScreenHeight() - 30, 20, BLACK );
drawText( message, 10, getScreenHeight() - 30, BLACK );

drawHSVCircle();

Expand Down
152 changes: 152 additions & 0 deletions src/br/com/davidbuzatto/jsge/examples/basic/CurveSmoothingExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright (C) 2024 Prof. Dr. David Buzatto
*
* 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 br.com.davidbuzatto.jsge.examples.basic;

import br.com.davidbuzatto.jsge.core.engine.EngineFrame;
import br.com.davidbuzatto.jsge.math.CurveUtils;
import br.com.davidbuzatto.jsge.math.Vector2;
import java.util.ArrayList;
import java.util.List;

/**
* Exemplo de uso dos métodos de suavização de curvas.
*
* @author Prof. Dr. David Buzatto
*/
public class CurveSmoothingExample extends EngineFrame {

private List<Vector2> points;
private List<Vector2> smoothChaikin;
private List<Vector2> smoothCatmullRom;
private boolean pointsChanged;

private double tension;
private int iterations;

/**
* Cria o exemplo.
*/
public CurveSmoothingExample() {
super( 600, 600, "Curve Smoothing", 60, true );
}

@Override
public void create() {
points = new ArrayList<>();
smoothChaikin = new ArrayList<>();
smoothCatmullRom = new ArrayList<>();
tension = 0;
iterations = 1;
setDefaultFontSize( 20 );
setDefaultStrokeLineWidth( 2 );
}

@Override
public void update( double delta ) {

if ( isKeyPressed( KEY_UP ) ) {
tension += 0.05;
pointsChanged = true;
} else if ( isKeyPressed( KEY_DOWN ) ) {
tension -= 0.05;
pointsChanged = true;
}

if ( tension < 0 ) {
tension = 0;
pointsChanged = false;
} else if ( tension > 1 ) {
tension = 1;
pointsChanged = false;
}

if ( isKeyPressed( KEY_LEFT ) ) {
iterations--;
pointsChanged = true;
} else if ( isKeyPressed( KEY_RIGHT ) ) {
iterations++;
pointsChanged = true;
}

if ( iterations < 1 ) {
iterations = 1;
pointsChanged = false;
} else if ( iterations > 10 ) {
iterations = 10;
pointsChanged = false;
}

if ( isMouseButtonPressed( MOUSE_BUTTON_LEFT ) ) {
points.add( getMousePositionPoint() );
pointsChanged = true;
}

if ( pointsChanged ) {
smoothChaikin = CurveUtils.getCurveSmoothingChaikin( points, tension, iterations );
smoothCatmullRom = CurveUtils.getSplineInterpolationCatmullRom( points, iterations );
pointsChanged = false;
}

}

@Override
public void draw() {

clearBackground( WHITE );

setStrokeLineWidth( 1 );
for ( Vector2 point : points ) {
drawCircle( point, 5, RED );
}

setStrokeDashArray( new float[]{ 4, 4 } );
for ( int i = 0; i < points.size() - 1; i++ ) {
drawLine( points.get( i ), points.get( i + 1 ), BLACK );
}

setStrokeLineWidth( 4 );
setStrokeDashArray( null );
for ( int i = 0; i < smoothChaikin.size() - 1; i++ ) {
drawLine( smoothChaikin.get( i ), smoothChaikin.get( i + 1 ), BLUE );
}

for ( int i = 0; i < smoothCatmullRom.size() - 1; i++ ) {
drawLine( smoothCatmullRom.get( i ), smoothCatmullRom.get( i + 1 ), ORANGE );
}

drawFPS( 10, 10 );


drawText( "Draw a curve by clicking with the mouse!", 115, 10, BLACK );
drawText( "Chaikin", getScreenWidth() - measureText( "Chaikin" ) - 10, getScreenHeight() - 50, BLUE );
drawText( "Catmull-Rom", getScreenWidth() - measureText( "Catmull-Rom" ) - 10, getScreenHeight() - 30, ORANGE );

drawText( String.format( "tension: %.2f (UP/DOWN)", tension ), 10, getScreenHeight() - 50, BLACK );
drawText( String.format( "iterations: %d (LEFT/RIGHT)", iterations ), 10, getScreenHeight() - 30, BLACK );


}

/**
* Executa o exemplo.
* @param args Argumentos.
*/
public static void main( String[] args ) {
new CurveSmoothingExample();
}

}
Loading

0 comments on commit 05bc090

Please sign in to comment.