473,387 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:

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
4 3930
JosAH
11,448 Expert 8TB
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
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
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
r035198x
13,262 8TB
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

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

Similar topics

0
by: Mickel Grönroos | last post by:
Hi, I'm trying to put an Tkinter.Entry of fixed size onto a specific location on a canvas using the place manager. The idea is that one can double-click a rectangle object on a canvas to get an...
7
by: Michael Galvin | last post by:
I am trying to use Python to send to the printer a calender filled with a mix of text and simple graphics. I want to draw on the printed page something like a table with 6 rows and 7 columns to...
0
by: nevada17 | last post by:
My goal is to find a way to apply scroll bars across the y axis of about 25 check buttons so that my interface isn't too large. I've read about scroll bars and I realize that's its not possible to...
2
TMS
by: TMS | last post by:
Schools over!!! Now its time to play. I would like to learn how to make objects move from one location to the next on a canvas widget. For example: from Tkinter import * class square:...
6
by: Nebulism | last post by:
I have been attempting to utilize a draw command script that loads a canvas, and through certain mouse events, draws rectangles. The original code is from...
1
by: =?Utf-8?B?bWlzc0JsdWVCYXI=?= | last post by:
Could someone please tell me how to add a circle (ellipse) to an InkCanvas but as a stroke - a collection of StylusPoints. The requirements is to allow the user to create a sketch on the canvas...
0
by: nish88 | last post by:
hi everybody...i'm trying to place an oval with a mouse click on my tkinter interface. can anyone please give me the code of how should i do it. i just want the part that i should add so that by...
2
by: almurph | last post by:
Hi, Hope you can help me with one please. I import an image and display it on a picture box control. I then have functionality change its size (code included below). Now I need to add some text...
4
by: moondaddy | last post by:
I have a wpf project where I use a canvas to drag shapes around. when I drag a shape beyond the right or bottom side I get scrollbars which is good. I also get scrollbars when I zoom in and a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.