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: -
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.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
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
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
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: -
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.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);
}
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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. ...
|
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),
|
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,...
| |
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...
|
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...
|
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...
|
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. ...
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |