Task: Image noise





Java: import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.Arrays;
import java.util.Random;
import javax.swing.*;

public class ImageNoise {
    int framecount = 0;
    int fps = 0;
    BufferedImage image;
    Kernel kernel;
    ConvolveOp cop;
    JFrame frame = new JFrame("Java Image Noise");

    JPanel panel = new JPanel() {
        private int show_fps = 0; 
        private MouseAdapter ma = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                show_fps = (show_fps + 1) % 3;
            }
        };
        {addMouseListener(ma);}

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(320, 240);
        }

        @Override
        @SuppressWarnings("fallthrough")
        public void paintComponent(Graphics g1) {
            Graphics2D g = (Graphics2D) g1;
            drawNoise();
            g.drawImage(image, 0, 0, null);

            switch (show_fps) {
            case 0: 
                
                int xblur = getWidth() - 130, yblur = getHeight() - 32;
                BufferedImage bc = image.getSubimage(xblur, yblur, 115, 32);
                BufferedImage bs = new BufferedImage(bc.getWidth(), bc.getHeight(),
                                                     BufferedImage.TYPE_BYTE_GRAY);
                cop.filter(bc, bs);
                g.drawImage(bs, xblur, yblur , null);
            case 1: 
                
                g.setColor(Color.RED);
                g.setFont(new Font("Monospaced", Font.BOLD, 20));
                g.drawString("FPS: " + fps, getWidth() - 120, getHeight() - 10);
            }
            framecount++;
        }
    };
    
    
    Timer repainter = new Timer(1, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            panel.repaint();
        }
    });
    
    
    Timer framerateChecker = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            fps = framecount;
            framecount = 0;
        }
    });
    
    public ImageNoise() {
        
        float[] vals = new float[121];
        Arrays.fill(vals, 1/121f);
        kernel = new Kernel(11, 11, vals);
        cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        
        
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        repainter.start();
        framerateChecker.start();
    }

    void drawNoise() {
        int w = panel.getWidth(), h = panel.getHeight();
        
        
        if (null == image || image.getWidth() != w || image.getHeight() != h) {
            image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
        }
        Random rand = new Random();
        int[] data = new int[w * h];
        
        for (int x = 0; x < w * h / 32; x++) {
            int r = rand.nextInt();
            for (int i = 0; i < 32; i++) {
                data[x * 32 + i] = (r & 1) * Integer.MAX_VALUE;
                r >>>= 1;
            }
        }
        
        image.getRaster().setPixels(0, 0, w, h, data);
    }
    
    public static void main(String[] args) {
        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ImageNoise i = new ImageNoise();
            }
        });
    }
}




VB: Imports System.Drawing.Imaging

Public Class frmSnowExercise
    Dim bRunning As Boolean = True

    Private Sub Form1_Load(ByVal sender As System.Object,
                           ByVal e As System.EventArgs) Handles MyBase.Load

        
        
        
        SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint _
            Or ControlStyles.OptimizedDoubleBuffer, True)
        UpdateStyles()

        
        
        FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
        MaximizeBox = False

        
        
        
        
        Width = 320 + Size.Width - ClientSize.Width
        Height = 240 + Size.Height - ClientSize.Height

        
        
        Show()
        Activate()
        Application.DoEvents()

        
        RenderLoop()

        
        Close()

    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As _
            System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        
        If e.KeyChar = ChrW(Keys.Escape) Then bRunning = False
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As _
            System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        
        
        e.Cancel = bRunning
        bRunning = False
    End Sub

    Private Sub RenderLoop()

        Const cfPadding As Single = 5.0F

        Dim b As New Bitmap(ClientSize.Width, ClientSize.Width,
                            PixelFormat.Format32bppArgb)
        Dim g As Graphics = Graphics.FromImage(b)
        Dim r As New Random(Now.Millisecond)
        Dim oBMPData As BitmapData = Nothing
        Dim oPixels() As Integer = Nothing
        Dim oBlackWhite() As Integer = {Color.White.ToArgb, Color.Black.ToArgb}
        Dim oStopwatch As New Stopwatch
        Dim fElapsed As Single = 0.0F
        Dim iLoops As Integer = 0
        Dim sFPS As String = "0.0 FPS"
        Dim oFPSSize As SizeF = g.MeasureString(sFPS, Font)
        Dim oFPSBG As RectangleF = New RectangleF(ClientSize.Width - cfPadding -
                      oFPSSize.Width, cfPadding, oFPSSize.Width, oFPSSize.Height)

        
        g.Clear(Color.Black)

        
        oBMPData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height),
                          ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)

        
        
        
        Array.Resize(oPixels, b.Width * b.Height)
        Runtime.InteropServices.Marshal.Copy(oBMPData.Scan0,
                                             oPixels, 0, oPixels.Length)
        b.UnlockBits(oBMPData)
        
        Do
            
            
            fElapsed += oStopwatch.ElapsedMilliseconds / 1000.0F
            oStopwatch.Reset() : oStopwatch.Start()
            
            iLoops += 1
            If fElapsed >= 1.0F Then
                
                
                
                
                
                
                
                sFPS = (iLoops / fElapsed).ToString("0.0") & " FPS"
                oFPSSize = g.MeasureString(sFPS, Font)
                oFPSBG = New RectangleF(ClientSize.Width - cfPadding -
                    oFPSSize.Width, cfPadding, oFPSSize.Width, oFPSSize.Height)
                
                
                fElapsed -= 1.0F
                iLoops = 0
            End If

            
            For i As Integer = 0 To oPixels.GetUpperBound(0)
                oPixels(i) = oBlackWhite(r.Next(oBlackWhite.Length))
            Next

            
            oBMPData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height),
                       ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb)
            
            Runtime.InteropServices.Marshal.Copy(oPixels, 0, oBMPData.Scan0,
                                                 oPixels.Length)
            b.UnlockBits(oBMPData)

            
            g.FillRectangle(Brushes.Black, oFPSBG)
            
            g.DrawString(sFPS, Font, Brushes.Yellow, oFPSBG.Left, oFPSBG.Top)

            
            BackgroundImage = b
            Invalidate(ClientRectangle)

            
            Application.DoEvents()
        Loop While bRunning

    End Sub
End Class