Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Tell me the Problem in that java Applet

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Tell me the Problem in that java Applet

    I have made an Applet in java for a paint application.
    In which i can use rect, oval, etc.

    Here's my code.....
    package drawit;
     
     
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
     
    /*<applet code="drawit1" width=1000 height=600></applet>*/
     
     
    public class Drawit1 extends Applet implements ItemListener,AdjustmentListener,ActionListener,MouseListener,MouseMotionListener {
     
     
    	Button red,yellow,white,black,orange,blue,magenta,gray,green,user,cyan,darkgray,pink;
    	Button fillrect,rect,oval,filloval,pen,brush,polygon;
    	Panel buttonPanel,imagePanel,toolPanel;
    	Color xorcolor,maincolor;
    	Scrollbar r,g,b;
    	TextField rt,gt,bt;
    	String shape,color,getx,gety;
    	Choice brushchoice,brushsize;
    	int mouseX,mouseY,initialX,initialY,height,width,i,j,mainX,mainY;
    	boolean t=true;
     
    	public void init() {
     
    //		setLayout(new GridLayout(2,1,10,10));
    		setLayout(new BorderLayout());
    		//***********************************Button Declaration Only*******************************************
     
    		red=new Button("Red");
    		yellow=new Button("Yellow");
    		cyan=new Button("Cyan");
    		black=new Button("Black");
    	    blue=new Button("Blue");
    		orange=new Button("Orange");
    		white=new Button("White");
    		magenta=new Button("Magenta");
    		gray=new Button("Gray");
    		green=new Button("Green");
    		pink=new Button("Pink");
    		darkgray=new Button("Dark Gray");
    		user=new Button("Custom");
    		filloval=new Button("FILL OVAL");
    		fillrect=new Button("FILL RECT");
    		rect=new Button("RECT");
    		oval=new Button("OVAL");
    		pen=new Button("PEN");
    		brush=new Button("BRUSH");
     
     
    		red.setBackground(Color.red);
    		yellow.setBackground(Color.yellow);
    		white.setBackground(Color.white);
    		black.setBackground(Color.black);
    		blue.setBackground(Color.blue);
    		magenta.setBackground(Color.magenta);
    		gray.setBackground(Color.gray);
    		pink.setBackground(Color.PINK);
    		green.setBackground(Color.GREEN);
    		darkgray.setBackground(Color.DARK_GRAY);
    		orange.setBackground(Color.orange);
    		cyan.setBackground(Color.CYAN);
    		black.setForeground(Color.WHITE);
    		darkgray.setForeground(Color.white);
    	//	user.setBackground(Color.BLACK);
     
    		r=new Scrollbar(Scrollbar.HORIZONTAL,0,0,0,256);
    		b=new Scrollbar(Scrollbar.HORIZONTAL,0,0,0,256);
    		g=new Scrollbar(Scrollbar.HORIZONTAL,0,0,0,256);
     
    		rt=new TextField("0");
    		bt=new TextField("0");
    		gt=new TextField("0");
    		rt.setEditable(false);
    		bt.setEditable(false);
    		gt.setEditable(false);
     
    		brushchoice=new Choice();
    		brushsize=new Choice();
     
    		buttonPanel=new Panel();
    		toolPanel=new Panel();
    		imagePanel=new Panel();
     
    		buttonPanel.setLayout(new GridLayout(12,2,0,0));
    		buttonPanel.setBackground(Color.blue);
    		imagePanel.setBackground(Color.white);
    		toolPanel.setBackground(Color.gray);
    		//****************Adding Component********************
     
     
    		buttonPanel.add(cyan);
    		buttonPanel.add(yellow);
    		buttonPanel.add(black);
    		buttonPanel.add(gray);
    		buttonPanel.add(pink);
    		buttonPanel.add(blue);
    		buttonPanel.add(darkgray);
    		buttonPanel.add(green);
    		buttonPanel.add(orange);
    		buttonPanel.add(white);
    		buttonPanel.add(magenta);
    		buttonPanel.add(user);
    		buttonPanel.add(fillrect);
    		buttonPanel.add(filloval);
    		buttonPanel.add(rect);
    		buttonPanel.add(oval);
    		buttonPanel.add(pen);
    		buttonPanel.add(brush);
    		buttonPanel.add(r);
    		buttonPanel.add(rt);
    		buttonPanel.add(g);
    		buttonPanel.add(gt);
    		buttonPanel.add(b);
    		buttonPanel.add(bt);
     
     
    	//	this.add(new GridLayout(1,1,0,0));
    	//	this.add(new GridLayout(1,2,0,0));
     
     
     
    		this.add(buttonPanel,BorderLayout.WEST);
    		this.add(imagePanel,BorderLayout.CENTER);
    		this.add(toolPanel,BorderLayout.NORTH);
     
     
     
    		//********************LISTENER FOR ALL OBJECTS****************************
    		r.addAdjustmentListener(this);
    		b.addAdjustmentListener(this);
    		g.addAdjustmentListener(this);
    		imagePanel.addMouseMotionListener(this);
    		imagePanel.addMouseListener(this);
    		red.addActionListener(this);
    		cyan.addActionListener(this);
    		yellow.addActionListener(this);
    		black.addActionListener(this);
    		darkgray.addActionListener(this);
    		blue.addActionListener(this);
    		pink.addActionListener(this);
    		green.addActionListener(this);
    		orange.addActionListener(this);
    		magenta.addActionListener(this);
    		white.addActionListener(this);
    		gray.addActionListener(this);
    		user.addActionListener(this);
    		oval.addActionListener(this);
    		filloval.addActionListener(this);
    		fillrect.addActionListener(this);
    		rect.addActionListener(this);
    		pen.addActionListener(this);
    		brush.addActionListener(this);
     
     
     
     
    	}
     
     
    	public void itemStateChanged(ItemEvent ie)
     
    	{
    		repaint();
    	}
    	public void adjustmentValueChanged(AdjustmentEvent ae)
    	{
    		if(ae.getSource()==r)
    		{
    			rt.setText(r.getValue()+"");
    		}
    		if(ae.getSource()==b)
    		{
    			bt.setText(b.getValue()+"");
    		}
    		if(ae.getSource()==g)
    		{
    			gt.setText(g.getValue()+"");
    		}
    		user.setBackground(new Color(r.getValue(),g.getValue(),b.getValue()));
     
    		repaint();
    	}
     
    	public void mouseClicked(MouseEvent me){
     
     
     
    		}
     
    	public void mouseEntered(MouseEvent me){}
     
    	public void mouseReleased(MouseEvent me){
    		t=true;
    		Graphics g=imagePanel.getGraphics();
    		g.setColor(maincolor);
     
    			switch(i)
    		{
    			case 1:g.drawOval(mainX,mainY,width,height);break;
    			case 2:g.drawRect(mainX,mainY,width,height);break;
    			case 3:g.fillRect(mainX,mainY,width,height);break;
    			case 4:g.fillOval(mainX,mainY,width,height);break;
    			case 5:break;
    			case 6:break;
     
    		}
    		}
     
     
    	public void mouseDragged(MouseEvent me){
     
    		switch(i)
    		{
    			case 1:drawoval(me);break;
    			case 2:drawrect(me);break;
    			case 3:fillrect(me);break;
    			case 4:filloval(me);break;
    			case 5:drawpen(me);break;
    			case 6:drawbrush(me);break;
     
    		}
     
     
     
    		}
     
     
     
    	public void mouseExited(MouseEvent me){
    		getx="---";
    		gety="---";
    		repaint();
    		}
     
    	public void mouseMoved(MouseEvent me){
    		getx=me.getX()+"";
    		gety=me.getY()+"";
    		//repaint();
    		}
     
    	public void mousePressed(MouseEvent me){
    	}
     
    	public void actionPerformed(ActionEvent ae){
     
    		if(ae.getSource()==red)
    		{
    		maincolor=Color.red;
    		color="RED";	
    		}
    		if(ae.getSource()==yellow)
    		{
    		maincolor=Color.yellow;
    		color="YELLOW";	
    		}
    		if(ae.getSource()==green)
    		{
    		maincolor=Color.green;
    		color="Green";	
    		}
    		if(ae.getSource()==cyan)
    		{
    		maincolor=Color.cyan;
    		color="CYAN";	
    		}
    		if(ae.getSource()==black)
    		{
    		maincolor=Color.black;
    		color="BLACK";	
    		}
    		if(ae.getSource()==pink)
    		{
    		maincolor=Color.pink;
    		color="PINK";	
    		}
    		if(ae.getSource()==blue)
    		{
    		maincolor=Color.blue;
    		color="BLUE";	
    		}
    		if(ae.getSource()==magenta)
    		{
    		maincolor=Color.magenta;
    		color="MAGENTA";	
    		}
    		if(ae.getSource()==orange)
    		{
    		maincolor=Color.orange;
    		color="ORANGE";	
    		}
    		if(ae.getSource()==green)
    		{
    		maincolor=Color.green;
    		color="GREEN";	
    		}
    		if(ae.getSource()==gray)
    		{
    		maincolor=Color.gray;
    		color="GRAY";	
    		}
    		if(ae.getSource()==darkgray)
    		{
    		maincolor=Color.DARK_GRAY;
    		color="DARK GRAY";	
    		}
    		if(ae.getSource()==white)
    		{
    		maincolor=Color.white;
    		color="WHITE";	
    		}
    		if(ae.getSource()==user)
    		{
    		maincolor=new Color(r.getValue(),g.getValue(),b.getValue());
    		color="CUSTOM";	
    		}
    		if(ae.getSource()==oval)
    		{
    		//here shape will be defined
    		shape="OVAL";
    		i=1;	
    		}
    		if(ae.getSource()==rect)
    		{
    		//here shape will be defined
    		shape="RECTANGLE";	
    		i=2;
    		}
    		if(ae.getSource()==fillrect)
    		{
    		//here shape will be defined
    		shape="FILLED RECTANGLE";	
    		i=3;
    		}
    		if(ae.getSource()==filloval)
    		{
    		//here shape will be defined
    		shape="FILLED OVAL";	
    		i=4;
    		}
    		if(ae.getSource()==pen)
    		{
    		//here shape will be defined
    		shape="PEN";	
    		i=5;
    		}
    		if(ae.getSource()==brush)
    		{
    		//here shape will be defined
    		shape="BRUSH";	
    		i=6;
    		}
     
     
    		}
     
     
     
    	public void setBoundary(MouseEvent me)
    	{
     
    		if(t)
    		{
    			initialX=me.getX();
    			initialY=me.getY();
    			height=0;width=0;
    			t=false;
    		}
     
     
     
     
    		mouseX=me.getX();
    		mouseY=me.getY();
    		if(mouseX<initialX || mouseY<initialY)
    		{
    			if(mouseX<initialX)
    			{
    				width=initialX-mouseX;
    				mainX=initialX-width;
    			}
    			else
    			{
    				width=mouseX-initialX;
    				mainX=initialX;
    			}
    			if(mouseY<initialY)
    			{
    				height=initialY-mouseY;
    				mainY=initialY-height;
    			}
    			else
    			{
    				height=mouseY-initialY;
    				mainY=initialY;
    			}
     
    		}
    		else
    		{
    			height=mouseY-initialY;
    			width=mouseX-initialX;
    			mainX=initialX;
    			mainY=initialY;
    		}
    	}
     
        public void drawoval(MouseEvent me){
     
        	Graphics g=imagePanel.getGraphics();
     
        		g.setColor(maincolor);
       			g.setXORMode(Color.white);
         		g.drawOval(mainX,mainY,width,height);
          		setBoundary(me);
     			g.drawOval(mainX,mainY,width,height);
     
        	}
     
        public void drawrect(MouseEvent me){
        		Graphics g=imagePanel.getGraphics();
     
        		g.setColor(maincolor);
       			g.setXORMode(new Color(255,255,255));
         		g.drawRect(mainX,mainY,width,height);
          		setBoundary(me);
     			g.drawRect(mainX,mainY,width,height);
     			}
     
        public void fillrect(MouseEvent me){
        		Graphics g=imagePanel.getGraphics();
     
        		g.setColor(maincolor);
       			g.setXORMode(Color.white);
         		g.fillRect(mainX,mainY,width,height);
          		setBoundary(me);
     			g.fillRect(mainX,mainY,width,height);
        	}
     
        public void filloval(MouseEvent me){
     
        		Graphics g=imagePanel.getGraphics();
        		g.setColor(maincolor);
       			g.setXORMode(Color.white);
         		g.fillOval(mainX,mainY,width,height);
          		setBoundary(me);
     			g.fillOval(mainX,mainY,width,height);
        	}
     
        public void drawpen(MouseEvent me){}
     
        public void drawbrush(MouseEvent me){}
     
     
     
    	//public void paint(Graphics g) {
    		//update(g);
    		//g.drawString("Welcome to Java!!", 50, 60 );
    	//showStatus("Shape: "+shape +"    Color: "+color+"    Cursor: X:"+getx+" Y:"+gety);
    	//}
    }
    In this applet i have a problem that when i draw a OVAL it will draw without any problem.
    But when i draw another OVAL then last OVAL shape disappear.


    And second problem is that...
    When i Draw a OVAL shape and then select Rectangle by buttons
    then As i clicked on image to draw rectangle then last OVAL shape is automatically surrounded by a Rectangle.

    I don't what the both problem is.
    That's by i didn't complete that applet


    I'm waiting for ur reply.
    Please must tell me the solution.
    Last edited by maz3r; August 20th, 2012 at 09:08 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tell me the Problem in that java Applet

    Please edit you post and change the quote tags to code tags. The posted code is hard to read without the proper formatting.
    But when i draw another OVAL then last OVAL shape disappear.
    You get a fresh graphics context every time the paint method is called.
    You need to save a list of what shapes you want drawn for the paint method to be able to "remember" the shapes drawn before.

    The above may not apply to your code because you are not using the paint method.
    But you should be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tell me the Problem in that java Applet

    Thank you very much...
    But my problem is solved without using any paint method..

    Actually i missed to use a function in which all the integer variables are set to default mouse location..

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tell me the Problem in that java Applet

    Have you solved your problem?

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tell me the Problem in that java Applet

    Yes, I have solved my problem.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Tell me the Problem in that java Applet

    Thread moved from Whats wrong with my code.

Similar Threads

  1. Problem Applet Error
    By mohsendeveloper in forum Java SE APIs
    Replies: 24
    Last Post: January 19th, 2012, 04:00 PM
  2. Problem with Java Classes, in an applet
    By SashaThompson in forum Object Oriented Programming
    Replies: 12
    Last Post: October 2nd, 2011, 07:15 AM
  3. Applet problem with MouseEvent
    By smashX in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 10th, 2011, 03:24 PM
  4. Java applet programming problem
    By danielparry in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 4th, 2011, 11:31 AM
  5. applet and JPS problem.... please help me
    By rockster14 in forum Java Applets
    Replies: 0
    Last Post: August 7th, 2009, 03:59 PM