5 Replies - 397 Views - Last Post: 20 December 2013 - 03:14 PM Rate Topic: -----

#1 tinkerbell14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-February 11

Problem with Paint program

Posted 18 December 2013 - 10:21 PM

I'm working on creating a paint program similar to MS Paint. I've managed to create code to create a pen, line, rectangle, circle, and square. Although, there is an error with the mousePressed method, so I'm unable to choose the option.Is there a way to fix it or another method? In addition, does anyone have tips on creating the other shape tools(pentagon,hexagon,octagon,and stars using the "rubber band" effect)?Thanks in advance!
import java.applet.Applet;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;


public class Lab05 extends Applet
   {
	
	Rectangle red,yellow,blue,green,black,white,orange,gray,pink,custom;
	Rectangle pencil,brush,roller, spraycan, eraser,clear,canvas;
	Rectangle line,circle,oval,rectangle,triangle,square,pentagon,hexagon,octagon;
	Rectangle fivestar,sixstar,eightstar;
	int numColor, numTool, xClick, yClick,xCoord,yCoord,xPress,yPress,xRelease,yRelease;
	int oldxPress, oldyPress,xDown, yDown;
	private boolean dragging;
	private Graphics gBuffer;
	
	
	public void init()
	{
	    Graphics gBuffer;
		
		numColor = 1;
		numTool = 1;

		red = new Rectangle(5,5,60,60);
		yellow = new Rectangle (70,5,60,60);
		blue = new Rectangle(135,5,60,60);
		green = new Rectangle(200,5,60,60);
		black = new Rectangle(265,5,60,60);
		orange = new Rectangle(330,5,60,60);
		gray = new Rectangle(395,5,60,60);
		pink = new Rectangle(460,5,60,60);
		
		pencil = new Rectangle (5,90,90,60);
		brush = new Rectangle (5,155,90,60);
		roller = new Rectangle (5,220,90,60);
		spraycan = new Rectangle (5,285,90,60);
		eraser = new Rectangle (5,350,90,60);
		clear = new Rectangle (5,415,90,60);
		
		line = new Rectangle (630,5,60,60);
		circle = new Rectangle (700,5,60,60);
		oval = new Rectangle (770,5,60,60);
		rectangle = new Rectangle (840,5,60,60);
		triangle = new Rectangle (910,5,60,60);
		square = new Rectangle (980,5,60,60);
		pentagon = new Rectangle (1050,5,60,60);
		hexagon = new Rectangle (1120,5,60,60);
		octagon = new Rectangle (1190,6,60,60);
		
		canvas = new Rectangle(100,90,1000,600);
		
	}
	
	public void paint(Graphics g)
	{   
	g.setColor(Color.red);
	g.fillRect(5,5,60,60);
	g.setColor(Color.yellow);
	g.fillRect(70,5,60,60);
	g.setColor(Color.blue);
	g.fillRect(135, 5, 60,60);
	g.setColor(Color.green);
	g.fillRect(200, 5, 60,60);
	g.setColor(Color.black);
	g.fillRect(265, 5, 60,60);
	g.setColor(Color.orange);
	g.fillRect(330, 5, 60,60);
	g.setColor(Color.gray);
	g.fillRect(395, 5, 60,60);
	g.setColor(Color.pink);
	g.fillRect(460, 5, 60,60);
	
	g.setColor(Color.black);
	g.drawRect(5,90,90,60);
	g.drawString("Pencil",20,120);
	g.drawRect(5,155,90,60);
	g.drawString("Brush",20,190);
	g.drawRect(5,220,90,60);
	g.drawString("Roller",20,250);
	g.drawRect(5,285,90,60);
	g.drawString("SprayCan",20,320);
	g.drawRect(5,350,90,60);
	g.drawString("Eraser",20,380);
	g.drawRect(5,415,90,60);
	g.drawString("Clear Screen",15,440);
	
	g.drawRect(630,5,60,60);
	g.drawString("Line",635,40);
	g.drawRect(700,5,60,60);
	g.drawString("Circle",705,40);
	g.drawRect(770,5,60, 60);
	g.drawString("Oval",775,40);
	g.drawRect(840,5,60,60);
	g.drawString("Rectangle",845,40);
	g.drawRect(910, 5, 60, 60);
	g.drawString("Triangle",915, 40);
	g.drawRect(980,5,60,60);
	g.drawString("Square",985,40);
	g.drawRect(1050,5,60,60);
	g.drawString("Pentagon",1055,40);
	g.drawRect(1120, 5, 60, 60);
	g.drawString("Hexagon",1125, 40);
	g.drawRect(1190, 5, 60, 60);
	g.drawString("Octagon", 1195,40);
	
	
	g.drawRect(100,90,1000,600); //Canvas -DrawingArea

		 switch (numColor)
	        {
	            case 1 : g.setColor(Color.red);
	                     break;
	            case 2 : g.setColor(Color.yellow);
	                     break;
	            case 3 : g.setColor(Color.blue);
	                     break;
	            case 4 : g.setColor(Color.green);
	                     break;   
	            case 5 : g.setColor(Color.black);
	                    break;    
	            case 6 : g.setColor(Color.orange);
	                     break;
	            case 7 : g.setColor(Color.gray);
	                     break;
	            case 8 : g.setColor(Color.pink);
	                      break;

	        }
	
		
		switch (numTool)
	   {
			case 1:  if(dragging){
				gBuffer.setColor(Color.black);
				gBuffer.drawLine(oldxPress,oldyPress,xPress,yPress);
				oldxPress = xPress;
				oldyPress = yPress;
			}
					 break;
			case 2 : g.fillRect(xCoord, yCoord, 10, 10);
					 break;
			case 3 :g.fillOval(xCoord, yCoord, 25, 25);
					 break;
					 
			case 4: int dots = 5;
			        double spraysize = 2;
			        for(int i=0; i < dots; i++)
			        {
			        	int x= (int) (Math.random() * 5);
		                int nX = (int)(xCoord + x * spraysize);
		                int nY = (int)(yCoord + x * spraysize);
		                g.fillOval(nX, nY, 3, 3);
					    break;
			        }
			    break;   	    
			case 5: g.setColor(Color.white); 
			        g.fillOval(xCoord, yCoord, 40, 40);
			        break;
			case 6: g.setColor(Color.white);
			        g.fillRect(100, 90, 1000, 600);
			        g.setColor(Color.black);
			        g.drawRect(100,90,1000,600);
			        numTool = 0;
			        repaint();
			        break;
			case 7: if(xDown!=xPress || yDown!=yPress){
				g.setColor(Color.red);
				g.drawLine(xDown,yDown,xPress,yPress);
				if(!dragging){
					gBuffer.setColor(Color.red);
					gBuffer.drawLine(xDown, yDown, xPress, yPress);
				}
			}
			break;
			case 8: if(xDown!= xPress || yDown!= yPress){
				int radius = (int) Math.sqrt(Math.pow(xPress-xDown,2)+Math.pow(yPress -yDown, 2));
				int diameter = 2*radius;
				g.setColor(Color.red);
				g.drawOval(xDown - radius, yDown - radius,diameter,diameter);
				if(!dragging){
					gBuffer.setColor(Color.red);
					gBuffer.drawOval(xDown-radius,yDown-radius,diameter,diameter);
				}
			}
			break;
			case 9: if(xDown!= xPress || yDown!= yPress){
				int radius = (int) Math.sqrt(Math.pow(xPress-xDown,2)+Math.pow(yPress -yDown, 2));
				int diameter = 2*radius;
				g.setColor(Color.red);
				g.drawOval(xDown - radius, yDown - radius,diameter,radius);
				if(!dragging){
					gBuffer.setColor(Color.red);
					gBuffer.drawOval(xDown-radius,yDown-radius,diameter,radius);
				}
			}
			break;
			case 10: if(xDown!= xPress&& yDown!= yPress){
				int x= Math.min(xDown, xPress);
				int y= Math.min(yDown, yPress);
				int width= Math.abs(xDown-xPress);
				int height= Math.abs(yDown-yPress);
				g.setColor(Color.red);
				g.drawRect(x, y, width, height);
				if(!dragging){
					gBuffer.setColor(Color.red);
					gBuffer.drawRect(x, y, width, height);
				}
			}
			break;
			case 12: if(xDown!= xPress&& yDown!= yPress){
				int x= Math.min(xDown, xPress);
				int y= Math.min(yDown, yPress);
				int width= Math.abs(xDown-xPress);
				int height= Math.abs(yDown-yPress);
				g.setColor(Color.red);
				g.drawRect(x, y, width, height);
				if(!dragging){
					gBuffer.setColor(Color.red);
					gBuffer.drawRect(x, y, width, height);
				}
			}
			break;
	} 
	}
	public boolean mouseDown(Event e, int x, int y)
	{

		if(red.inside(x,y))
		{
			numColor = 1;
		}
		if(yellow.inside(x,y))
		{
			numColor = 2;
		}
		if(blue.inside(x,y))
		{ 
			numColor = 3;
		}
		if(green.inside(x,y))
		{
			numColor = 4;
		}
		if(black.inside(x,y))
		{
			numColor = 5;
		}
		if(orange.inside(x,y))
		{
			numColor = 6;
		}
		if(gray.inside(x,y))
		{
			numColor = 7;
		}
		if(pink.inside(x,y))
		{
			numColor = 8;
		}
		
	
		if(pencil.inside(x,y))
		{
			numTool=1;
		}
		if(brush.inside(x,y))
		{
			numTool=2;
		}
		if(roller.inside(x,y))
		{
			numTool=3;
		}
		if(spraycan.inside(x,y))
		{
			numTool=4;
		}
		if(eraser.inside(x,y))
		{
			numTool=5;
		}
		if(clear.inside(x,y))
		{
			numTool=6;
		}
		if(line.inside(x,y))
		{
			numTool=7;
		}
		if(circle.inside(x,y))
		{
			numTool=8;
		}
		if(oval.inside(x,y))
		{
			numTool=9;
		}
		if(rectangle.inside(x,y))
		{
			numTool=10;
		}
		if(triangle.inside(x,y))
		{
			numTool=11;
		}
		if(square.inside(x,y))
		{
			numTool=12;
		}
		if(pentagon.inside(x,y))
		{
			numTool=13;
		}
		if(hexagon.inside(x,y))
		{
			numTool=14;
		}
		if(octagon.inside(x,y))
		{
			numTool=15;
		}
		if(fivestar.inside(x,y))
		{
			numTool=16;
		}
		if(sixstar.inside(x,y))
		{
			numTool=17;
		}
		if(eightstar.inside(x,y))
		{
			numTool=18;
		}
		if(canvas.contains(x,y))
		{
		xClick = x;
		yClick = y;	
		repaint();
		}
		return true;
	}

	
	public boolean mouseDrag(Event e, int x, int y)
	  {

		if(canvas.contains(x,y))
		{
		xCoord = x;
		yCoord = y;
		repaint();
		}
		dragging=true;
		return true;
	  }
	public boolean mousePressed (Event e,int x,int y)
	{
		if(canvas.contains(x,y))
		{
		if(dragging) 
		if (e.isMetaDown()) {
			numTool++;
		if(numTool>3)
		 numTool=0;
		}
		oldxPress = xPress = xDown = e.getX();
		oldyPress = yPress = yDown = e.getY();
		dragging = true;
		repaint();
		return true;
		}
		
	}
	
	  public void update(Graphics g)
	  {
		  paint(g);
	  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Paint program

#2 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Problem with Paint program

Posted 18 December 2013 - 11:31 PM

The error is?
No offence but no one will read through all that code to find the error, narrow it down a bit and you will get the answers you seek.....
Was This Post Helpful? 0
  • +
  • -

#3 tinkerbell14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-February 11

Re: Problem with Paint program

Posted 19 December 2013 - 10:13 AM

View Postfloppyspace, on 18 December 2013 - 11:31 PM, said:

The error is?
No offence but no one will read through all that code to find the error, narrow it down a bit and you will get the answers you seek.....


There was no specific error showing, but there is something wrong in the mousePressed method when using MetaDown and getX and getY.
The program runs, but when it comes to clicking on the options for pen, line,circle, and rectangle nothing appears on screen to draw. That's the best I can describe it.
Was This Post Helpful? 0
  • +
  • -

#4 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Problem with Paint program

Posted 19 December 2013 - 08:26 PM

ok, took a moment to check this.

you need a mouseListener

public class Lab05 extends Applet implements MouseListener


The book you must be using is old as inside(x,y) works but is depreciated fyi,are you using an ide?

I ran the program with the mouseListener and it worked (although a few other bits for you to fix) ; )

This post has been edited by floppyspace: 19 December 2013 - 08:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 tinkerbell14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-February 11

Re: Problem with Paint program

Posted 19 December 2013 - 10:53 PM

View Postfloppyspace, on 19 December 2013 - 08:26 PM, said:

ok, took a moment to check this.

you need a mouseListener

public class Lab05 extends Applet implements MouseListener


The book you must be using is old as inside(x,y) works but is depreciated fyi,are you using an ide?

I ran the program with the mouseListener and it worked (although a few other bits for you to fix) ; )


Thanks floppyspace! I actually started using MouseListener while working on it today, besides implementing it, are there other interfaces or libraries that should be imported for this to work? Thanks again for taking the time!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10444
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: Problem with Paint program

Posted 20 December 2013 - 03:14 PM

Quote

Rectangle red,yellow,blue,green,black,white,orange,gray,pink,custom;


I would suggest taking a look at arrays and loops. When you want to iterate through the Rectangles for colors, you don't want if statements to check each one. A loop is a few lines, and you don't have to change the code if you decide to add a dozen colors later. Whereas with your current design, you would have to add a dozen more if statements.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1