donderdag 31 maart 2011

Creating an Image and drawing into a Image.

It took me a while. This is one of the first times that I actually started programming in java. I tried to create a image in memory and to draw in that created image.

The applet below shows a red block. This is a Image that was created in java and then drawin into using the red color. It is drawn only once into the applet.









import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class MyCreateImage extends Applet
{
     Graphics bufferGraphics;
     Image offscreen;
     Image image2;

     Dimension dim;

     public void init()
     {
        dim = getSize();
        setBackground(Color.black);
        offscreen = createImage(dim.width,dim.height);
       bufferGraphics = offscreen.getGraphics();

  image2 = createImage(32,32);
  Graphics test = image2.getGraphics();
  test.setColor(Color.red);
  test.fillRect(0,0,32,32);


     }

      public void paint(Graphics g)
     {
        bufferGraphics.clearRect(0,0,dim.width,dim.width);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("CreateImageExample",10,10);
        g.drawImage(offscreen,0,0,this);
  g.drawImage(image2,30,30,this);

     }

     public void update(Graphics g)
     {
          paint(g);
     }



 }



You can copy and paste this code into a java editor. Be sure to name the project as the same name as the class.

Geen opmerkingen:

Een reactie posten