954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

JLabel not visible

Hey here's my code for functioning like MS Paint. But JLabels are just not visible. Please Help.

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;

public class MSPaint 
{
	JFrame frame;
	JPanel panel;
	Painting paint = new Painting();

	public MSPaint()
	{	
		frame = new JFrame("MS Paint");
		frame.setSize(1024,768);
		
		panel = new JPanel();
		frame.getContentPane().add(panel);
		panel.setLayout(new BorderLayout());
		panel.add(paint);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setVisible(true);
		paint.getComponent(0).requestFocus();
	}

	public void init()
	{
					
	}

	public static void main(String[] args)
	{
		MSPaint msp = new MSPaint();
	}
}

class Painting extends JPanel implements MouseListener, ActionListener, MouseMotionListener
{
	Raster r;
	int x1, y1, x2, y2;	
	boolean point = true, circle, line, rect, start = true;
	BufferedImage bf;
	JPanel pcen, psouth;
	JComboBox drawChooser, colorChooser;
	JLabel select, fillLabel, drawLabel, eraserLabel, color;
	ButtonGroup group;
	JRadioButton fill;
	JRadioButton draw;
	JRadioButton eraser;	
	
	Cursor cursor;
	BufferedImage cusrImage;
	
	Painting()
	{
		super();
		this.setLayout(new BorderLayout());
		pcen = new JPanel();
		pcen.setBackground(Color.WHITE);
		setBackground(Color.WHITE);
		add(pcen);

		psouth= new JPanel();
		psouth.setBackground(Color.LIGHT_GRAY);
		
		select = new JLabel("Select");
		select.setBackground(Color.RED);
		select.setVisible(true);
		psouth.add(select);
		
		drawChooser = new JComboBox();
		drawChooser.addItem("Point");
		drawChooser.addItem("Line");
		drawChooser.addItem("Circle");
		drawChooser.addItem("Rectangle");
		drawChooser.addActionListener(this);
		
		psouth.add(drawChooser);

		fillLabel = new JLabel("Fill");
		drawLabel = new JLabel("Draw");
		eraserLabel = new JLabel("Eraser");
		
		group = new ButtonGroup();
		
		fill = new JRadioButton();
		fill.addActionListener(this);
		
		draw = new JRadioButton();
		draw.addActionListener(this);
		
		eraser = new JRadioButton();
		eraser.addActionListener(this);
		
		draw.setSelected(true);
		group.add(draw);
		group.add(fill);
		group.add(eraser);
		psouth.add(drawLabel);
		psouth.add(draw);
		psouth.add(fillLabel);
		psouth.add(fill);
		psouth.add(eraserLabel);
		psouth.add(eraser);
		
		
		color = new JLabel("Color");
		psouth.add(color);
		
		
		colorChooser = new JComboBox();
		colorChooser.addItem("Black");
		colorChooser.addItem("Red");
		colorChooser.addItem("Green");
		colorChooser.addItem("Blue");
		colorChooser.addItem("Yellow");
		colorChooser.addItem("Cyan");
		colorChooser.addItem("Orange");
		colorChooser.addItem("Magenta");
		colorChooser.addItem("Gray");
		colorChooser.addItem("White");
		
		colorChooser.addActionListener(this);

		psouth.add(colorChooser);
		psouth.addMouseListener(this);
		this.add("South", psouth);
		
			
		pcen.addMouseListener(this);
		pcen.addMouseMotionListener(this);
		
		
		setSize(1024,768);
	
		bf = new BufferedImage(this.getWidth(),this.getHeight()-35,BufferedImage.TYPE_INT_RGB);
		
		
		cursor=new Cursor(Cursor.CROSSHAIR_CURSOR);
		pcen.setCursor(cursor);
		
		pcen.requestFocus();
		validate();
	}

	public void paint(Graphics g)
	{
		if(bf.getHeight()!=this.getHeight()-35 || bf.getWidth()!= this.getWidth())
		{
			r = bf.getData();
			bf = new 

BufferedImage(this.getWidth(),this.getHeight()-35,BufferedImage.TYPE_INT_RGB);
			bf.getGraphics().setColor(Color.WHITE);
			bf.getGraphics().fillRect(0, 0, this.getWidth(), this.getWidth());
			bf.setData(r);
		}

		draw(bf.getGraphics());
		g.drawImage(bf, 0, 0, null);
	}

	public Color getColor()
	{
		Color c = Color.BLACK;
		
		if(colorChooser.getSelectedItem()=="Black")
		{
			c= Color.BLACK;
			return c;
		}
		if(colorChooser.getSelectedItem()=="Blue")
		{
			c= Color.BLUE;
			return c;
		}
		if(colorChooser.getSelectedItem()=="Red")
		{
			c= Color.RED;
			return c;
		}
		if(colorChooser.getSelectedItem()=="Green")
		{
			c= Color.GREEN;
			return c;
		}
		if(colorChooser.getSelectedItem()=="Yellow")
		{
			c= Color.YELLOW;
			return c;
		}
		if(colorChooser.getSelectedItem()=="Cyan")
		{
			c= Color.CYAN;
			return c;
		}
		if(colorChooser.getSelectedItem()=="Orange")
		{
			c= Color.ORANGE;
			return c;
		}
		if(colorChooser.getSelectedItem()=="Magenta")
		{
			c= Color.MAGENTA;
			return c;
		}
		if(colorChooser.getSelectedItem()=="Gray")
		{
			c= Color.GRAY;
			return c;
		}
		if(colorChooser.getSelectedItem()=="White")
		{
			c= Color.WHITE;
			return c;
		}
		
		return c;
	}

	public void draw(Graphics g)
	{	
		
		if(start)
		{
			g.setClip(0,0,this.getWidth(),this.getHeight());
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, this.getWidth(), this.getWidth());
		}
		
		
		if(!point && !eraser.isSelected() && !start)	
		{
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, this.getWidth(), this.getWidth());
			bf.setData(r);	
		}
		else
		{
			
			r = bf.getData();
		}
		
		 
		start = false;
		
		g.setColor(getColor());
		
		if(draw.isSelected())
		{
			if(circle)
			{
				if(x1<x2 && y1<y2)
				{
					g.drawOval(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if(x1>x2 && y1> y2)
				{
					g.drawOval(x2, y2, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if (y1<y2)
				{
					g.drawOval(x2, y1, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if (y1>y2)
				{
					g.drawOval(x1, y2, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}	
			}
			else if(rect)
			{
				if(x1<x2 && y1<y2)
				{
					g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if(x1>x2 && y1> y2)
				{
					g.drawRect(x2, y2, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if (y1<y2)
				{
					g.drawRect(x2, y1, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if (y1>y2)
				{
					g.drawRect(x1, y2, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
			}
			else
			{
				g.drawLine(x1,y1, x2,y2 );
			}
		}
		else if(fill.isSelected())
		{
			
			if(circle)
			{
				if(x1<x2 && y1<y2)
				{
					g.fillOval(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if(x1>x2 && y1> y2)
				{
					g.fillOval(x2, y2, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if (y1<y2)
				{
					g.fillOval(x2, y1, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if (y1>y2)
				{
					g.fillOval(x1, y2, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				
				
			}
			else if(rect){
				if(x1<x2 && y1<y2)
				{
					g.fillRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if(x1>x2 && y1> y2)
				{
					g.fillRect(x2, y2, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if (y1<y2)
				{
					g.fillRect(x2, y1, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				else if (y1>y2)
				{
					g.fillRect(x1, y2, Math.abs(x2-x1), Math.abs(y2-y1));
					
				}
				
			}
			else
			{
				g.drawLine(x1,y1, x2,y2 );
			}
		}
		
		else if(eraser.isSelected())
		{
			
			g.setColor(Color.WHITE);
			g.fillRect(x2-15,y2-15,30,30);
		}
		
		if(point)
		{
			x1=x2;
			y1=y2;
		}
		
		try 
		{
			Thread.sleep(5);
		} 
		catch (InterruptedException e) 
		{
			e.printStackTrace();
		}
		
	}

	public void mouseDragged(MouseEvent me) 
	{
		x2=me.getX();
		y2= me.getY();
		paint(pcen.getGraphics());	
	}
	
	
	public void mousePressed(MouseEvent me) 
	{	
		x1=me.getX();
		y1= me.getY();
		x2=x1;
		y2=y1;
		paint(pcen.getGraphics());	
	}

	
	public void mouseReleased(MouseEvent e) 
	{	
		if(!point)
		{
			r = bf.getData();
		}
		
	}

	public void mouseEntered(MouseEvent me) 
	{
		if(me.getSource()==psouth)	
		{
			this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		}
		if(me.getSource()==pcen)
		{
			pcen.setCursor(cursor);
		}
	}

	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==drawChooser)
		{
			if(drawChooser.getSelectedItem()=="Point")
			{
				line=false;
				point=true;
				circle=false;
				rect=false;
			}
			if(drawChooser.getSelectedItem()=="Circle")
			{
				line=false;
				circle=true;
				point=false;
				rect=false;
			}
			if(drawChooser.getSelectedItem()=="Line")
			{
				line=true;
				rect=false;
				circle=false;
				point=false;
			}
			if(drawChooser.getSelectedItem()=="Rectangle")
			{
				
				rect=true;
				line=false;
				circle=false;
				point=false;
			}
		}

		if(ae.getSource()==eraser)
		{
			cusrImage = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
			cusrImage.getGraphics().drawRect(0, 0, 30, 30);
			Toolkit t = Toolkit.getDefaultToolkit();
			cursor = t.createCustomCursor(cusrImage, new Point(15,15), "mycursor");
			
		}
		
		if(ae.getSource()==fill)
		{
			cursor = new Cursor( Cursor.CROSSHAIR_CURSOR);
		}
		
		if(ae.getSource()==draw)
		{
			cursor = new Cursor( Cursor.CROSSHAIR_CURSOR);
		}
	
		pcen.requestFocus();
	}	
	
	//remaining Mouse Listener Method
	public void mouseClicked(MouseEvent e) {}
	
	public void mouseExited(MouseEvent me) {}

	public void mouseMoved(MouseEvent e) {}
	
}
guru_iyer
Light Poster
30 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Probably because you override paint rather than paintComponent. paint also handles painting of children (eg lables ina child panel!), borders etc, and should not normally be overridden. paintComponent just handles the painting of the content area, and is the one you normally override.

JamesCherrill
Posting Genius
Moderator
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Yes dat was exactly the problem. I never knew paintComponent ever existed. Now I will try to explore and learn it. Thanks a lot James!!

guru_iyer
Light Poster
30 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: