473,531 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

placing/adding canvas on/to applet

9 New Member
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 3938
JosAH
11,448 Recognized Expert MVP
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
foleyflint
9 New Member
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
Need help54321
30 New Member
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 MVP
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
2193
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 entry field of the same size as the rectangle placed exactly over the rectangle thus creating the effect that the rectangle has entered "input...
7
3011
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 represent a calendar. I want to place text precisely within those boxes on the printed page. I am using Python 2.4 on Windows XP I was in the...
0
1594
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 add scroll bars to a frame, window, or check buttons while using the Perl TK. I was wondering whether anyone has hand any luck with a work around. ...
2
18230
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: def __init__(self, canvas, xy, color, change): self.canvas = canvas self.id = self.canvas.create_rectangle(-10-abs(change),
6
3533
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 http://www.java2s.com/Code/Python/Event/Usemousetodrawashapeoncanvas.htm . The code itself is: from Tkinter import * trace = 0 class CanvasEventsDemo: def __init__(self,...
1
5591
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 and allow circles and rectangles to be dragged onto the canvas. I then playback the formation of the canvas with animation. I can do all but the...
0
1200
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 clicking on interface an initial oval appear. thank you in advance.. here is my code. from Tkinter import * import Tkinter from Tkconstants...
2
1690
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 *not* on image but beside the image, like an area to the side of the image say but not actually on the image (likn on a lable that can be added to...
4
10079
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 shape goes beyond the right or bottom side. However, I don't get scrollbars when shapes move beyond the left or top side of the canvas. This is bad. ...
0
7359
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7286
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7528
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7677
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7270
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3341
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
570
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.