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.

