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

Thread: HELP with Java Paint Program Code

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default HELP with Java Paint Program Code

    I need some help with figuring out how to let a user choose between colors and the size of the line by clicking on the button with the corresponding color name and a JSpinner to increase the size. Basically I need help with the listeners and methods on my color buttons and JSpinner. Here is the code I have so far.

    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    public class myJPanelstd extends JPanel implements MouseMotionListener, ActionListener
    {
    	// Attributes
        JButton bx,by, bmessage,bc1,bc2,bc3,be1;
        JPanel p1;
        int width=5;
        int height=5;
     
        Point[]ps= new Point [10000];
        Color[]cs= new Color[10000];
        int[] size = new int[10000];
        int count=0;
        JSpinner spin1;
     
            // Constructor
    	public myJPanelstd()
    	{
    		setBackground(Color.white);
    		setLayout(new BorderLayout());
     
    		p1 = new JPanel();
    		p1.setLayout(new GridLayout(2,4));
    		//Create Buttons
    		bc1= new JButton("RED");
    		bc2= new JButton("BLUE");
    		bc3= new JButton("GREEN");
    		be1= new JButton("ERASE");
    		bx = new JButton("x = ");
    		by = new JButton("y = ");
    		spin1=new JSpinner();
     
    		bmessage = new JButton("Move the mouse");
     
    		// Add buttons to Panel p1
    		p1.add(bc1);
    		bc1.addActionListener(this);
    		bc1.setBackground(Color.red);
    		p1.add(bc2);
    		bc2.setBackground(Color.blue);
    		p1.add(bc3);
    		bc3.setBackground(Color.green);
    		p1.add(be1);
     
    		p1.add(spin1);
     
    		add(p1,"South");
     
    		// Add Mouse Listener to Panel
            addMouseMotionListener(this);
     
    	} // end constructor
     
    	//----------------------------------------------------------------------------
    	// All 2 methods must be created, even if you are not going to use one of them
    	//----------------------------------------------------------------------------
        public void mouseMoved(MouseEvent evt)
        {
        	Point pt = evt.getPoint();		// get the mouse position
     
        	String sx = "x = " + pt.getX();	// get X position of the mouse
            bx.setText(sx);
     
        	String sy = "y = " + pt.getY();	// get X position of the mouse
            by.setText(sy);
     
            bmessage.setText("Mouse moved");
        }
     
        //-------------------------------------------------------------------
        public void mouseDragged(MouseEvent evt)
        {
        	Point pt=evt.getPoint();
        	int mx=(int) pt.getX();
        	int my =(int) pt.getY();
        	Graphics g=getGraphics();
        	g.setColor(Color.cyan);
        	g.fillOval(mx,my,width ,height);
        	ps[count]=pt;
        	cs[count]=Color.cyan;
        	size[count]=
        	count++;
        	bmessage.setText("Mouse dragged");
        }
        public void paintComponent(Graphics g)
        {
        	super.paintComponent(g);
        	for(int j=0;j< count; j++)
        	{
     
        		g.setColor(cs[j]);
        		g.fillOval((int)ps[j].getX(),(int)ps[j].getY(),5,5);
        	}
        }
     
    } // end class


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: HELP with Java Paint Program Code

    Never, never use getGraphics() of a Component. Learn to do custom painting the correct way:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

Similar Threads

  1. calling c code from java
    By sara in forum Java Native Interface
    Replies: 3
    Last Post: April 6th, 2013, 09:53 PM
  2. [SOLVED] what is wrong for the java code
    By chuikingman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2010, 02:00 AM
  3. How overloaded paint() works?
    By maikeru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2009, 06:13 PM
  4. Convert C/C++ code to Java
    By cristianll in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 14th, 2009, 02:43 AM
  5. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM