Jump to content
  • Advertisement
Sign in to follow this  
Ueneth Echil

JAVA: A way to get part of the paint method to run once and independantly!?

This topic is 4316 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a Paint program, and we've decided to have a "coloring book" option. Where you can choose a few black and white images, and color, between the lines, just like in the old days. Our code is this: CiPaint.java
// CiPaint
// Version 0.90
// Programmed and Designed by Paul Adamski & Austin Nork

// This program is inflexible and exhibits poor design
// ZOMG!!

// TODO:	
 
import java.awt.*;
import java.applet.*;

public class CiPaint extends Applet
{
	Rectangle red, green, blue, purple, black, white, yellow;
	Rectangle two, four, six, eight, ten;
	Rectangle oval, square, roller;
	Rectangle clear, fill;
	
	boolean boolClear, boolFill;
	Color backgroundColor, eraseColor, currentColor;
	int numColor, numSize, numType;
	
	Image virtualMem;
	Graphics gBuffer;
	int oldX, oldY, newX, newY;
	int appletWidth;		
	int appletHeight;
	
	Image img;
	
	boolean imagee;
		
	public void init() 
	{				
		backgroundColor = Color.white;
		eraseColor = Color.black;
	
		red = new Rectangle(10,30,25,25);
		green = new Rectangle(40,30,25,25);
		blue = new Rectangle(70,30,25,25);
		purple = new Rectangle(100,30,25,25);
		yellow = new Rectangle(130,30,25,25);
		black = new Rectangle(160,30,25,25);
		white = new Rectangle(85,60,25,25);
		
		two = new Rectangle(10,120,25,25);
		four = new Rectangle(40,120,25,25);
		six = new Rectangle(70,120,25,25);
		eight = new Rectangle(100,120,25,25);
		ten = new Rectangle(130,120,25,25);
		
		oval = new Rectangle(40,175,25,25);
		square = new Rectangle(10,175,25,25);
		roller = new Rectangle(70,175,50,25);
		
		clear = new Rectangle(10,235,35,25);
		fill = new Rectangle(50,230,73,25);;
		
		numColor = 0;
		numSize = 0;
		numType = 0;
		boolClear = false;
		boolFill = false;
		imagee = false;
		
		int x = 400;
		int y = 400;
		
		img = getImage(getDocumentBase(), getParameter("img"));
		
		appletWidth = getWidth();
		appletHeight = getHeight();	
		virtualMem = createImage(appletWidth,appletHeight);
		gBuffer = virtualMem.getGraphics();
		gBuffer.fillRect(200,0,appletWidth-200,appletHeight);
	}

	public void paint(Graphics g) 
	{		
		// Draws the borders and UI	
		g.drawLine(200,0,200,600);
		g.drawLine(1,1,800,1);
		g.drawLine(800,1,800,600);
		g.drawLine(800,600,1,600);
		g.drawLine(1,600,1,1);
		
		g.drawString("Color",10,20);
		
		g.setColor(Color.red);
		g.fillRect(10,30,25,25);
		
		g.setColor(Color.green);
		g.fillRect(40,30,25,25);
		
		g.setColor(Color.blue);
		g.fillRect(70,30,25,25);
		
		g.setColor(Color.magenta);
		g.fillRect(100,30,25,25);
		
		g.setColor(Color.yellow);
		g.fillRect(130,30,25,25);
		
		g.setColor(Color.black);
		g.fillRect(160,30,25,25);
		
		g.drawRect(85,60,25,25);
		
		g.drawString("Size",10,110);
		
		g.drawString("2",20,136);
		g.drawRect(10,120,25,25);
		
		g.drawString("4",50,136);
		g.drawRect(40,120,25,25);
		
		g.drawString("6",80,136);
		g.drawRect(70,120,25,25);
		
		g.drawString("8",110,136);
		g.drawRect(100,120,25,25);
		
		g.drawString("10",135,136);
		g.drawRect(130,120,25,25);
		
		g.drawString("Brush Type",10,165);
		
		g.drawRect(10,175,25,25);
		
		g.drawOval(40,175,25,25);
		
		g.drawRect(70,175,50,25);
		
		g.drawString("Miscellaneous",10,220);
		
		g.drawString("Clear",13,247);
		g.drawRect(10,230,35,25);
		
		g.drawString("Fill Window",55,247);
		g.drawRect(50,230,73,25);
		// End Drawing
				
		g.setColor(currentColor);
				
		if (oldX > 200 && oldY > 1 && oldY < 600 && oldX < 795)
		{
			if (numType == 0)
			{
				if (numSize == 0)
					g.fillOval(oldX,oldY,2,2);
				if (numSize == 1)
					g.fillOval(oldX,oldY,4,4);
				if (numSize == 2)
					g.fillOval(oldX,oldY,6,6);
				if (numSize == 3)
					g.fillOval(oldX,oldY,8,8);
				if (numSize == 4)
					g.fillOval(oldX,oldY,10,10);
			}
				
			if (numType == 1)
			{
				if (numSize == 0)
					g.fillRect(oldX,oldY,2,2);
				if (numSize == 1)
					g.fillRect(oldX,oldY,4,4);
				if (numSize == 2)
					g.fillRect(oldX,oldY,6,6);
				if (numSize == 3)
					g.fillRect(oldX,oldY,8,8);
				if (numSize == 4)
					g.fillRect(oldX,oldY,10,10);
			}
			
			if (numType == 2)
			{
				if (numSize == 0)
					g.fillRect(oldX,oldY,6,1);
				if (numSize == 1)
					g.fillRect(oldX,oldY,12,2);
				if (numSize == 2)
					g.fillRect(oldX,oldY,18,3);
				if (numSize == 3)
					g.fillRect(oldX,oldY,24,4);
				if (numSize == 4)
					g.fillRect(oldX,oldY,30,5);
			}
		}
		
		if (boolClear == true)
		{
			g.setColor(Color.white);
			g.fillRect(201,2,599,598);
			boolClear = false;
		}
		
		if (boolFill == true)
		{
			g.fillRect(201,2,599,598);
			boolFill = false;
		}
		
		g.setColor(Color.white);
		g.fillRect(801,0,25,601);
		g.fillRect(0,601,801,25);
	}
	
	public boolean mouseDown(Event e, int x, int y)
	{
		if (red.inside(x,y))
			numColor = 1;
		else if (green.inside(x,y))
			numColor = 2;
		else if (blue.inside(x,y))
			numColor = 3;
		else if (purple.inside(x,y))
			numColor = 4;
		else if (yellow.inside(x,y))
			numColor = 5;
		else if (black.inside(x,y))
			numColor = 6;
		else if (white.inside(x,y))
			numColor = 7;
		else
			numColor = 8;
			
		if (two.inside(x,y))
			numSize = 0;
		if (four.inside(x,y))
			numSize = 1;
		if (six.inside(x,y))
			numSize = 2;
		if (eight.inside(x,y))
			numSize = 3;
		if (ten.inside(x,y))
			numSize = 4;
			
		if (oval.inside(x,y))
			numType = 0;
		if (square.inside(x,y))
			numType = 1;
		if (roller.inside(x,y))
			numType = 2;
						
		if (clear.inside(x,y))
			boolClear = true;
		if (fill.inside(x,y))
			boolFill = true;
			
		newX = x;
		newY = y;
		oldX = newX;
		oldY = newY;
		
		switch (numColor)
		{
			case 1:
				currentColor = Color.red; 
				break;
			case 2:
				currentColor = Color.green; 
				break;
			case 3:
				currentColor = Color.blue; 
				break;
			case 4:
				currentColor = Color.magenta;
				break;
			case 5:
				currentColor = Color.yellow; 
				break;
			case 6:
				currentColor = Color.black; 
				break;
			case 7:
				currentColor = Color.white; 
				break;
			case 8:
				break;
		}
			
		repaint();
		
		return true;
	}
	
	public boolean mouseDrag(Event e, int x, int y)
	{
		newX = x;
		newY = y;
		oldX = newX;
		oldY = newY;
		
		repaint();
		
		return true;
	}
	
	public void update(Graphics g)
	{
		paint(g);
	}
}

CiPaint.htm
<HTML>
<HEAD>
</HEAD>
<BODY BGCOLOR="000000">
<CENTER>
<APPLET
	code	= "CiPaint.class"
	width	= "801"
	height	= "601"
	>

	<param name="img" value="testest.gif">
</APPLET>
</CENTER>
</BODY>
</HTML>

Currently, the image will be drawn repeaditly and so you cannot draw over it. What we need is the image to be drawn once and independatly, like on another layer. So if you mess up, it is possible to normally erase your markings, but not the image. Thanks in advance, Adamski

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!