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

Thread: Won't Draw??

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Won't Draw??

    Hi, I'm having trouble trying to get my program to draw a rectangle when I click somewhere in the window. I really have no idea why it won't work, everything seems to be in place. Can someone please help?

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Paint;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
     
    public class testing extends Applet implements KeyListener{
    	boolean drawRedBlock = false;
    	int x = 0;
    	int y = 0;
    		public void paint(Graphics g){
    			g.setColor(Color.white);
    			g.fillRect(0,0,500,500);
    			g.setColor(Color.black);
    			g.drawLine(0,0,500,0);
    			g.drawLine(500,500,500,0);
    			g.drawLine(500,500,0,500);
    			g.drawLine(0,500,0,0);
    			g.setColor(Color.red);
    			g.fillRect(125,125,125,125);
    		}
     
    		public void mouseClicked(MouseEvent e) {
    			drawRedBlock = true;
    			x=e.getX();
    			y=e.getY();
    			repaint();
    		}
    		public void drawRedBlock(Graphics g){
    			if(drawRedBlock=true)
    			{
    				g.setColor(Color.red);
    				g.fillRect(x, y, 125, 125);
    				repaint();
    			}
    		}
    		public void mouseEntered(MouseEvent arg0) {}
    		public void mouseExited(MouseEvent arg0) {}
    		public void mousePressed(MouseEvent arg0) {}
    		public void mouseReleased(MouseEvent arg0) {}
    		public void keyPressed(KeyEvent e) {}
    		public void keyReleased(KeyEvent e) {}
    		public void keyTyped(KeyEvent e) {}
    	}


  2. #2
    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: Won't Draw??

    You have the implementation to receive mouse and key events, but for them to work properly you also need to register the listener with a particular component, for example a JPanel.
    JPanel panel = new JPanel();
    panel.addMouseListener(this); //assuming this refers to an object that implements MouseListener
    or you can add the listener directly to an applet
    this.addMouseListener(this);
    Either of these should be done only once, such as in the init function of the applet

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Won't Draw??

    Sorry, I'm a beginner, I've inserted the registration of the MouseListener in the code yet it still has errors for some reason, under each parenthesis, any idea why?

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Paint;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
     
    import javax.swing.JPanel;
     
    public class testing extends Applet implements MouseListener{
    	[B]this.addMouseListener(this);[/B]
    	boolean drawRedBlock = false;
    	int x = 0;
    	int y = 0;
    		public void paint(Graphics g){
    			g.setColor(Color.white);
    			g.fillRect(0,0,500,500);
    			g.setColor(Color.black);
    			g.drawLine(0,0,500,0);
    			g.drawLine(500,500,500,0);
    			g.drawLine(500,500,0,500);
    			g.drawLine(0,500,0,0);
    			g.setColor(Color.red);
    			g.fillRect(125,125,125,125);
    		}
     
    		public void mouseClicked(MouseEvent e) {
    			drawRedBlock = true;
    			x=e.getX();
    			y=e.getY();
    			repaint();
    		}
    		public void drawRedBlock(Graphics g){
    			if(drawRedBlock=true)
    			{
    				g.setColor(Color.red);
    				g.fillRect(x, y, 125, 125);
    				repaint();
    			}
    		}
    		public void mouseEntered(MouseEvent arg0) {}
    		public void mouseExited(MouseEvent arg0) {}
    		public void mousePressed(MouseEvent arg0) {}
    		public void mouseReleased(MouseEvent arg0) {}
    		public void keyPressed(KeyEvent e) {}
    		public void keyReleased(KeyEvent e) {}
    		public void keyTyped(KeyEvent e) {}
    	}
    So now it shows errors everywhere under each parenthesis and it seems like i didn't put it in the right place or something, yet I've tried putting it in several different places and none of them worked, even if they didn't show errors when I did.

  4. #4
    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: Won't Draw??

    As I alluded to above, applets have a method init() which is called once when a browser loads the applet. It should be overridden and any initial setup code, such as adding mouse listeners or setting up the gui, should be placed in the overridden init method. Fundamentally, you cannot reference 'this' outside of a non-static function

  5. #5
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Won't Draw??

    Ok, so something like this?

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Paint;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
     
    import javax.swing.JPanel;
     
    public class Drawing extends Applet implements MouseListener{
    	[B]public void init() {
    		this.addMouseListener(this);
    		if(this.mouseClicked(this))
    		{
    			x=getX();
    			y=getY();
    			drawRedBlock = true;
    		}[/B]
    	}
    	private boolean mouseClicked(Drawing drawing) {
    		return false;
     
    	}
    	boolean drawRedBlock = false;
    	int x = 0;
    	int y = 0;
    		public void paint(Graphics g){
    			g.setColor(Color.white);
    			g.fillRect(0,0,500,500);
    			g.setColor(Color.black);
    			g.drawLine(0,0,500,0);
    			g.drawLine(500,500,500,0);
    			g.drawLine(500,500,0,500);
    			g.drawLine(0,500,0,0);
    			g.setColor(Color.red);
    			g.fillRect(125,125,125,125);
    		}
    		public void drawRedBlock(Graphics g){
    			if(drawRedBlock=true)
    			{
    				g.setColor(Color.red);
    				g.fillRect(x, y, 125, 125);
    				repaint();
    			}
    		}
    		public void mouseEntered(MouseEvent arg0) {}
    		public void mouseExited(MouseEvent arg0) {}
    		public void mousePressed(MouseEvent arg0) {}
    		public void mouseReleased(MouseEvent arg0) {}
    		public void keyPressed(KeyEvent e) {}
    		public void keyReleased(KeyEvent e) {}
    		public void keyTyped(KeyEvent e) {}
    		@Override
    		public void mouseClicked(MouseEvent e) {
    			// TODO Auto-generated method stub
     
    		}
    	}

Similar Threads

  1. lucky draw.. (pls help)
    By amin in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 20th, 2009, 11:30 PM
  2. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM
  3. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM