placing/adding canvas on/to applet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • foleyflint
    New Member
    • Oct 2006
    • 9

    placing/adding canvas on/to applet

    Hello,
    lately I've been trying to make an applet that has a background image and a couple of canvas objects on it holding an image each.
    I created the class ImageCanvas which extends Canvas adn draws a picture in the canvas.
    Next I have to place the canvas on the applet and there I got stuck because I don't know how to control the position of every canvas. How can I put a canvas exactly there where I want it to be?

    Here is my code so far:

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import java.util.*;
    import java.net.*; 
    import java.awt.Graphics;
    
    public class TLCS extends Applet {
    	
    	Image image;
    	ImageCanvas canvas1;
    	Graphics g;
    	
    	public void init()
        { 
          setSize(774,536);
          Toolkit toolkit = Toolkit.getDefaultToolkit();
          image = toolkit.getImage("Crossing.gif");
          canvas1 = new ImageCanvas();
          canvas1.setBounds(50, 80, 39, 99);
          //setLocation(50, 100);
          
          add(canvas1);  
        }
    
    	public void paint(Graphics g)
    	{
    		g.drawImage(image, 0, 0, this);
    	}
    
    }
    
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Image;
    import java.awt.Graphics;
    import java.awt.SystemColor;
    import java.awt.Toolkit;
    
    
    public class ImageCanvas extends Canvas {
    
    private Image image;
    protected int width = 39;
    protected int height = 99;
    Image img;
    
    public ImageCanvas() {
    //this.image = image;
    	setSize(39,99);
    }
    
    
    public void paint(Graphics g)
    {
    	Toolkit toolkit = Toolkit.getDefaultToolkit();
        img = toolkit.getImage("red3.gif");
    	    g.drawImage(img, 0, 0, this);
    }
    }
    The applet does work: it displays the background image "crossing.g if" and in the center of the window it place the Canvas, but I want to have some more canvasses and place them where I want them.
    Thank you,
    Andre
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    If you had used Swing instead (a JApplet versus an Applet etc.) you could've
    used a JLayeredPane; it's a nice component where other componenents (such
    as JPanels (instead of a Canvas) can 'float' around on top of each other and
    behind each other depending on the 'layer' the JPanel is put in.

    AWT is very outdated; consider using Swing instead; the transition doesn't cost much.

    kind regards,

    Jos

    Comment

    • foleyflint
      New Member
      • Oct 2006
      • 9

      #3
      Originally posted by JosAH
      If you had used Swing instead (a JApplet versus an Applet etc.) you could've
      used a JLayeredPane; it's a nice component where other componenents (such
      as JPanels (instead of a Canvas) can 'float' around on top of each other and
      behind each other depending on the 'layer' the JPanel is put in.

      AWT is very outdated; consider using Swing instead; the transition doesn't cost much.

      kind regards,

      Jos
      Thank you, but I just started working with applets for the first time and finally got to understand something about a Canvas and Applet so if I have to switch I will have to do all my research all over again. My professor told me it would be the easiest way for me if I just used Applet and Canvas, because the result doesn't have to be very fancy.
      I've already done pretty much and only need to know how to position a Canvas, and the rest of the programming would be what I already know.
      Andre

      Comment

      • Need help54321
        New Member
        • Jun 2007
        • 30

        #4
        Originally posted by foleyflint
        Hello,
        lately I've been trying to make an applet that has a background image and a couple of canvas objects on it holding an image each.
        I created the class ImageCanvas which extends Canvas adn draws a picture in the canvas.
        Next I have to place the canvas on the applet and there I got stuck because I don't know how to control the position of every canvas. How can I put a canvas exactly there where I want it to be?

        Here is my code so far:

        Code:
        import java.awt.*;
        import java.applet.*;
        import java.awt.event.*;
        import java.awt.image.*;
        import java.io.*;
        import java.util.*;
        import java.net.*; 
        import java.awt.Graphics;
        
        public class TLCS extends Applet {
        	
        	Image image;
        	ImageCanvas canvas1;
        	Graphics g;
        	
        	public void init()
            { 
              setSize(774,536);
              Toolkit toolkit = Toolkit.getDefaultToolkit();
              image = toolkit.getImage("Crossing.gif");
              canvas1 = new ImageCanvas();
              canvas1.setBounds(50, 80, 39, 99);
              //setLocation(50, 100);
              
              add(canvas1);  
            }
        
        	public void paint(Graphics g)
        	{
        		g.drawImage(image, 0, 0, this);
        	}
        
        }
        
        import java.awt.Canvas;
        import java.awt.Color;
        import java.awt.Font;
        import java.awt.Image;
        import java.awt.Graphics;
        import java.awt.SystemColor;
        import java.awt.Toolkit;
        
        
        public class ImageCanvas extends Canvas {
        
        private Image image;
        protected int width = 39;
        protected int height = 99;
        Image img;
        
        public ImageCanvas() {
        //this.image = image;
        	setSize(39,99);
        }
        
        
        public void paint(Graphics g)
        {
        	Toolkit toolkit = Toolkit.getDefaultToolkit();
            img = toolkit.getImage("red3.gif");
        	    g.drawImage(img, 0, 0, this);
        }
        }
        The applet does work: it displays the background image "crossing.g if" and in the center of the window it place the Canvas, but I want to have some more canvasses and place them where I want them.
        Thank you,
        Andre


        Hey man could u have aloook at mine im not sure how to add the second image i can only get the one to pop up
        import javax.swing.*;
        import java.awt.*;
        public class BackUp
        {
        public static void main(String args [])
        {
        JFrame jf1 = new JFrame("This Is the First Frame");
        JLabel lbl1 = new JLabel();
        JLabel lbl2 = new JLabel();


        jf1.setSize(500 ,500);
        Container c = jf1.getContentP ane();


        ImageIcon icon = new ImageIcon("Golf .jpg");
        lbl1.setIcon(ic on);

        c.add(lbl1);

        ImageIcon icon2 = new ImageIcon("golf ball.jpg");
        lbl2.setIcon(ic on2);



        c.add(lbl2);
        c.add(lbl1);

        jf1.setVisible( true);



        }
        }

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Need help54321
          Hey man could u have aloook at mine im not sure how to add the second image i can only get the one to pop up
          import javax.swing.*;
          import java.awt.*;
          public class BackUp
          {
          public static void main(String args [])
          {
          JFrame jf1 = new JFrame("This Is the First Frame");
          JLabel lbl1 = new JLabel();
          JLabel lbl2 = new JLabel();


          jf1.setSize(500 ,500);
          Container c = jf1.getContentP ane();


          ImageIcon icon = new ImageIcon("Golf .jpg");
          lbl1.setIcon(ic on);

          c.add(lbl1);

          ImageIcon icon2 = new ImageIcon("golf ball.jpg");
          lbl2.setIcon(ic on2);



          c.add(lbl2);
          c.add(lbl1);

          jf1.setVisible( true);



          }
          }
          1.) Hijacking is bad
          2.) Code tags are good.
          3.) Using a layout manager is sweet.

          Comment

          Working...