www.webdeveloper.com
Results 1 to 2 of 2

Thread: Paint Application that doesn't paint, problems with mouseEvents and StateChanged

  1. #1
    Join Date
    Feb 2012
    Posts
    7

    Paint Application that doesn't paint, problems with mouseEvents and StateChanged

    Hi, I'm writing a paint application for my java class. I've gotten the JFrame to look the way it's supposed to, and everything looks in order, but I can't draw. Something's wrong but I can't tell what it is. Can anyone help?
    Code:
    import javax.swing.*;
    import javax.swing.border.BevelBorder;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Hashtable;
    
    public class PaintApp extends JFrame implements ActionListener, ChangeListener, MouseMotionListener, MouseListener
    {      JLabel textprompt = new JLabel("Enter text, then click location:");
    	JTextField textinput = new JTextField("", 25);
    
    	ImageIcon eraserIcon = new ImageIcon("eraser.jpg");
    	JButton eraser = new JButton(eraserIcon);
    
    	ImageIcon pencilIcon = new ImageIcon("pencil.jpg");
    	JButton pencil = new JButton(pencilIcon);
    	
    	ImageIcon brushIcon = new ImageIcon("brush.jpg");
    	JButton brush = new JButton(brushIcon);
    	
    	ImageIcon textIcon = new ImageIcon("text.jpg");
    	JButton text = new JButton (textIcon);
    	
    	JLabel stroke = new JLabel("Stroke:");
    	JSlider sliderbar = new JSlider(JSlider.VERTICAL, 0,3,3);
    
    	ImageIcon redIcon = new ImageIcon("red.gif");
    	JButton redCrayon = new JButton (redIcon);
    
    	ImageIcon yellowIcon = new ImageIcon("yellow.gif");
    	JButton yellowCrayon = new JButton (yellowIcon);
    	
    	ImageIcon greenIcon = new ImageIcon("green.gif");
    	JButton greenCrayon = new JButton (greenIcon);
    	
    	ImageIcon blueIcon = new ImageIcon("blue.gif");
    	JButton blueCrayon = new JButton (blueIcon);
    	
    	JPanel topPanel = new JPanel(new FlowLayout());
    	JPanel centerPanel = new JPanel(new FlowLayout());
    	JPanel sidePanel = new JPanel(new GridLayout(6,1));
    	JPanel bottomPanel = new JPanel(new GridLayout(2,2));
    
    	boolean eraserOn = false;
    	boolean pencilOn = false; 
    	boolean brushOn = false;
    	boolean textOn = false; 
    	int last_x=-10;
    	int last_y=-10;
    	int xpos=-10;
    	int ypos=-10;
    	Color drawColor = (Color.RED);
    	Font font1 = new Font("Arial", Font.PLAIN, 20);
    	int value;
    	JMenuBar menuBar = new JMenuBar();
    	JMenu file = new JMenu("File");
    	JMenuItem newopt = new JMenuItem("New");
    	JMenuItem exit = new JMenuItem("Exit");
    	JMenu help = new JMenu("Help");
    	JMenuItem about = new JMenuItem("About");
    	JLabel createdby = new JLabel("Created by LV");
    	float lineWeight;	
    	BasicStroke str = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); 
    	Container con = null;
    
    	public PaintApp()
    	{
    	    super("My Paint Program");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    		
    	    con = this.getContentPane(); 
    	    con.setLayout(new BorderLayout()); 
    		
    	    setJMenuBar(menuBar);
    	    menuBar.add(file);
    	    menuBar.add(help);
    	    file.add(newopt);
    	    file.add(exit);
    	    help.add(about);
    	    newopt.addActionListener(this);
    	    exit.addActionListener(this);
    	    about.addActionListener(this);
    	       
    		topPanel.add(textprompt);
    		textprompt.setEnabled(false);
    		topPanel.add(textinput);
    		textinput.setEditable(false);
    
    		centerPanel.setBackground(Color.white);
    		centerPanel.add(createdby);
    	        createdby.setVisible(false);
    
    		sidePanel.add(eraser);
    		eraser.setBorder(new BevelBorder(BevelBorder.RAISED));
    		eraser.addActionListener(this);
    
    		sidePanel.add(pencil);
    		pencil.setBorder(new BevelBorder(BevelBorder.LOWERED));
    		pencil.addActionListener(this);
    		
    		sidePanel.add(brush);
    		brush.setBorder(new BevelBorder(BevelBorder.RAISED));
    		brush.addActionListener(this);
    		
    		sidePanel.add(text);
    		sidePanel.setBackground(Color.WHITE);
    		text.setBorder(new BevelBorder(BevelBorder.RAISED));
    		text.addActionListener(this);
    		
    		sidePanel.add(stroke);
    		
    		sidePanel.add(sliderbar);
    	    sliderbar.setMinorTickSpacing(1);
    	    sliderbar.setMajorTickSpacing(1);
    	    sliderbar.setPaintTicks(true);
    	    sliderbar.setPaintLabels(true);
    	    sliderbar.setSnapToTicks(true);
    	    
    	    Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
    	    labelTable.put(new Integer( 0 ), new JLabel("Thin") );
    	    labelTable.put(new Integer(3), new JLabel("Thick") );
    	    sliderbar.setLabelTable( labelTable );
    	    sliderbar.addChangeListener(this);
    		
    		bottomPanel.add(redCrayon);
    		redCrayon.setBorder(new BevelBorder(BevelBorder.LOWERED));
    		redCrayon.addActionListener(this);
    		
    		bottomPanel.add(yellowCrayon);
    		yellowCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    		yellowCrayon.addActionListener(this);
    		
    		bottomPanel.add(greenCrayon);
    		greenCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    		greenCrayon.addActionListener(this);
    		
    		bottomPanel.add(blueCrayon);
    		blueCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    		blueCrayon.addActionListener(this);
    		
    		con.add(topPanel, BorderLayout.NORTH);
    		con.add(centerPanel, BorderLayout.CENTER);
    		con.add(sidePanel, BorderLayout.WEST);
    		con.add(bottomPanel, BorderLayout.SOUTH);
    	}
    	public void actionPerformed(ActionEvent e) 
    	{      Object source = e.getSource();
                    if (source == exit)
    		{
    			System.exit(0);
    		}
    		if (source == newopt)
    		{centerPanel.repaint();
    		}
    		if (source == about)
    		{createdby.setVisible(true);
    		}
    		if (source == eraser)
    		{      eraser.setBorder(new BevelBorder(BevelBorder.LOWERED));
    			pencil.setBorder(new BevelBorder(BevelBorder.RAISED));
    			brush.setBorder(new BevelBorder(BevelBorder.RAISED));
    			text.setBorder(new BevelBorder(BevelBorder.RAISED));
    			drawColor = (Color.white);
    			eraserOn = true;
    		}
    		if (source == pencil)
    		{
    			eraser.setBorder(new BevelBorder(BevelBorder.RAISED));
    			pencil.setBorder(new BevelBorder(BevelBorder.LOWERED));
    			brush.setBorder(new BevelBorder(BevelBorder.RAISED));
    			text.setBorder(new BevelBorder(BevelBorder.RAISED));
    			drawColor = (Color.red);
    			pencilOn = true;
    		}
    		if (source == brush)
    		{      eraser.setBorder(new BevelBorder(BevelBorder.RAISED));
    			pencil.setBorder(new BevelBorder(BevelBorder.RAISED));
    			brush.setBorder(new BevelBorder(BevelBorder.LOWERED));
    			text.setBorder(new BevelBorder(BevelBorder.RAISED));
    			drawColor = (Color.red);
    			brushOn = true;
    		}
    		if (source == text)
    		{	eraser.setBorder(new BevelBorder(BevelBorder.RAISED));
    			pencil.setBorder(new BevelBorder(BevelBorder.RAISED));
    			brush.setBorder(new BevelBorder(BevelBorder.RAISED));
    			text.setBorder(new BevelBorder(BevelBorder.LOWERED));
    			drawColor = (Color.red);
    			textprompt.setEnabled(true);
    			textinput.setEditable(true);
    			textOn = true;	
    		}
    		
    		if (source == redCrayon)
    		{	redCrayon.setBorder(new BevelBorder(BevelBorder.LOWERED));
    			yellowCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			greenCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			blueCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			drawColor = (Color.red);
    		}
    		
    		if (source == yellowCrayon)
    		{      redCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			yellowCrayon.setBorder(new BevelBorder(BevelBorder.LOWERED));
    			greenCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			blueCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			drawColor = (Color.yellow);
    		}
    		
    		if (source == greenCrayon)
    		{      redCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			yellowCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			greenCrayon.setBorder(new BevelBorder(BevelBorder.LOWERED));
    			blueCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			drawColor = (Color.green);
    		}
    		
    		if (source == blueCrayon)
    		{
    			redCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			yellowCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			greenCrayon.setBorder(new BevelBorder(BevelBorder.RAISED));
    			blueCrayon.setBorder(new BevelBorder(BevelBorder.LOWERED));
    			drawColor = (Color.blue);
    		}
    	}
    	
    	public void mousePressed(MouseEvent e) {
    		last_x = e.getX();
    		last_y = e.getY();
    		Graphics gr = getGraphics();
    		if(textOn == true)
    		{
    			String s = textinput.getText();
    			gr.setColor(drawColor);
    			gr.setFont(font1);
    			gr.drawString(s, last_x, last_y);
    		}
    	}
    	
    	public void mouseDragged(MouseEvent e) 
    	{
    	    xpos=e.getX();
    	    ypos=e.getY();
    	    Graphics gr = getGraphics();
    	    gr.setColor(drawColor);
    	    gr.drawLine(last_x,last_y,xpos,ypos);
      
    	    Graphics2D gr2d = (Graphics2D)gr;
    	    BasicStroke drawLine = new BasicStroke(lineWeight, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    	    gr2d.setStroke(drawLine);
    	    if (eraserOn == true)
    	    {
    	    	gr2d.drawLine(last_x, last_y, xpos, ypos);
    	    }
    	    if (brushOn == true)
    	    {
    	    	gr2d.drawLine(last_x, last_y, xpos, ypos);
    	    }
    		gr2d.setStroke(str);
    		
    		last_x = xpos;
    		last_y = ypos;
    		
    		topPanel.repaint();
    		menuBar.repaint();
    		eraser.repaint();
    		pencil.repaint();
    		brush.repaint();
    		text.repaint();
    		stroke.repaint();
    		sliderbar.repaint();
    		redCrayon.repaint();
    		yellowCrayon.repaint();
    		greenCrayon.repaint();
    		blueCrayon.repaint();
    	}
    
    	public void mouseMoved(MouseEvent e){}
    	public void mouseClicked(MouseEvent e){}
    	public void mouseReleased(MouseEvent e){}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e){}
    
    	public void stateChanged(ChangeEvent e) 
    	{
    		JSlider source = (JSlider)e.getSource();
    		
    		value = source.getValue();
    		
    			if (value==1)
    			{
    				lineWeight = 5;
    			}
    			
    			if (value==2)
    			{
    				lineWeight = 10;
    			}
    			
    			if (value==3)
    			{
    				lineWeight = 20;
    			}
    	}
    	public static void main(String[] args) 
    	{
    		PaintApp aFrame = new PaintApp();
    		Image img = Toolkit.getDefaultToolkit().getImage("IconImage.gif");
    		aFrame.setIconImage(img);
    		aFrame.setSize(750,550);
    		aFrame.setResizable(false);
    		aFrame.setVisible(true);
    		aFrame.setLocationRelativeTo(null);
    	}
    }

  2. #2
    Join Date
    Feb 2012
    Posts
    7
    I just now realized that I'm missing the mouse listeners in my constructor method. I added

    centerPanel.addMouseListener(this);
    centerPanel.addMouseMotionListener(this);

    and now it works fine!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles