Some Help Getting Started

Hey,

I'm using JCreator, and am trying to use a simple program to draw 2D boxes where you click and drag on the screen, and I want to make each of the boxes a "constructable" object. Coming from BASIC (DarkBASIC Professional), I'm a bit confused on how and why things won't work, because they do in basic. First off, my code:

import java.awt.*;

import java.applet.*;

publicclass Testingextends Applet

{

int xPos = 50;

int yPos = 50;

int xSize = 10;

int ySize = 10;

int startX;

int startY;

int mouseX;

int mouseY;

publicstaticvoid main( String args[] )

{

}

publicvoid paint( Graphics g )

{

int[] construct =newint[5];

Constructable construct1 =new Constructable( g, startX, startY, mouseX, mouseY );

g.fillRect( xPos, yPos, xSize, ySize );

g.drawString("Testing", xPos, yPos );

}

publicboolean keyDown( Event e,int key )

{

if( key == Event.RIGHT )

{

xPos = xPos + 5;

repaint();

}

if( key == Event.LEFT )

{

xPos = xPos - 5;

repaint();

}

if( key == Event.DOWN )

{

yPos = yPos + 5;

repaint();

}

if( key == Event.UP )

{

yPos = yPos - 5;

repaint();

}

returntrue;

}

publicboolean mouseDown( Event e,int x,int y )

{

startX = x;

startY = y;

repaint();

returntrue;

}

publicboolean mouseDrag( Event e,int x,int y )

{

mouseX = x;

mouseY = y;

repaint();

returntrue;

}

}

class Constructable

{

public Constructable( Graphics g,int xStart,int yStart,int xMouse,int yMouse )

{

g.fillRect( xStart, yStart, xMouse - xStart, yMouse - yStart );

}

}

I wrote it from scratch so I understand what it does, but not how to do some things I'm wanting to do.

1) How would I create a new Constructable object every time I draw a rectangle? Right now the old one disappears when I draw a new one, but I don't want it to. I was trying to do something like "Constructor construct[1]", using an array, but I couldn't get it to work.

2) Why do you have to pass the "Graphics" into a method to use it? This gets really annoying because I have to repaint the entire screen when I want to redraw one object.

I've come across a few more problems/questions, but can't think of them right now. Any help appreciated.

[4821 byte] By [GilGalvantia] at [2007-11-27 1:03:41]
# 1

Hi,

1 - You should separate data from their representation (MVC design

pattern), a Construtable

will only store coordinates. The paint method

will then be used to draw all the Contrutables

you created

2 - Constructable creation should start in mouseDown event handler,

should be updated in mouseDrag event handler and should be

ended in mouseUp event handler.

Hope that help

Jack

jack@square6a at 2007-7-11 23:38:46 > top of Java-index,Java Essentials,New To Java...
# 2
Okay, thanks for the response, but I'm still confused. What do you mean sperate data from their representation? Also, how should Constructable creation be in the mouseDown event handler? Could you provide some sample code or just psuedo code to show me what you mean, please?
Gil_Galvantia at 2007-7-11 23:38:46 > top of Java-index,Java Essentials,New To Java...
# 3

Hi,

Something like this (really kick and dirty code !) :

public class Testing extends Applet

{

// List of constructables ...

Constructable cCurrent = null;

public static void main( String args[] )

{

}

public void paint( Graphics g )

{

// for each element c of my list of constructable :

// draw it

{

g.fillRect( c.xBegin, c.yBegin, c.xEnd - c.xBegin, c.yEnd - c.yBegin );

g.drawString( "Testing", c.xBegin, c.yBegin );

}

}

public boolean keyDown( Event e, int key )

{

if (cCurrent != null)

{

cCurrent.move (key);

repaint ();

}

return true;

}

public boolean mouseDown( Event e, int x, int y )

{

// start a new Constructable

cCurrent = new Constructable (x, y);

// add cCurrent to the list of constructable

repaint();

return true;

}

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

{

cCurrent.xEnd = x;

cCurrent.yEnd = y;

repaint();

return true;

}

public boolean mouseUp( Event e, int x, int y)

{

cCurrent.xEnd = x;

cCurrent.yEnd = y;

}

}

class Constructable

{

public int xBegin;

public int yBegin;

public int xEnd;

public int yEnd;

public Constructable( int xStart, int yStart )

{

xBegin = xStart;

xEnd = xStart;

yBegin = yStart;

yEnd = yStart;

}

public void move (int iDirection)

{

switch (iDirection)

{

case Event.RIGHT :

xBegin += 5;

xEnd += 5;

break;

case Event.LEFT :

xBegin -= 5;

xEnd -= 5;

break;

case Event.UP :

yBegin -= 5;

yEnd -= 5;

break;

case Event.DOWN :

yBegin += 5;

yEnd += 5;

break;

}

}

}

You have now to replace the comments with your favorite list structure

(Vector, Hastable ...)

Google MVC for details about the pattern

As you can see the Constructable class just hold the data.

Hope that help,

Jack

jack@square6a at 2007-7-11 23:38:46 > top of Java-index,Java Essentials,New To Java...