5 Replies - 644 Views - Last Post: 14 February 2015 - 06:47 PM Rate Topic: -----

#1 shortbus  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 235
  • Joined: 26-March 13

how to make paint program not error out

Posted 14 October 2013 - 05:55 PM

i am screwing around with mouse listeners so i decided to make a basic paint program.
it works fine but when it draws more than 100 circles it errors out. i know why its doing it (its because i set the array to 100)
but my question is how to make it so it has not limit in the array.
here is the code
public class PaintProject extends Applet {
int numClicks = 100;
int numCicles;	
int xCoord[];
int yCoord[];
boolean paint;
public void init()  {  
        setSize(400,300);
        numCicles = 0;    
        xCoord = new int[100 + numCicles];
            yCoord = new int[100 + numCicles];
          
            paint = true;
}

	public void paint(Graphics g)
	{
            // for later to make it all nice.
            //g.drawString()
         for(int a = 0; a < numCicles; a++){       
          g.fillOval(xCoord[a], yCoord[a], 10, 10);
         }
         }
        public boolean mouseDrag(Event e, int x, int y)
	{
		xCoord[numCicles] = x;
		yCoord[numCicles] = y;
		numClicks++;
                repaint();
		return true;
	}
	
 


   
}


any help is appreciated

This post has been edited by shortbus: 14 October 2013 - 06:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how to make paint program not error out

#2 Elemental_1_1  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 114
  • Joined: 22-January 09

Re: how to make paint program not error out

Posted 14 October 2013 - 06:32 PM

What about using ArrayList<Dimension> instead of two different arrays for x and y?
Was This Post Helpful? 1
  • +
  • -

#3 shortbus  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 235
  • Joined: 26-March 13

Re: how to make paint program not error out

Posted 14 October 2013 - 06:36 PM

i do not fully understand arrayList yet so i just used a static array since its all i know atm ( im only in comp sci AP at the moment)
Was This Post Helpful? 0
  • +
  • -

#4 Elemental_1_1  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 114
  • Joined: 22-January 09

Re: how to make paint program not error out

Posted 14 October 2013 - 06:39 PM

It's just you can't have a dynamically sized array; you need to use ArrayList, otherwise you will have an upper limit on the number of circles
Was This Post Helpful? 1
  • +
  • -

#5 Charles_Dodson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 5
  • Joined: 14-February 15

Re: how to make paint program not error out

Posted 14 February 2015 - 06:38 PM

You don't necessarily have to use arrays when making simple paint programs. I personally found them really frustrating to use when i first started programming in java. Here is a quick paint program I whipped up for you. :)/>

 import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class SimplePaint extends Applet
   implements MouseMotionListener { //DECLARE YOUR FIELDS HERE 

   int width, height;
   Image canvas;
   Graphics g;

   public void init() { //DEFINE YOUR FIELDS HERE
       width = getSize().width;
      height = getSize().height;


      canvas = createImage(width*5,height*5);
      g = canvas.getGraphics();
      g.setColor(Color.white);
      g.fillRect(0,0,width*5,height*5);
      g.setColor(Color.blue );

      addMouseMotionListener(this);
   }
//BELOW ARE ALL THE MOUSE EVENTS
   public void mouseMoved(MouseEvent e) {}
   public void mouseDragged(MouseEvent e) {
      int x = e.getX();
      int y = e.getY();
      g.fillOval(x-20,y-20,40,40);
      repaint();
      
   }

   public void update( Graphics g) {
      g.drawImage(canvas,0,0,this);
   }

   public void paint(Graphics g) { //DRAW STUFF HERE
      
   }
}



You can fool around with this too. Hope this helped.
HAPPY CODING
Was This Post Helpful? 1
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: how to make paint program not error out

Posted 14 February 2015 - 06:47 PM

ArrayLists are totally easy. Here is a quick intro that covers what you'll need 95% of the time:

import java.util.*;

public class ArrayListDemo {
	public static void main(String[] args) {
		
		// First we make an array list.
		List<String> myList = new ArrayList<>();
		
		// Then we add some stuff to it
		myList.add("Hello, world!");
		myList.add("OK Computer");
		myList.add("Where do you want to go today?");

		// Then we iterate over the list.
		for(String s : myList) {
			System.out.println(s);
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1