Skip to content

Instantly share code, notes, and snippets.

@RavuAlHemio
Created June 24, 2011 21:28
Show Gist options
  • Save RavuAlHemio/1045732 to your computer and use it in GitHub Desktop.
Save RavuAlHemio/1045732 to your computer and use it in GitHub Desktop.
"Soften image" Java applet
/*
* ButtPlet.java
*
* Created on Jun 24, 2011, 10:46:14 PM
*/
package foon;
import java.applet.Applet;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.Graphics;
import java.io.FileInputStream;
import java.io.IOException;
/**
*
* @author ondra
*/
public class ButtPlet extends Applet {
// FIXME: hardcoded is stupid
private static final String imagePath = "/home/ondra/Pictures/gir.gif";
private BufferedImage bi;
/** Initializes the applet ButtPlet */
@Override
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
imagePanel = new javax.swing.JPanel();
setLayout(new java.awt.BorderLayout());
imagePanel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
imagePanelMouseClicked(evt);
}
});
add(imagePanel, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
private void imagePanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imagePanelMouseClicked
int btn = evt.getButton();
if (bi == null)
btn = MouseEvent.BUTTON2;
if (btn == MouseEvent.BUTTON1)
{
int w = bi.getWidth(), h = bi.getHeight();
BufferedImage newBi = new BufferedImage(w, h, bi.getType());
// next two for loops: for each pixel
for (int x = 0; x < w; ++x)
{
for (int y = 0; y < h; ++y)
{
// array of neighbor pixels' color values
Color[] pixelVals = new Color[9];
// iterator into pixelVals
int pixie = 0;
// next two for loops: for all nine neighbors
for (int ax = x-1; ax <= x+1; ++ax)
{
for (int ay = y-1; ay <= y+1; ++ay)
{
if (ax < 0 || ay < 0 || ax >= w || ay >= h)
{
// out of bounds
continue;
}
// get color and store it into pixelVals
int clr = bi.getRGB(ax, ay);
pixelVals[pixie++] = new Color(clr, true);
}
}
// running sums for calculating average
int sumr = 0, sumg = 0, sumb = 0;
// pixie contains the actual number of elements used in pixelVals here
for (int i = 0; i < pixie; ++i)
{
sumr += pixelVals[i].getRed();
sumg += pixelVals[i].getGreen();
sumb += pixelVals[i].getBlue();
}
// calculate average color and set it in the new image
Color targetColor = new Color(sumr / pixie, sumg / pixie, sumb / pixie);
newBi.setRGB(x, y, targetColor.getRGB());
}
}
bi = newBi;
repaint();
}
else if (btn == MouseEvent.BUTTON2)
{
// reload image
try
{
BufferedImage tmp = javax.imageio.ImageIO.read(new FileInputStream(imagePath));
int w = tmp.getWidth(), h = tmp.getHeight();
int[] pixels = tmp.getRGB(0, 0, w, h, null, 0, w);
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
bi.setRGB(0, 0, w, h, pixels, 0, w);
}
catch (IOException _)
{
}
repaint();
}
}//GEN-LAST:event_imagePanelMouseClicked
@Override
public void paint(Graphics g)
{
g.drawImage(bi, 0, 0, null);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel imagePanel;
// End of variables declaration//GEN-END:variables
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment