Home arrow static arrow Java Programming [Archive] - applets not working help urgent
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - applets not working help urgent
This topic has 6 replies on 1 page.

Posts:7
Registered: 7/18/04
applets not working help urgent  
Jul 22, 2004 11:37 PM



 
Thsi is a applet to draw a Slider implemented using rectangles with two points to slide on the same slider with two different mousedrag functions. So i have implemented it using two classes . Can anybody kindly help me please.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.image.*;

public class Events1 extends Applet {

public void init() {

setLayout(new BorderLayout());
add(new VMCanvas());
add(new SMCanvas());
}

class SMCanvas extends Canvas implements MouseMotionListener{

int mouseX =100,mouseY=100,mouseX2=496,mouseY2=100;

public void SMcanvas() {
addMouseMotionListener(this);

}
//public void mousedragged
public void paint(Graphics g)
{
g.setColor(Color.yellow);
g.fillRect(mouseX2,mouseY2,5,15);
g.setColor(Color.green);
g.fillRect(mouseX2+5,100,500-mouseX2-6,15);
}

public void mouseDragged(MouseEvent me) {
if((me.getX() >= 100) && (me.getX() <= 496)) {

if((me.getY() == 100) || (me.getX() == 496)) {
mouseX2 = me.getX();
mouseY2 = me.getY();

repaint();
}
}
}

public void mouseMoved(java.awt.event.MouseEvent mouseEvent) {
}

}

class VMCanvas extends Canvas implements MouseMotionListener{

int mouseX =100,mouseY=100,mouseX2=496,mouseY2=100;

public void VMcanvas() {
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
g.drawRect(100,99,401,16);
g.setColor(Color.red);
g.fillRect(100,100,400,15);
g.setColor(Color.green);
g.fillRect(100,100,mouseX-100,15);
g.setColor(Color.blue);
g.fillRect(mouseX,mouseY,5,15);
}

public void mouseDragged(MouseEvent e) {
if((e.getX() >= 100) && (e.getX() <= 496))
{
if((e.getY() == 100) || (e.getX() == 100)) {
mouseX = e.getX();
mouseY = e.getY();

repaint();
}
}
}

public void mouseMoved(java.awt.event.MouseEvent mouseEvent) {
}

}

This is the HTML code

<html>
<head>
<title>
Welcome to applets
</title>
</head>

<body>
<center>

Welcome to the World of Applets>
</center>

<center>
<applet
code =Events1.class
width = 600
height = 400>
</applet>
</center>
</body>
</html>

 

Posts:5,119
Registered: 16.08.01
Re: applets not working help urgent  
Jul 23, 2004 12:03 AM (reply 1 of 6)



 
when you post code please use the [code][/code] tags.

What help do you need? Do you get exceptions? What is supposed to happen and what actually happens?
 

Posts:7
Registered: 7/18/04
Re: applets not working help urgent  
Jul 23, 2004 12:41 AM (reply 2 of 6)



 
The want the SMCanvas paint() function and the VMCanvas paint() function to be executed, but only the SMCanvas paint() function is executed and only that image is displayed.

 

Posts:12,831
Registered: 2/22/00
Re: applets not working help urgent  
Jul 23, 2004 12:49 AM (reply 3 of 6)



 

I think that's because you're using a BorderLayout, but using the one-arg version of Panel.add (in Events1's init method).

I may be wrong but I think that this will result in both components being added to the center position, one overwriting the other.

Read the docs for BorderLayout, in particular the stuff about the BorderLayout.POSITION fields. (Where POSITION is SOUTH, EAST, CENTER, etc.)
 

Posts:7
Registered: 7/18/04
Re: applets not working help urgent  
Jul 23, 2004 3:28 AM (reply 4 of 6)



 
I have tried that but still it is not working and i actually want the two images to map on each other with the
smaller one on the bigger image.
 

Posts:37
Registered: 7/23/04
Re: applets not working help urgent  
Jul 23, 2004 4:07 AM (reply 5 of 6)



 
Here I give you a code which is in a single applet not using two canvas class, it works like slider.
I think you got solution by executing this code.
If you want to ask any other query thn mail me at chirag813@yahoo.com

import java.awt.*;import java.awt.event.*;import java.applet.*;import java.awt.image.*; public class TempApplet extends Applet implements MouseMotionListener{	int mouseX =10,mouseY=100,mouseX2=300,mouseY2=40;	int x=10,y=100,width=40,height=40;	int x1=270,y1=150,width1=40,height1=40;	public void init()	{		addMouseMotionListener(this);		 	} //public void mousedraggedpublic void paint(Graphics g){ g.setColor(Color.red);g.fillRect(mouseX,mouseY,mouseX2,mouseY2);g.setColor(Color.blue);g.fillRect(x,y,width,height);g.setColor(Color.blue);g.fillRect(x1,y1,width1,height1);g.drawString("x2 : "+mouseX2+" y2 : "+mouseY2,10,10);}  //public void mousePressed(MouseEvent me){}//public void mouseReleased(MouseEvent me){}//public void mouseEntered(MouseEvent me){}//public void mouseExited(MouseEvent me){}//public void mouseClicked(MouseEvent me){} public void mouseDragged(MouseEvent me) {	if(me.getX()>=10 && me.getX()<=270 && me.getY()>=100 && me.getY()<=140)		{			x = me.getX();			x1=270-x;		}		 repaint();} public void mouseMoved(java.awt.event.MouseEvent me) { } 		}  
 

Posts:7
Registered: 7/18/04
Re: applets not working help urgent  
Jul 23, 2004 9:25 PM (reply 6 of 6)



 
This was not what i wanted but, any how from this code i got what was wanted thanks a lot
 
This topic has 6 replies on 1 page.