Results 1 to 7 of 7
  1. #1
    KilKidd is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default Java Paint Program problem (JPanel)

    Okay, so, I am making a java Paint like program and I want to draw in a panel. The problem is, when I draw, the lines go outside of the panel, and i don't want this. Any help would be appreciated

    here is the GUI
    Java Code:
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    
    import javax.swing.JButton;
    import javax.swing.JColorChooser;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    public class GUI extends JFrame implements ChangeListener {
    
    	private JButton pen;
    	private JButton brush;
    	private JButton image;
    	private JButton animator;
    	private JButton change_Color;
    	
    	private JMenuBar menubar;
    	
    	private JMenu file;
    	private JMenu edit;
    	
    	private JColorChooser palette;
    	
    	private JPanel drawing_Area;
    	
    	private Color current_Color = Color.BLACK;
    	
    	private int X_Value;
    	private int Y_Value;
    	
    	private Boolean is_Pen = true;
    	private Boolean is_Brush = false;
    	
    	
    	public GUI(){
            setTitle ("Artist");
            setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
            getContentPane().setLayout(null ); 
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100,100,300,300);
            setVisible(true);
            
            drawing_Area = new JPanel();
            drawing_Area.setSize (800,600);
            drawing_Area.setVisible(true);
            drawing_Area.setBackground(Color.WHITE);
    		drawing_Area.addMouseMotionListener(new MouseMotionAdapter(){
    		            public void mouseDragged( MouseEvent event ){
    		               X_Value = event.getX();
    		               Y_Value = event.getY();
    		               repaint(X_Value,Y_Value,10,10);
    		            } // end mouseDragged
    		         }); // end addMouseMotionListener
    		drawing_Area.setBounds(0,100,900,600);
    		getContentPane().add(drawing_Area);
    		
            // adds a menu bar, and menus (doesn't work, should later)
            menubar = new JMenuBar();
            setJMenuBar(menubar);
            
            JMenu fileMenu = new JMenu("File");
            JMenu editMenu = new JMenu("Edit");
            menubar.add(fileMenu);
            menubar.add(editMenu);
            
            JMenuItem newAction = new JMenuItem("New");
            JMenuItem openAction = new JMenuItem("Open");
            JMenuItem exitAction = new JMenuItem("Exit");
            exitAction.addActionListener(new ActionListener(){
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				System.exit(0);
    				
    			}
            });
            
            JMenuItem cutAction = new JMenuItem("Cut");
            JMenuItem copyAction = new JMenuItem("Copy");
            JMenuItem pasteAction = new JMenuItem("Paste");
            
            fileMenu.add(newAction);
            fileMenu.add(openAction);
            fileMenu.add(exitAction);
            editMenu.add(cutAction);
            editMenu.add(copyAction);
            editMenu.add(pasteAction);
    
    
            
            // sets the pen button
            pen = new JButton("Pen");
            pen.addActionListener(new ActionListener(){
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				is_Pen = true;
    				is_Brush= false;
    			} // end of (ActionPerformed(pen))
            }); // end of (addActionListener)
            pen.setBounds(20,20, 20, 20);
    		getContentPane().add(pen);
    		
    		// sets the brush button
    		brush = new JButton("Brush");
    		brush.addActionListener(new ActionListener(){
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				is_Pen = false;
    				is_Brush= true;
    			} // end of (ActionPerformed(pen))
            }); // end of (addActionListener)
    		getContentPane().add(brush);
    		
    		// image chooser
    		image= new JButton("Image");
    		getContentPane().add(image);
    		
    		// animator
    		animator= new JButton("Animator");
    		getContentPane().add(animator);
    		
    		// sets the color chooser
    		change_Color = new JButton("Change Color");
    		change_Color.addActionListener(new ActionListener(){
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				  current_Color = JColorChooser.showDialog( GUI.this, "Choose Color", current_Color );
    			} // end of (ActionPerformed(pen))
            }); // end of (addActionListener)
    		getContentPane().add(change_Color);
    
    		
    		show();
    	}
    	
    	public void paint(Graphics g){
    		if((X_Value >0) && (Y_Value < 600)&&(X_Value <900) && (Y_Value > 100)){
    		g.setColor(current_Color);
    		if(is_Pen){
    			g.fillOval( X_Value, Y_Value, 1, 1 ); 
    		}
    		
    		if( is_Brush){
    		 g.fillOval( X_Value, Y_Value, 10, 10 ); 
    		}
    		}
    	} // end of function (paint)
    
    	@Override
    	public void stateChanged(ChangeEvent arg0) {
    		 Color newColor = palette.getColor();
    		 setForeground(newColor);
    	}
    }
    and here is the actual app
    Java Code:
    import java.awt.Frame;
    
    public class Artist {
    	public static void main(String[] args){
    		Frame f;
    		f = new GUI();
    	} // end main
    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    JPanel? paint method? so who wrote your code?

  3. #3
    KilKidd is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    I wrote my code.

  4. #4
    KilKidd is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Also, the drawing is a bit laggy, is there anyway to optimize it without a timer function?

    and lastly, when the JColorChooser pops up, it removes all color behind it, is there a way to prevent that?

  5. #5
    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 KilKidd View Post
    The problem is, when I draw, the lines go outside of the panel, and i don't want this.
    What do you mean "go outside of the panel"? Can you elaborate exactly how your program is misbehaving?

    I see that you're drawing directly in the JFrame's paint method. This suggests that you're trying to do Swing graphics without first reading a tutorial on this, which will lead to grief, side effects, and lag. I urge you to go through the graphics tutorials first before writing this where you'll learn how to draw in a JPanel's paintCompoment method amongst other important lessons. Luck!

  6. #6
    KilKidd is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    What i mean is that i want a hard-programmed border so that if i drag the mouse off of the panel, it stops drawing, so that it wont spill over into the frame.

  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 KilKidd View Post
    What i mean is that i want a hard-programmed border so that if i drag the mouse off of the panel, it stops drawing, so that it wont spill over into the frame.
    Again this is because you're drawing in a JFrame's paint method not the JPanel's paintComponent method, and again, the tutorials will show you the correct way to do this.

Similar Threads

  1. Paint Program Help
    By ngiannini in forum AWT / Swing
    Replies: 12
    Last Post: 05-10-2010, 04:24 PM
  2. Simple Paint program question
    By StressaJune in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 08:46 PM
  3. paint vs paintComponent for a JPanel
    By lightstream in forum AWT / Swing
    Replies: 4
    Last Post: 01-29-2009, 02:26 AM
  4. Replies: 3
    Last Post: 12-31-2008, 05:57 AM
  5. help?! paint on top of swing components on JPanel
    By beam2008 in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2008, 04:15 AM

Tags for this Thread

Posting Permissions

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