email me at borlaj@portlandschools.org

Loading
notes previous (12/<12) submit the dump links  
 

Layers:

Examine the code below carefully, it is difficult to explain, see me in class.

 

The key things that you will need to add:

Add import:

import java.awt.image.*;

Add global for set layer:

layerSet = new BufferedImage(2000, 2000, BufferedImage.TYPE_INT_ARGB);

Add this to paint:

g.drawImage(layerSet,0,0,this);

 

Now in your code, when you are drawing anything that is not dynamic, instead of doing g.draw...

 
        Graphics layerSetGraphics=layerSet.getGraphics();
        layerSetGraphics.draw...

And after doing all your drawing on the set layer, you need to show that to the screen:

        Graphics g=getGraphics();
        g.drawImage(layerSet,0,0,this);

For dynamic stuff, you will draw on the temporary

 
        BufferedImage layerTemp = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
        Graphics layerTempGraphics= layerTemp.getGraphics();
  
        layerTempGraphics.draw..

Then put the layerSet and layerTemp together and put it on the screen:

         //Now I create another buffergraphics and draw the 2 layers on top of each other
        //then i add it to g, the screen.  This minimizes flickering.
        BufferedImage layerAll = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);     
        Graphics layerAllGraphics = layerAll.getGraphics();
          
        layerAllGraphics.drawImage(layerSet,0,0,this);
        layerAllGraphics.drawImage(layerTemp,0,0,this); 
        Graphics g=getGraphics();
        g.drawImage(layerAll,0,0,this);

CODE IS BELOW

/*
 * This applet shows how to do layers
 *
 * @author Jeff Borland
 * @date version 1.12
 */
  
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
public class Layers extends JApplet implements MouseListener,MouseMotionListener,ActionListener
{
    int x1=0;
    int y1=0;
    int x2=0;
    int y2=0;
      
    //Below I create 2 bufferGraphics, which is like 2 layers that is drawn
    // offscreen and then add to the screen when done.  They are transparent
    //The 2 layers are named Set and Temp.  The set is when it is finalized
    // The temp is temporary and gets erased constantly.
    BufferedImage layerSet;
     
    public void init()
    {
        addMouseListener(this);
        addMouseMotionListener(this);
        Container screen = getContentPane();
        screen.setBackground(Color.white);
        screen.setLayout (new FlowLayout() );
                  
        layerSet = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);      
        Graphics layerGraphics=layerSet.getGraphics();
        layerGraphics.setColor(Color.green);
        layerGraphics.fillRect(0,0,500,500);           
         
    }
  
    public void paint (Graphics g)
    {
        super.paint(g);             
        g.drawImage(layerSet,0,0,this);
    }
  
    //when someone presses the mouse button
    public void mousePressed(MouseEvent e)
    {    
        x1=e.getX();
        y1=e.getY();
    }
  
    //when someone releases the mouse button I make the line permeant on the set layer
    public void mouseReleased(MouseEvent e)
    {    
  
        x2=e.getX();
        y2=e.getY();
 
        Graphics layerSetGraphics=layerSet.getGraphics();
        layerSetGraphics.setColor(Color.blue);
        layerSetGraphics.drawLine(x1,y1,x2,y2);
          
        Graphics g=getGraphics();
        g.drawImage(layerSet,0,0,this);
          
    }
    // when the mouse enters the applet
    public void mouseEntered(MouseEvent e)
    {    
  
    }
  
    //when the mouse leaves the applet
    public void mouseExited(MouseEvent e)
    {    
  
    }
  
    //when the mouse button is clicked
    public void mouseClicked(MouseEvent e)
    {
  
    }
  
    //the mouse button is pressed and the mouse makes a significantly large movement
    public void mouseDragged(MouseEvent e)
    {
         BufferedImage layerTemp = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
        Graphics layerTempGraphics= layerTemp.getGraphics();
  
        x2=e.getX();
        y2=e.getY();
  
        layerTempGraphics.setColor(Color.yellow);
        layerTempGraphics.drawLine(x1,y1,x2,y2);
  
        //Now I create another buffergraphics and draw the 2 layers on top of each other
        //then i add it to g, the screen.  This minimizes flickering.
        BufferedImage layerAll = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);     
        Graphics layerAllGraphics = layerAll.getGraphics();
          
        layerAllGraphics.drawImage(layerSet,0,0,this);
        layerAllGraphics.drawImage(layerTemp,0,0,this); 
        Graphics g=getGraphics();
        g.drawImage(layerAll,0,0,this);
    }
    //the mouse makes a significantly large movement
    public void mouseMoved(MouseEvent e)
    {
  
    }
  
    public void actionPerformed(ActionEvent thisEvent)
    {
        Object source = thisEvent.getSource();
  
        //now have if statements seeing finding out where the action occured
  
    }
  
}