use of org.jwildfire.swing.ImagePanel in project JWildfire by thargor6.
the class MeshGenController method getPreviewPanel.
private ImagePanel getPreviewPanel() {
if (previewPanel == null) {
int width = previewRootPanel.getWidth();
int height = previewRootPanel.getHeight();
SimpleImage img = new SimpleImage(width, height);
img.fillBackground(0, 0, 0);
previewPanel = new ImagePanel(img, 0, 0, previewRootPanel.getWidth());
previewRootPanel.add(previewPanel, BorderLayout.CENTER);
previewRootPanel.getParent().validate();
previewRootPanel.repaint();
}
return previewPanel;
}
use of org.jwildfire.swing.ImagePanel in project JWildfire by thargor6.
the class MutaGenController method mutate.
protected void mutate(int pRow, int pCol) {
try {
if (selectedGenerationIdx >= 0 && selectedGenerationIdx < mutationList.size()) {
Dimension renderSize = calcImageSize();
Dimension probeSize = new Dimension(50, 40);
MutationSet selectedSet = mutationList.get(selectedGenerationIdx);
final int rows = MUTA_ROWS;
final int cols = MUTA_COLS;
Flame baseFlame = selectedSet.getFlame(pRow, pCol);
baseFlame = createWeightedFlame(selectedSet.getBaseFlame(), baseFlame);
boolean doMorph = fabs((Double) amountREd.getValue() - 1.0) > EPSILON;
List<Flame> mutations = new ArrayList<Flame>();
initProgress(rows, cols);
int centreX = rows / 2;
int centreY = cols / 2;
SimpleImage baseFlameImg = renderFlame(baseFlame.makeCopy(), probeSize, false);
SimpleImage simplifiedBaseFlameImg = RandomFlameGeneratorSampler.createSimplifiedRefImage(baseFlameImg);
int step = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// Let centre flame untouched
SimpleImage renderedImg = null;
if ((i != centreX || j != centreY)) {
int x = (i - centreX);
int y = (j - centreY);
final int MAX_ITER = 10;
final double MIN_RENDER_COVERAGE = 0.32;
final double MIN_DIFF_COVERAGE = 0.18;
final double INVALID_COVERAGE = -1.0;
int iter = 0;
double bestCoverage = INVALID_COVERAGE;
Flame bestMutation = null;
while (true) {
Flame currMutation = baseFlame.makeCopy();
List<MutationType> mutationTypes = createMutationTypes(x, y);
modifyFlame(currMutation, x, y, mutationTypes);
renderedImg = renderFlame(currMutation.makeCopy(), probeSize, true);
double coverage = renderedImg != null ? RandomFlameGeneratorSampler.calculateCoverage(renderedImg, 0, 0, 0, true) : INVALID_COVERAGE;
if (coverage > MIN_RENDER_COVERAGE) {
coverage = RandomFlameGeneratorSampler.calculateDiffCoverage(renderedImg, simplifiedBaseFlameImg);
}
if (coverage > MIN_DIFF_COVERAGE) {
mutations.add(currMutation);
if (doMorph) {
Flame morphed = createWeightedFlame(baseFlame, currMutation);
renderedImg = renderFlame(morphed, renderSize, false);
} else {
renderedImg = renderFlame(currMutation, renderSize, false);
}
break;
} else if (coverage > bestCoverage) {
bestCoverage = coverage;
bestMutation = currMutation;
}
// Don't count invalid mutations
if (renderedImg != null) {
iter++;
}
if (iter >= MAX_ITER) {
mutations.add(bestMutation);
if (doMorph) {
Flame morphed = createWeightedFlame(baseFlame, bestMutation);
renderedImg = renderFlame(morphed, renderSize, false);
} else {
renderedImg = renderFlame(bestMutation, renderSize, false);
}
break;
}
}
} else {
mutations.add(baseFlame.makeCopy());
renderedImg = renderFlame(baseFlame.makeCopy(), renderSize, false);
}
ImagePanel pnl = imagePanels[i][j];
pnl.setImage(renderedImg);
showProgress(++step);
try {
pnl.invalidate();
Graphics g = pnl.getGraphics();
if (g != null) {
pnl.paint(g);
}
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}
MutationSet newSet = new MutationSet(rows, cols, baseFlame, mutations);
mutationList.add(newSet);
selectedGenerationIdx = mutationList.size() - 1;
enableControls();
}
} catch (Throwable ex) {
errorHandler.handleError(ex);
}
}
use of org.jwildfire.swing.ImagePanel in project JWildfire by thargor6.
the class MutaGenController method calcImageSize.
private Dimension calcImageSize() {
createImagePanels();
ImagePanel pnl = imagePanels[0][0];
Dimension size = pnl.getSize();
int imageWidth = Tools.FTOI(size.getWidth());
int imageHeight = Tools.FTOI(size.getHeight());
return new Dimension(imageWidth, imageHeight);
}
use of org.jwildfire.swing.ImagePanel in project JWildfire by thargor6.
the class MutaGenController method createImagePanels.
private void createImagePanels() {
if (imagePanels == null) {
try {
final int rows = MUTA_ROWS;
final int cols = MUTA_COLS;
final int BORDER_SIZE = 0;
((JPanel) flamePanels[0].getParent()).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(37, 0), "selectLeftCellAction");
((JPanel) flamePanels[0].getParent()).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(39, 0), "selectRightCellAction");
((JPanel) flamePanels[0].getParent()).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(38, 0), "selectTopCellAction");
((JPanel) flamePanels[0].getParent()).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(40, 0), "selectBottomCellAction");
((JPanel) flamePanels[0].getParent()).getActionMap().put("selectLeftCellAction", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
selectLeftCell();
}
});
((JPanel) flamePanels[0].getParent()).getActionMap().put("selectRightCellAction", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
selectRightCell();
}
});
((JPanel) flamePanels[0].getParent()).getActionMap().put("selectTopCellAction", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
selectUpperCell();
}
});
((JPanel) flamePanels[0].getParent()).getActionMap().put("selectBottomCellAction", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
selectLowerCell();
}
});
imagePanels = new ImagePanel[rows][cols];
int idx = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
JPanel panel = flamePanels[idx++];
Dimension size = panel.getSize();
SimpleImage img = new SimpleImage(Tools.FTOI(size.getWidth()), Tools.FTOI(size.getHeight()));
final ImagePanel imgPanel = new ImagePanel(img, 0, 0, img.getImageWidth());
imagePanels[i][j] = imgPanel;
imgPanel.setImage(img);
imgPanel.setLocation(BORDER_SIZE, BORDER_SIZE);
final int row = i;
final int col = j;
imgPanel.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
if (e.getClickCount() == 2 || e.getButton() == MouseEvent.BUTTON3) {
mutate(row, col);
} else if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON2) {
export(row, col);
}
}
@Override
public void mousePressed(MouseEvent e) {
selectCell(row, col);
}
});
panel.add(imgPanel);
}
}
} catch (Throwable ex) {
imagePanels = null;
errorHandler.handleError(ex);
}
}
}
use of org.jwildfire.swing.ImagePanel in project JWildfire by thargor6.
the class DetachedPreviewController method refreshImagePanel.
private void refreshImagePanel() {
if (imagePanel != null) {
imageRootPanel.remove(imagePanel);
}
ImgSize size = getImgSize();
image = new SimpleImage(size.getWidth(), size.getHeight());
image.fillBackground(prefs.getTinaRandomBatchBGColorRed(), prefs.getTinaRandomBatchBGColorGreen(), prefs.getTinaRandomBatchBGColorBlue());
imagePanel = new ImagePanel(image, 0, 0, image.getImageWidth());
imagePanel.setSize(image.getImageWidth(), image.getImageHeight());
imagePanel.setPreferredSize(new Dimension(image.getImageWidth(), image.getImageHeight()));
imageRootPanel.add(imagePanel, BorderLayout.CENTER);
imageRootPanel.getParent().validate();
}
Aggregations