org.pepsoft.worldpainter.ConfigureOverlayDialog Maven / Gradle / Ivy
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JDialog.java to edit this template
*/
package org.pepsoft.worldpainter;
import java.awt.*;
import java.awt.image.BufferedImage;
import static org.pepsoft.worldpainter.Constants.TILE_SIZE_BITS;
import static org.pepsoft.worldpainter.util.ImageUtils.loadImage;
/**
*
* @author pepijn
*/
@SuppressWarnings({"FieldCanBeLocal", "unused"}) // Managed by NetBeans
public class ConfigureOverlayDialog extends WorldPainterDialog {
/**
* Creates new form ConfigureOverlayDialog
*/
public ConfigureOverlayDialog(Window parent, Overlay overlay, Dimension dimension) {
super(parent);
this.overlay = overlay;
this.dimension = dimension;
originalEnabled = overlay.isEnabled();
originalScale = overlay.getScale();
originalTransparency = overlay.getTransparency();
originalOffsetX = overlay.getOffsetX();
originalOffsetY = overlay.getOffsetY();
initComponents();
labelFile.setText(overlay.getFile().getAbsolutePath());
checkBoxImageOverlay.setSelected(overlay.isEnabled());
spinnerScale.setValue(overlay.getScale() * 100);
spinnerTransparency.setValue(Math.round(overlay.getTransparency() * 100));
spinnerXOffset.setValue(overlay.getOffsetX());
spinnerYOffset.setValue(overlay.getOffsetY());
setControlStates();
getRootPane().setDefaultButton(buttonOK);
scaleToUI();
pack();
setLocationRelativeTo(parent);
programmaticChange = false;
}
@Override
protected void cancel() {
overlay.setScale(originalScale);
overlay.setTransparency(originalTransparency);
overlay.setOffsetX(originalOffsetX);
overlay.setOffsetY(originalOffsetY);
overlay.setEnabled(originalEnabled);
super.cancel();
}
private void setControlStates() {
boolean imageOverlayEnabled = checkBoxImageOverlay.isSelected();
spinnerScale.setEnabled(imageOverlayEnabled);
spinnerTransparency.setEnabled(imageOverlayEnabled);
spinnerXOffset.setEnabled(imageOverlayEnabled);
spinnerYOffset.setEnabled(imageOverlayEnabled);
buttonFitToDimension.setEnabled(imageOverlayEnabled);
}
private void fitOverlayToDimension() {
final BufferedImage overlayImage = loadImage(this, overlay.getFile());
if (overlayImage == null) {
return;
}
final float desiredScale;
final float dimRatio = (float) dimension.getWidth() / dimension.getHeight();
final float imgRatio = (float) overlayImage.getWidth() / overlayImage.getHeight();
if (dimRatio > imgRatio) {
// Dimension is wider than image, so make image the height of the
// dimension
desiredScale = (float) (dimension.getHeight() << TILE_SIZE_BITS) / overlayImage.getHeight();
} else {
// Dimension is taller than image, so make image the width of the
// dimension
desiredScale = (float) (dimension.getWidth() << TILE_SIZE_BITS) / overlayImage.getWidth();
}
final int scaledWidth = Math.round(overlayImage.getWidth() * desiredScale);
final int scaledHeight = Math.round(overlayImage.getHeight() * desiredScale);
final int xOffset = (dimension.getLowestX() << TILE_SIZE_BITS) + ((dimension.getWidth() << TILE_SIZE_BITS) - scaledWidth) / 2;
final int yOffset = (dimension.getLowestY() << TILE_SIZE_BITS) + ((dimension.getHeight() << TILE_SIZE_BITS) - scaledHeight) / 2;
programmaticChange = true;
try {
overlay.setOffsetX(xOffset);
overlay.setOffsetY(yOffset);
overlay.setScale(desiredScale);
spinnerScale.setValue(desiredScale * 100);
spinnerXOffset.setValue(xOffset);
spinnerYOffset.setValue(yOffset);
} finally {
programmaticChange = false;
}
}
/**
* This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
* content of this method is always regenerated by the Form Editor.
*/
// //GEN-BEGIN:initComponents
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
spinnerScale = new javax.swing.JSpinner();
jLabel4 = new javax.swing.JLabel();
buttonFitToDimension = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
spinnerTransparency = new javax.swing.JSpinner();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
spinnerXOffset = new javax.swing.JSpinner();
jLabel8 = new javax.swing.JLabel();
spinnerYOffset = new javax.swing.JSpinner();
buttonCancel = new javax.swing.JButton();
buttonOK = new javax.swing.JButton();
checkBoxImageOverlay = new javax.swing.JCheckBox();
jLabel1 = new javax.swing.JLabel();
labelFile = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Configure Overlay");
jLabel2.setText("File:");
jLabel3.setText("Scale:");
spinnerScale.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(100.0f), Float.valueOf(0.01f), Float.valueOf(9999.99f), Float.valueOf(0.1f)));
spinnerScale.setEditor(new javax.swing.JSpinner.NumberEditor(spinnerScale, "0.00"));
spinnerScale.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
spinnerScaleStateChanged(evt);
}
});
jLabel4.setText("%");
buttonFitToDimension.setText("Fit to dimension");
buttonFitToDimension.setToolTipText("Sets the scale and offset such that the image exactly covers the current dimension (as far as possible).");
buttonFitToDimension.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonFitToDimensionActionPerformed(evt);
}
});
jLabel5.setText("Transparency:");
spinnerTransparency.setModel(new javax.swing.SpinnerNumberModel(50, 0, 99, 1));
spinnerTransparency.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
spinnerTransparencyStateChanged(evt);
}
});
jLabel6.setText("%");
jLabel7.setText("X offset:");
spinnerXOffset.setModel(new javax.swing.SpinnerNumberModel(0, -999999, 999999, 1));
spinnerXOffset.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
spinnerXOffsetStateChanged(evt);
}
});
jLabel8.setText(", Y offset:");
spinnerYOffset.setModel(new javax.swing.SpinnerNumberModel(0, -999999, 999999, 1));
spinnerYOffset.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
spinnerYOffsetStateChanged(evt);
}
});
buttonCancel.setText("Cancel");
buttonCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonCancelActionPerformed(evt);
}
});
buttonOK.setText("OK");
buttonOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonOKActionPerformed(evt);
}
});
checkBoxImageOverlay.setText(" ");
checkBoxImageOverlay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
checkBoxImageOverlayActionPerformed(evt);
}
});
jLabel1.setLabelFor(checkBoxImageOverlay);
jLabel1.setText("Enabled:");
labelFile.setText("jLabel9");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(buttonOK)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonCancel))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(spinnerScale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttonFitToDimension))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(spinnerTransparency, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(jLabel6))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(spinnerXOffset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(spinnerYOffset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(checkBoxImageOverlay))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelFile)))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(labelFile))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(checkBoxImageOverlay)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(spinnerScale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(buttonFitToDimension))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(spinnerTransparency, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(spinnerXOffset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8)
.addComponent(spinnerYOffset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(buttonCancel)
.addComponent(buttonOK))
.addContainerGap())
);
pack();
}// //GEN-END:initComponents
private void spinnerScaleStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerScaleStateChanged
if (! programmaticChange) {
overlay.setScale(((float) spinnerScale.getValue()) / 100);
}
}//GEN-LAST:event_spinnerScaleStateChanged
private void buttonFitToDimensionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonFitToDimensionActionPerformed
fitOverlayToDimension();
}//GEN-LAST:event_buttonFitToDimensionActionPerformed
private void spinnerTransparencyStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerTransparencyStateChanged
if (! programmaticChange) {
overlay.setTransparency(((Number) spinnerTransparency.getValue()).intValue() / 100.0f);
}
}//GEN-LAST:event_spinnerTransparencyStateChanged
private void spinnerXOffsetStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerXOffsetStateChanged
if (! programmaticChange) {
overlay.setOffsetX(((Number) spinnerXOffset.getValue()).intValue());
}
}//GEN-LAST:event_spinnerXOffsetStateChanged
private void spinnerYOffsetStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerYOffsetStateChanged
if (! programmaticChange) {
overlay.setOffsetY(((Number) spinnerYOffset.getValue()).intValue());
}
}//GEN-LAST:event_spinnerYOffsetStateChanged
private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
cancel();
}//GEN-LAST:event_buttonCancelActionPerformed
private void buttonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOKActionPerformed
ok();
}//GEN-LAST:event_buttonOKActionPerformed
private void checkBoxImageOverlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxImageOverlayActionPerformed
if (! programmaticChange) {
overlay.setEnabled(checkBoxImageOverlay.isSelected());
}
}//GEN-LAST:event_checkBoxImageOverlayActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton buttonCancel;
private javax.swing.JButton buttonFitToDimension;
private javax.swing.JButton buttonOK;
private javax.swing.JCheckBox checkBoxImageOverlay;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel labelFile;
private javax.swing.JSpinner spinnerScale;
private javax.swing.JSpinner spinnerTransparency;
private javax.swing.JSpinner spinnerXOffset;
private javax.swing.JSpinner spinnerYOffset;
// End of variables declaration//GEN-END:variables
private final Overlay overlay;
private final Dimension dimension;
private final boolean originalEnabled;
private final float originalScale, originalTransparency;
private final int originalOffsetX, originalOffsetY;
private boolean programmaticChange = true;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy