By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,826 Members | 815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,826 IT Pros & Developers. It's quick & easy.

placing/adding canvas on/to applet

P: 9
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:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.awt.event.*;
  4. import java.awt.image.*;
  5. import java.io.*;
  6. import java.util.*;
  7. import java.net.*; 
  8. import java.awt.Graphics;
  9.  
  10. public class TLCS extends Applet {
  11.  
  12.     Image image;
  13.     ImageCanvas canvas1;
  14.     Graphics g;
  15.  
  16.     public void init()
  17.     { 
  18.       setSize(774,536);
  19.       Toolkit toolkit = Toolkit.getDefaultToolkit();
  20.       image = toolkit.getImage("Crossing.gif");
  21.       canvas1 = new ImageCanvas();
  22.       canvas1.setBounds(50, 80, 39, 99);
  23.       //setLocation(50, 100);
  24.  
  25.       add(canvas1);  
  26.     }
  27.  
  28.     public void paint(Graphics g)
  29.     {
  30.         g.drawImage(image, 0, 0, this);
  31.     }
  32.  
  33. }
  34.  
  35. import java.awt.Canvas;
  36. import java.awt.Color;
  37. import java.awt.Font;
  38. import java.awt.Image;
  39. import java.awt.Graphics;
  40. import java.awt.SystemColor;
  41. import java.awt.Toolkit;
  42.  
  43.  
  44. public class ImageCanvas extends Canvas {
  45.  
  46. private Image image;
  47. protected int width = 39;
  48. protected int height = 99;
  49. Image img;
  50.  
  51. public ImageCanvas() {
  52. //this.image = image;
  53.     setSize(39,99);
  54. }
  55.  
  56.  
  57. public void paint(Graphics g)
  58. {
  59.     Toolkit toolkit = Toolkit.getDefaultToolkit();
  60.     img = toolkit.getImage("red3.gif");
  61.         g.drawImage(img, 0, 0, this);
  62. }
  63. }
  64.  
The applet does work: it displays the background image "crossing.gif" 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
May 26 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 10K+
P: 11,448
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
May 26 '07 #2

P: 9
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
May 26 '07 #3

P: 30
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:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.awt.event.*;
  4. import java.awt.image.*;
  5. import java.io.*;
  6. import java.util.*;
  7. import java.net.*; 
  8. import java.awt.Graphics;
  9.  
  10. public class TLCS extends Applet {
  11.  
  12.     Image image;
  13.     ImageCanvas canvas1;
  14.     Graphics g;
  15.  
  16.     public void init()
  17.     { 
  18.       setSize(774,536);
  19.       Toolkit toolkit = Toolkit.getDefaultToolkit();
  20.       image = toolkit.getImage("Crossing.gif");
  21.       canvas1 = new ImageCanvas();
  22.       canvas1.setBounds(50, 80, 39, 99);
  23.       //setLocation(50, 100);
  24.  
  25.       add(canvas1);  
  26.     }
  27.  
  28.     public void paint(Graphics g)
  29.     {
  30.         g.drawImage(image, 0, 0, this);
  31.     }
  32.  
  33. }
  34.  
  35. import java.awt.Canvas;
  36. import java.awt.Color;
  37. import java.awt.Font;
  38. import java.awt.Image;
  39. import java.awt.Graphics;
  40. import java.awt.SystemColor;
  41. import java.awt.Toolkit;
  42.  
  43.  
  44. public class ImageCanvas extends Canvas {
  45.  
  46. private Image image;
  47. protected int width = 39;
  48. protected int height = 99;
  49. Image img;
  50.  
  51. public ImageCanvas() {
  52. //this.image = image;
  53.     setSize(39,99);
  54. }
  55.  
  56.  
  57. public void paint(Graphics g)
  58. {
  59.     Toolkit toolkit = Toolkit.getDefaultToolkit();
  60.     img = toolkit.getImage("red3.gif");
  61.         g.drawImage(img, 0, 0, this);
  62. }
  63. }
  64.  
The applet does work: it displays the background image "crossing.gif" 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.getContentPane();


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

c.add(lbl1);

ImageIcon icon2 = new ImageIcon("golfball.jpg");
lbl2.setIcon(icon2);



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

jf1.setVisible(true);



}
}
Jun 5 '07 #4

10K+
P: 13,264
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.getContentPane();


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

c.add(lbl1);

ImageIcon icon2 = new ImageIcon("golfball.jpg");
lbl2.setIcon(icon2);



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.
Jun 6 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.