|
-
Apr 9th, 2001, 03:39 PM
#1
Thread Starter
Addicted Member
Hi, I'm currently working on a Java assignment, but because the book I have is mainly based on the GUI side I don't really have any resource for plain Java stuff to run under DOS (you know those straight forward liner programs).
Any way I need to know how to do the following:
1. Clear the screen the proper way, because our teacher at this stage has just said to put about 30 returns in and that should clear it, but I want to know how to do it properly.
2. How can I place text at coordinate I choose (in Pascal I use to use gotoXY is there anything similar in JAVA?
3. Draw rectangles and fill them, remember this is a command line program so no GUI stuff please.
I think that'll do for now...
!!! THANKS IN ADVANCE !!!
-
Apr 9th, 2001, 04:01 PM
#2
Hyperactive Member
1) if you want to know how to clear the dos screen, you can use the command cls
2) To put text to the dos screen use System.out.println("THE TEXT");
To put text to an applet or application screen at specific coords, you can use the drawText method of the graphics class,
g.drawText("Hello my name is Jim!", 10,10);
That will draw the string starting at coords 10, 10, remember java does coords like other programs, with 0,0 at upper left corner.
3) to draw an empty rectangle you can use
g.drawRect(x-coord,y-coord,width,height);
to draw filled rectangles you can use
g.fillRect(x-coord,y-coord,width,height);
if you want to have them colored differently before you draw them you can switch colors by doing
g.setColor(Color.green);
or whatever color you want, look up Color class
-
Apr 9th, 2001, 04:13 PM
#3
Thread Starter
Addicted Member
I know how to clear the screen in DOS it self, what I meant was how can I clear the DOS Command Window from within my program you know like in PASCAL you would use ClrScr etc.
The other thing about displaying text at given cordinates, I want to do it in DOS not in an Applet. I need to be able to print out the text from my Java program at cordinates I supply onto the DOS Command Window not to an applet.
Thanks
Last edited by Ramandeep; Apr 10th, 2001 at 12:23 PM.
-
Apr 11th, 2001, 03:47 PM
#4
I think unless you take control of a piece of real estate on your desktop, you can only use line commands. So you either do gui, or you can only speak about lines. Like your teacher said, multiple returns (or blank System.out.println()'s to clear the screen using the latter).
You'll see this if you try Runtime.exec("cls"); nothing is gained due to running in a different process.
What you don't want to hear:
Once I made a frame (gui) that looked like the DOS prompt. Then I had control over it as you seek. But that is using the dreaded gui concept.
In NT, you can have many lines in a DOS prompt, so knowing how many blank lines to clear the screen might be a pain. I'm sure there is an API guru that could tell you how many lines you need to clear the screen.
So each frame (where "frame" represents 24 standard lines [or n non-standard lines]) would have to be generated to make your presentation page look like "nice". But then you'll have coarse scrolling or flicker making it look not so nice. Then there is the issue of where to get user input. I think it will always be at the bottom of your screen.
I think even a DOS solution would be bitmapping (i.e. gui).
So you might want to embrace the gui concept. You may surprise yourself and become the next gui guru.
The Java "paint" method is your friend.
-
Apr 12th, 2001, 03:22 PM
#5
Thread Starter
Addicted Member
OK let's forget about clearing the screen I can see it's going to be a pain in the back side, I'll just keep it simple. Thanks for your help any way guys.
Here's my current problem, as I'm new to Java I need all the help I can get as my heart isn't really into this language (i find it off putting) anyway here's the problem:
I've created two classes one (SongAccount) holds instance variables and methods to manupulate those variables.
The other (TopTen01) is the main class, this class uses the other class to instantiate objects as element of an array. But I get some error message at compile time. I had about 26 errors at first which then I cut down to 18 then 9 now I think it's bout 6. I listed the classes bellow, just to test the first class I created only two accounts in the main class. Am I doing all this right? Would it be better for me to use a record type instead of the SongAcount class?
Please help, this is kind of urgent!!!
Class SongAcount:
Code:
public class SongAccount
{
private int chartPosistion;
private String songTitle,
songArtist,
recordCompany;
private double price;
// ----------------------------------------------------------------------------------------------------------------------
public void setChartPosistion ( int posistion )
{
chartPosistion = posistion;
}
public int getChartPosistion ()
{
return chartPosistion;
}
// ----------------------------------------------------------------------------------------------------------------------
public void setSongTitle ( String title )
{
songTitle = title;
}
public String getSongTitle ()
{
return songTitle;
}
// ----------------------------------------------------------------------------------------------------------------------
public void setSongArtist ( String artist )
{
songArtist = artist;
}
public String getSongArtist ()
{
return songArtist;
}
// ----------------------------------------------------------------------------------------------------------------------
public void setRecordCompany ( String company )
{
recordCompany = company;
}
public String getRecordCompany ()
{
return recordCompany;
}
// ----------------------------------------------------------------------------------------------------------------------
public void setPrice ( double amount )
{
price = amount;
}
public double getPrice ()
{
return price;
}
// ----------------------------------------------------------------------------------------------------------------------
}
Class TopTen01
Code:
public class TopTen01
{
public static void main( String args[] )
{
SongAccount [] topTenSongs = new SongAccount [ 2 ];
topTenSongs[0].setChartPosistion ( 1 );
topTenSongs[0].setSongTitle ( "Song 01" );
topTenSongs[0].setSongArtist ( "Artist 01" );
topTenSongs[0].setRecordCompany ( "Company 01" );
topTenSongs[0].setPrice ( 3.99 );
topTenSongs[1].setChartPosistion ( 2 );
topTenSongs[1].setSongTitle ( "Song 02" );
topTenSongs[1].setSongArtist ( "Artist 02" );
topTenSongs[1].setRecordCompany ( "Company 01" );
topTenSongs[1].setPrice ( 4.99 );
System.out.println ( "-------------------------------------------------------------------" );
System.out.println ( "ID\tPOSISTION\tTITLE\tARTIST\tCOMPANY\tPRICE" );
System.out.println ( "-------------------------------------------------------------------" );
for (int i = 0; i <= 1; i++ )
{
System.out.println ( i + \t + topTenSongs[i].getChartPosistion + \t + topTenSongs[i].getSongTitle + \t + topTenSongs[i].getSongArtist + \t + topTenSongs[i].getRecordCompany + \t + topTenSongs[i].getPrice );
}
System.out.println ( "-------------------------------------------------------------------" );
}
}
I looked at another example and that works fine, why doesn't this one?
Before some reply's asking what I'm trying to do her is the answer:
I want to create a program which can hold a record of Top Ten songs.
-
Apr 12th, 2001, 06:50 PM
#6
Dazed Member
Ramandeep i was going to try and compile your
code but first off i have ask. Are these two classes
in seperate files? because i dont see any import
directives anywhere in your code...
And what errors are being generated..
-
Apr 12th, 2001, 07:10 PM
#7
Dazed Member
Ok the SongAccount class compiles fine.
Now the TopTen01 class file compiles fine....
public class TopTen01
{
public static void main( String args[] )
SongAccount [] topTenSongs = new SongAccount [ 2 ];
topTenSongs[0].setChartPosistion ( 1 );
topTenSongs[0].setSongTitle ( "Song 01" );
topTenSongs[0].setSongArtist ( "Artist 01" );
topTenSongs[0].setRecordCompany ( "Company 01" );
topTenSongs[0].setPrice ( 3.99 );
topTenSongs[1].setChartPosistion ( 2 );
topTenSongs[1].setSongTitle ( "Song 02" );
topTenSongs[1].setSongArtist ( "Artist 02" );
topTenSongs[1].setRecordCompany ( "Company 01" );
topTenSongs[1].setPrice ( 4.99 );
System.out.println ( "-------------------------------------------------------------------" );
System.out.println ( "ID\tPOSISTION\tTITLE\tARTIST\tCOMPANY\tPRICE" );
System.out.println ( "-------------------------------------------------------------------" );
for (int i = 0; i <= 1; i++ )
{
System.out.println ( i + topTenSongs[i].getChartPosistion() + topTenSongs[i].getSongTitle() + topTenSongs[i].getSongArtist() + topTenSongs[i].getRecordCompany() + topTenSongs[i].getPrice() );
}
System.out.println ( "-------------------------------------------------------------------" );
}
}
you omitted the "()" in your calls to the getsongTitle()
getSongArtist(), getRecordCompany(),getPrice()
methods
but you are generating a null pointer exception
-
Apr 12th, 2001, 08:32 PM
#8
Dazed Member
I played with the TopTen01 class a little:
omitting the SongAccount class
public class TopTen01
{
public static void main( String args[] )
{
String[] songinfoholder = new String[10];
songinfoholder[0] = "1";
songinfoholder[1] = "Disconnected";
songinfoholder[2] = "FacetoFace";
songinfoholder[3] = "Electra";
songinfoholder[4] = "15.99";
songinfoholder[5] = "2";
songinfoholder[6] = "22nd street";
songinfoholder[7] = "LagWagon";
songinfoholder[8] = "Epataph";
songinfoholder[9] = "14.99";
System.out.println ( "-------------------------------------------------------------------------" );
System.out.println ( " ID " + " POSISTION " + " TITLE " + " ARTIST " + " COMPANY " + " PRICE ");
System.out.println ( "-------------------------------------------------------------------------" );
for (int i = 0; i <= 9; i++ ){
System.out.println(songinfoholder[i] + " ");
}
}
}
I didnt format the output to print a new line every 5
lines but i guess you could use the % "Modulo"
operator to achieve that.
Why dont you store the record information in a file
and read it in and store the information in
a Vector this way you dont have to hardcode
all of that stuff and the Vector will expand as needed?
just a thought
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|