Results 1 to 15 of 15

Thread: Paint program

  1. #1
    aarti is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Paint program

    I am writing a program where if the user clicks, it should draw a point. The following code does not seem to work properly. If I click, it draws a point but vanishes later on. I tried using paint and paint component but I am not able to use them. Please help!!

    Java Code:
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class GUIClass extends JFrame{
    /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    public Container c;
    JButton jbtnStart;
    JButton jbtnEnd;
    JPanel panel;
    Border border;
    StartListener start;
    ClickHandler click;
    EndListener end;
    Point point;
    int width,height,x,y;
    Image backbuffer;
    Graphics bg;
    
    public static void main (String[] args)
    {
    	new GUIClass();
    }
    
    GUIClass()
    {
    	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    	setSize(screenSize);
    	setResizable (false);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	c = getRootPane();                                                       
    	c.setLayout(null);
    	
    	panel = new JPanel();
    	panel.setBounds(10,10,screenSize.width-30,screenSize.height-100);
    	border = BorderFactory.createLineBorder(Color.black,4);
    	panel.setBackground(Color.WHITE);
    	panel.setBorder(border);
    	click = new ClickHandler();
    	panel.addMouseListener(click);
    	c.add(panel);
    	
    	jbtnStart = new JButton("START");
    	jbtnStart.setBounds(750, screenSize.height-80, 100, 50);
    	start = new StartListener();
    	jbtnStart.addActionListener(start);
    	c.add(jbtnStart);
    	
    	jbtnEnd = new JButton("END");
    	jbtnEnd.setBounds(900, screenSize.height-80, 100, 50);
    	end = new EndListener();
    	jbtnEnd.addActionListener(end);
    	c.add(jbtnEnd);
    	
    	setVisible(true);
    	
    }
    
    
    class StartListener implements ActionListener
    {
    	public void actionPerformed(ActionEvent e1)
    	{
    		
    	}
    }
    class EndListener implements ActionListener
    {
    	public void actionPerformed(ActionEvent e1)
    	{
    		System.exit(0);
    	}
    }
    /*public void update(Graphics g)
    {
    	
    	bg.fillOval(x-5, y-5, 10, 10);
    }
    public void paintComponent (Graphics g)
    {
    	super.paint(g);
    	bg.fillOval(x-5, y-5, 10, 10);
    	update(g);
    }*/
    
    class ClickHandler implements MouseListener
    {
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    		 x = e.getX();
    		 y = e.getY();
    		System.out.println(x+" "+y);
    		bg = panel.getGraphics();
    		bg.fillOval(x, y, 50, 50);
    		repaint();
    		e.consume();
    	}
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    }
    }

  2. #2
    wassat676 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    what do you mean by "disappears later on"?

  3. #3
    aarti is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    means when i click, a point appears and immediately vanishes... i mean i can see a point being placed on the the JPanel where I click however it does not get drawn there. If you could try running the code you will know what i mean.

  4. #4
    wassat676 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    getContentPane().repaint();

    that is all.

    youre welcome
    Last edited by wassat676; 05-06-2011 at 12:45 AM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    Quote Originally Posted by wassat676 View Post
    getContentPane().repaint();

    that is all.
    Won't help since the OP is wrongly using getGraphics() and not performing custom painting in the correct way.

    Never use getGraphics() of a Component in client code.
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  6. #6
    wassat676 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    true story. The OP is not what I would have done, but it seemed to fix the problem in this case.4

    *Figuring out a mistake is rewarding despite how its implemented.
    Last edited by wassat676; 05-06-2011 at 05:55 AM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by wassat676 View Post
    true story. The OP is not what I would have done, but it seemed to fix the problem in this case.
    There's a big difference between a fix and a kludge. I fix solves the problem, end of story. A kludge puts a band aid on the wound, packs it dresses it, and allows for functional use, but the wound still remains and still can fester and explode at the most inopportune times. Your suggestion is a kludge and doesn't address the real underlying issue as Darryl has described.

  8. #8
    wassat676 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    alright so Darryl wins -1 wassat676 congratulations.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by wassat676 View Post
    alright so Darryl wins -1 wassat676 congratulations.
    I score it differently: The OP wins because he has gotten good advice from Darryl. If all he had were your advice, he'd think that there was nothing wrong with using getGraphics and so the error would perpetuate.

  10. #10
    wassat676 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    glad i could help

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by wassat676 View Post
    glad i could help
    You did. You're probably like me in that you usually give good advice but occasionally misread the problem or mis understand it, and then drop a bomb. It happens to us all. Myself, I try to apologize, learn from the experience and move on.

  12. #12
    wassat676 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    lived and learned

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    Some other comments for the OP:
    -- Declare variables in the tightest possible scope. All those variables don't need to be instance fields.
    -- Most of the time, instance fields should be declared as private, unless ther is a clear requirement of direct access from outside the class.
    -- Use meaningful variable names. Leave aside others, even you may be puzzled when you revisit the code after 6 months or so.
    -- Always construct, manipulate and query all Swing components only on the EDT.
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Threads and Swing
    The Last Word in Swing Threads

    db

    edit
    -- Don't play with the layout of the rootPane
    -- Don't add anything directly to the rootPane

    edit2
    -- By extending MouseAdapter instead of implementing MouseListener, you could eliminate the empty methods.
    -- Don't use a null layout. Learn to use layout managers
    http://download.oracle.com/javase/tu...out/index.html

    Heck, just go through the entire trail and learn all about coding a Swing GUI!
    http://download.oracle.com/javase/tu...ing/index.html
    Last edited by DarrylBurke; 05-06-2011 at 06:40 AM.

  14. #14
    wassat676 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    I guess since were all here, can I ask why its bad to use a null layout?

    i.e. my thread below

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

Similar Threads

  1. paint program
    By dewdadamnthang in forum New To Java
    Replies: 4
    Last Post: 03-30-2011, 11:40 AM
  2. Help with paint program
    By michcan in forum Java 2D
    Replies: 1
    Last Post: 02-04-2011, 06:26 AM
  3. Paint Program Help
    By ngiannini in forum AWT / Swing
    Replies: 12
    Last Post: 05-10-2010, 04:24 PM
  4. Simple Paint program question
    By StressaJune in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 08:46 PM
  5. Replies: 3
    Last Post: 12-31-2008, 05:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •