# Images in Java mitels Pixelgrabber



## oska (15. Oktober 2003)

Ich hab mittles Pixelgrabber ein Bild in einem int-Array gespeichert um es während der Laufzeit bearbeiten zu können.
Java scheint dabei allerdings ein Problem zu haben. Ich vermute mal das, dass Speichermanagement den Flaschenhals darstellt, denn ab über 300*300 Pixeln kommt das ganze ins stottern und bei zwei Bildern ist das ne Diashow. Ich zeichne das Bild mitels drawImage() auf den Screen. Hat jemand ne Ahnung was da schief läuft.
System Athlon 1400, 512 MB

Thanks


----------



## Thomas Darimont (15. Oktober 2003)

Servus!

Damit hatte ich auch schon mal zu kämpfen ... da hilft nur ... effizienter Programmieren ... schau mal auf http://www.javagaming.org/cgi-bin/JGNetForums/YaBB.cgi dort dürftest du genügend Performancetipps dazu finden ...

Mein "Versuch von "damals" ... super langsam, weil sch315e Programmiert ...


```
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.*;

/*
 * Crea2.java
 *
 * Created on 25. Juli 2003, 20:01
 */

/**
 *
 * @author  Administrator
 */
public class Crea2 extends javax.swing.JFrame {
    
    private int x = 0;
    private int y = 0;
    
    private int dx;
    private int dy;
    
    
    private Image img = null;
    private Graphics2D gra = null;
    private boolean running = true;
    private int w = 200;
    private int h = 200;
    private int pix[] = new int[w * h];
    private int index = 0;
    private Random r = null;
    private Thread t = null;
    int red = 0;
    int blue = 0;
    
    
    
    
    
    /** Creates new form Crea2 */
    public Crea2() {
        initComponents();
        prepareImg();
        
        r = new Random();
        start();
    }
    
       
    private void start(){
        t = new Thread(new Test());
        t.start();
    }
    
    
    private void prepareImg(){
        for (int y = 0; y < h; y++) {
            int red = (y * 255) / (h - 1);
            for (int x = 0; x < w; x++) {
                int blue = (x * 255) / (w - 1);
                pix[index++] = (255 << 24) | (red << 16) | blue;
            }
        }
        img = createImage(new MemoryImageSource(w, h, pix, 0, w));
        
        gra = (Graphics2D)jPanel1.getGraphics();
    }
    
    
    /** 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.
     */
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setMinimumSize(new java.awt.Dimension(400, 300));
        jPanel1.setPreferredSize(new java.awt.Dimension(400, 300));
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanel1MousePressed(evt);
            }
        });

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }
    
    private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {
        // Add your handling code here:
    }
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new Crea2().show();
    }
    
    /*
    public void paint(Graphics g){
        super.paint(g);
     
     
    }
     **/
    
    class Test implements Runnable{
        
        public void run(){
            
            while(true){
                
                if(!running){
                    while(!running){
                        try{
                            Thread.currentThread().sleep(100);
                        }catch(InterruptedException ie){ie.printStackTrace();}
                    }
                } else {
                    
                    while(running){
                        
                        index = 0;
                        
                        
                        dy += 2;
                        dx += 2;
                        
                        for(int y = 0; y < h; y++){
                             
                        
                            for( int x = 0; x < w; x++){
                               
                                
                                red = (int)(180+ 180 *Math.sin( (x + dx) / (37 + 15 * Math.sin( (y+dy) / 74))) * Math.sin( (y+dy) / (31 + 11 * Math.sin((x + dx)/37))));
                                blue = (int)(180 + 180 *Math.cos( (x + dx) / (37 + 15 * Math.cos( (y + dy) / 74))) * Math.cos((y+dy) / (31 + 11 * Math.cos((x+dx)/37))));
                                
                                pix[index++] = (16 << 24) | (red << 16) | blue;
                                
                                //System.out.println("x: " + x + " y: "+y +" index: " + index);
                            }
                        }
                        
                        
                        img = createImage(new MemoryImageSource(w, h, pix, 0, w));
                        
                        try{
                            
                            Thread.currentThread().sleep(10);
                            
                        }catch(InterruptedException ie){ ie.printStackTrace();}
                        if(img != null)
                            gra.drawImage(img,50,50,200,200,jPanel1);
                        
                    }
                }
                
            }
            
        }
        
    }
    
    
    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    // End of variables declaration
    
}
```

Gruß Tom


----------



## oska (15. Oktober 2003)

jup Ich dank dir!


----------

