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 14 of 14

Thread: How to retain Graphics after resizing java applet

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

    Lightbulb How to retain Graphics after resizing java applet

    Is there is any way of how to retain Graphics after resizing java applet.
    Actually when i resize or minimize that applet all the graphics that i have made are disappeared.

    I want to tell all of you in advance that I'm not using any paint function at all.
    I have used local graphics variable for drawing shape on a panel in java applet.

    The applet I'm creating is a simple Paint applet.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: How to retain Graphics after resizing java applet

    How are you "drawing shapes on a panel" if you are not using any paint functions?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    Default Re: How to retain Graphics after resizing java applet

    I have told that i am using local graphics variable. and reference to that variable is taken from [Panelname].getgraphics() method

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: How to retain Graphics after resizing java applet

    I would guess that the Graphics are not being redrawn when the repaint method is called. The repaint method will get called every time you resize or minimize the component. By default, the repaint method clears the graphics on the component. So you may not be directly using the paint methods, but I would bet the Graphics object is. Perhaps simply creating a method that calls the draw method of the Graphics object, and putting:
    @Override
    public void paintComponent(Graphics g) {
         super.paintComponent(g);
         call-whatever-method-draws-the-graphics;
    }
    will solve your problem.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: How to retain Graphics after resizing java applet

    Quote Originally Posted by maz3r View Post
    I have told that i am using local graphics variable. and reference to that variable is taken from [Panelname].getgraphics() method
    This is not a good way to draw Swing graphics because (as you're finding out) the Graphics object obtained will not be the correct one if a repaint occurs. A better way is as aussiemcgr showed you. Another technique you can use is to draw your images to a BufferedImage object (yes, you can get its Graphics context via getGraphics()), and then draw that BufferedImage to the JComponent in the JComponent's paintComponent method.

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

    Default Re: How to retain Graphics after resizing java applet

    Hey can you tell me that when and how that paintComponents method is called??

    Because i didn't know how to implement this method

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: How to retain Graphics after resizing java applet

    The paintComponent() method is defined in many swing components. You need to override it and add code as needed to draw your graphics.

    Take a look at the tutorial:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: How to retain Graphics after resizing java applet

    Sorry!!!!!
    But i have not use any swing component in my application.
    So please tell me a different solution.
    If you want i can post here a snippet of my applet code.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: How to retain Graphics after resizing java applet

    I don't understand why you are not using swing components. There are many benefits with them like double buffering to prevent flickering.
    With the AWT components, you override the paint() method instead of the paintComponent() method.

    If you could make a small complete program that compiles, executes and shows the problem, that would help with testing the problem.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: How to retain Graphics after resizing java applet

     
     
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
     
    /*<applet code="drawit" width=1000 height=600></applet>*/
     
     
    public class dr extends Applet implements ActionListener,MouseListener,MouseMotionListener {
    	Button oval;
    	Panel buttonPanel,toolPanel;
    	Color xorcolor,maincolor;
    	String shape,color,getx,gety;
    	Choice brushchoice,brushsize;
    	int mouseX,mouseY,initialX,initialY,height,width,i,j,mainX,mainY,prevX,prevY;
    	boolean t=true;
        boolean initialoval=true;
    	public void init() {
    		setLayout(new BorderLayout());
    		oval=new Button("OVAL");
            buttonPanel=new Panel();
    		toolPanel=new Panel();
            buttonPanel.setLayout(new GridLayout(12,2,5,5));
    		buttonPanel.setBackground(Color.blue);
    		toolPanel.setBackground(Color.gray);
    		buttonPanel.add(oval);
    		this.add(buttonPanel,BorderLayout.WEST);
    		this.add(toolPanel,BorderLayout.NORTH);
     
     
     
    		//********************LISTENER FOR ALL OBJECTS****************************
     
                    addMouseListener(this);
                    addMouseMotionListener(this);
    		oval.addActionListener(this);
     
    	}
     
     
     
     
    	public void mouseClicked(MouseEvent me){
     
    		}
     
    	public void mouseEntered(MouseEvent me){
              }
     
     
     
    	public void mouseReleased(MouseEvent me){
    	   repaint();
    		}
     
     
    	public void mouseDragged(MouseEvent me){
     
    	drawoval(me);
    	    }
     
     
     
    	public void mouseExited(MouseEvent me){
    		repaint();
    		}
     
    	public void mouseMoved(MouseEvent me){
    		repaint();
    		}
     
    	public void mousePressed(MouseEvent me){
    	refreshValues(me);
            }
     
    	public void actionPerformed(ActionEvent ae){
    		if(ae.getSource()==oval)
    		{
    		//here shape will be defined
    		shape="OVAL";
    				}
    		}
     
     
            public void refreshValues(MouseEvent me)
            {
                mainX=me.getX();
                mainY=me.getY();
                initialX=me.getX();
                initialY=me.getY();
                height=0;width=0;
                mouseX=me.getX();
                mouseY=me.getY();
            }
    	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){
        	Graphics2D g=(Graphics2D)getGraphics();
        	if(initialoval)
            {
                refreshValues(me);
                initialoval=false;
            }
            		g.setColor(maincolor);
       			g.setXORMode(Color.white);
         		g.drawOval(mainX,mainY,width,height);
                setBoundary(me);
     	        g.drawOval(mainX,mainY,width,height);
     
        	}
     
        public void update(Graphics gm)
        {
     
            Graphics2D g=(Graphics2D)gm;
            t=true;
    		g.setColor(maincolor);
    		if(!initialoval)g.drawOval(mainX,mainY,width,height);initialoval=true;
    		showStatus("Shape : "+shape+"  Color : "+color+"  Cursor:  X:"+getx+"  Y:"+gety);
        }
     
        public void paint(Graphics gm)
        {
            update(gm);
        }
     
     
    }


    Here is the small part of that code in which i have problem.
    It is an edited code so please ignore additional things. and focus on oval.
    In this code you can draw Oval on the screen.
    But all the Oval that you drawn are disable if windows is resized.
    So please my problem in that applet.
    I will solve it in my main application

  11. #11
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: How to retain Graphics after resizing java applet

    You should get rid of your update method, get rid of your drawOval method and do all drawing in the paint(...) method using the Graphics object provided. Be sure to call the super.paint(gm) method first, and be sure to remove the call to update(gm) from within paint(...).

    After you've done this, then read through some of the Swing tutorial to learn how to do this in Swing.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: How to retain Graphics after resizing java applet

    Your code doesn't draw the ovals when the paint method is called because the window has been resized or because of a call to repaint.
    The paint method needs to control the drawing of the ovals on the screen because it is called when the window is resized or the repaint() is called.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: How to retain Graphics after resizing java applet

    Quote Originally Posted by curmudgeon View Post
    You should get rid of your update method, get rid of your drawOval method and do all drawing in the paint(...) method using the Graphics object provided. Be sure to call the super.paint(gm) method first, and be sure to remove the call to update(gm) from within paint(...).

    After you've done this, then read through some of the Swing tutorial to learn how to do this in Swing.
    I have done this that you told me
    Here is the code
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
     
    /*<applet code="drawit" width=1000 height=600></applet>*/
     
     
    public class dr extends Applet implements ActionListener,MouseListener,MouseMotionListener {
    	Button oval;
    	Panel buttonPanel,toolPanel;
    	Color xorcolor,maincolor;
    	String shape,color,getx,gety;
    	Choice brushchoice,brushsize;
    	int mouseX,mouseY,initialX,initialY,height,width,i,j,mainX,mainY,prevX,prevY;
    	boolean t=true;
        boolean initialoval=true;
    	public void init() {
    		setLayout(new BorderLayout());
    		oval=new Button("OVAL");
            buttonPanel=new Panel();
    		toolPanel=new Panel();
            buttonPanel.setLayout(new GridLayout(12,2,5,5));
    		buttonPanel.setBackground(Color.blue);
    		toolPanel.setBackground(Color.gray);
    		buttonPanel.add(oval);
    		this.add(buttonPanel,BorderLayout.WEST);
    		this.add(toolPanel,BorderLayout.NORTH);
     
     
     
    		//********************LISTENER FOR ALL OBJECTS****************************
     
                    addMouseListener(this);
                    addMouseMotionListener(this);
    		oval.addActionListener(this);
     
    	}
     
     
     
     
    	public void mouseClicked(MouseEvent me){
     
    		}
     
    	public void mouseEntered(MouseEvent me){
              }
     
     
     
    	public void mouseReleased(MouseEvent me){
     
    		}
     
     
    	public void mouseDragged(MouseEvent me){
     
    	drawoval(me);
    	    }
     
     
     
    	public void mouseExited(MouseEvent me){
     
    		}
     
    	public void mouseMoved(MouseEvent me){
    			}
     
    	public void mousePressed(MouseEvent me){
    	refreshValues(me);
            }
     
    	public void actionPerformed(ActionEvent ae){
    		if(ae.getSource()==oval)
    		{
    		//here shape will be defined
    		shape="OVAL";
    				}
    		}
     
     
            public void refreshValues(MouseEvent me)
            {
                mainX=me.getX();
                mainY=me.getY();
                initialX=me.getX();
                initialY=me.getY();
                height=0;width=0;
                mouseX=me.getX();
                mouseY=me.getY();
            }
    	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){
        	Graphics2D g=(Graphics2D)getGraphics();
        	if(initialoval)
            {
                refreshValues(me);
                initialoval=false;
            }
            		g.setColor(maincolor);
       			g.setXORMode(Color.white);
         		g.drawOval(mainX,mainY,width,height);
                setBoundary(me);
     	        g.drawOval(mainX,mainY,width,height);
     
        	}
     
     
        public void paint(Graphics gm)
        {
          super.paint(gm);
            t=true;
    		gm.setColor(maincolor);
    		if(!initialoval)gm.drawOval(mainX,mainY,width,height);initialoval=true;
    		showStatus("Shape : "+shape+"  Color : "+color+"  Cursor:  X:"+getx+"  Y:"+gety);
        }
     
     
    }

    Still problem persists.

    Is this task cannot be done without using swing?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: How to retain Graphics after resizing java applet

    What is this line of code supposed to do?
    if(!initialoval)gm.drawOval(mainX,mainY,width,height);initialoval=true;
    Why hide the statements like that?


    Try debugging the code by adding a println statement to the paint method to see when it is called.

    The drawing code needs to be called from the paint() method.

    Another test. Change the color used in the paint() method:
    		gm.setColor(Color.magenta); //<<<<<<<<<maincolor);
    Then draw an oval and slowly drag the side to change the size. Notice the color of oval changes to magenta and then the oval disappears.
    Last edited by Norm; August 25th, 2012 at 12:50 PM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How To Retain Entity Reference Character While Reading XML
    By leon850515 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2011, 09:10 AM
  2. retain checked status of checkbox
    By rahulj5 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: November 29th, 2010, 12:10 AM
  3. Java Graphics help
    By Stockholm Syndrome in forum AWT / Java Swing
    Replies: 9
    Last Post: November 4th, 2010, 06:23 PM
  4. Dynamically resizing an applet
    By mjpam in forum AWT / Java Swing
    Replies: 6
    Last Post: September 19th, 2010, 11:32 PM
  5. JAVA Image Icon and JButton resizing problem
    By antitru5t in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2009, 05:39 AM