email me at borlaj@portlandschools.org

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

Layers:

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

 

/*
 * This applet shows how to do layers
 *
 * @author Jeff Borland
 * @date 11-18-11
 */

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 bufferedImageSet;
    Graphics bufferGraphicsSet;
    BufferedImage bufferedImageTemp; 
    Graphics bufferGraphicsTemp;

    public void init()
    {
        addMouseListener(this);
        addMouseMotionListener(this);
        Container screen = getContentPane();
        screen.setBackground(Color.white);
        screen.setLayout (new FlowLayout() );
                
        bufferedImageSet = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
        bufferGraphicsSet = bufferedImageSet.getGraphics(); 
        
        
    }

    public void paint (Graphics g)
    {
        super.paint(g);               

        bufferGraphicsSet.setColor(Color.green);
        bufferGraphicsSet.fillRect(0,0,500,500);
        g.drawImage(bufferedImageSet,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();

        bufferGraphicsSet.setColor(Color.blue);
        bufferGraphicsSet.drawLine(x1,y1,x2,y2);
        
        Graphics g=getGraphics();
        g.drawImage(bufferedImageSet,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)
    {
        bufferedImageTemp = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
        bufferGraphicsTemp = bufferedImageTemp.getGraphics(); 

        x2=e.getX();
        y2=e.getY();

        bufferGraphicsTemp.setColor(Color.yellow);
        bufferGraphicsTemp.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 bufferedImageAll = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);       
        Graphics bufferGraphicsAll = bufferedImageAll.getGraphics();
        
        bufferGraphicsAll.drawImage(bufferedImageSet,0,0,this); 
        bufferGraphicsAll.drawImage(bufferedImageTemp,0,0,this);   
        Graphics g=getGraphics();
        g.drawImage(bufferedImageAll,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

    }

}