Simple Java Question

A

adric22

This may sound like a simple question, but I can't get a simple answer
anywhere. I'm a long-time programmer. I've used Assembler, Commodore
BASIC, Pascal, and C++. I've decided to stop using C++ and learn
Java so that my applications are cross-platform from now on.

However, I'm having the most difficult time finding information on how
to do what I want to do. I would rather program using the command-
line compiler if at all possible because the program I want to write
is a game and will essentially only be needing to plot pixels on the
screen and read input from the keyboard and mouse. Granted, I may
eventually want to write text on the screen or some basic shapes, but
most of what I'm going to be doing will just be plotting a screen-full
of pixels.

Unfortunately, I can't figure out how to do this. Everytime I lookup
info on the web or follow tutorials they are all about creating text
boxes, takling to databases, handing strings, etc.. And on the rare
occasion I find some source code that looks like it may be what I
want, I try to compile it but get tons of errors about missing files,
and especially things telling me that the library I'm trying to use
has been "depricated" Essentially saying the code is obsolete. Also
everytime I find something that looks useful it turns out to be
designed for a web-browser aplett and not an actually application that
you can run on your desktop.

I use a Mac primarily these days so that is the environment I'm in,
but I don't think that makes much difference.

if anyone can point me in the right direction of a web page, or some
modern source code that just shows how to open a window of a specified
size and plot a pixel at x,y coordinates in whatever color I want...
that would be great!
 
D

Daniel Pitts

adric22 said:
This may sound like a simple question, but I can't get a simple answer
anywhere. I'm a long-time programmer. I've used Assembler, Commodore
BASIC, Pascal, and C++. I've decided to stop using C++ and learn
Java so that my applications are cross-platform from now on.

However, I'm having the most difficult time finding information on how
to do what I want to do. I would rather program using the command-
line compiler if at all possible because the program I want to write
is a game and will essentially only be needing to plot pixels on the
screen and read input from the keyboard and mouse. Granted, I may
eventually want to write text on the screen or some basic shapes, but
most of what I'm going to be doing will just be plotting a screen-full
of pixels.

Unfortunately, I can't figure out how to do this. Everytime I lookup
info on the web or follow tutorials they are all about creating text
boxes, takling to databases, handing strings, etc.. And on the rare
occasion I find some source code that looks like it may be what I
want, I try to compile it but get tons of errors about missing files,
and especially things telling me that the library I'm trying to use
has been "depricated" Essentially saying the code is obsolete. Also
everytime I find something that looks useful it turns out to be
designed for a web-browser aplett and not an actually application that
you can run on your desktop.

I use a Mac primarily these days so that is the environment I'm in,
but I don't think that makes much difference.

if anyone can point me in the right direction of a web page, or some
modern source code that just shows how to open a window of a specified
size and plot a pixel at x,y coordinates in whatever color I want...
that would be great!

Start by learning Swing/AWT. You can create your own JComponent
subclass, which overrides the paintComponent method. Note that you may
be use to "active rendering", which is different than the standard
rendering model in Java. With active rendering, you decide *when* to
draw to the screen. In Java's model, your code is asked to perform the
task of redrawing the section of the screen.

It is possible to use Active Rendering in Java, but you have to jump a
couple of loops first, and you loose a bit of built-in flexibility.
 
M

markspace

adric22 said:
Unfortunately, I can't figure out how to do this. Everytime I lookup
info on the web or follow tutorials they are all about creating text
boxes, takling to databases, handing strings, etc.. And on the rare
occasion I find some source code that looks like it may be what I
want, I try to compile it but get tons of errors about missing files,
and especially things telling me that the library I'm trying to use
has been "depricated" Essentially saying the code is obsolete. Also


First things first. You need to learn Java basics, so you understand
these errors and know what to do about them. Including a library is a
basic skill in compiling, and if you don't know how to do it, you don't
know anything about Java yet.

<http://www.javapassion.com/javaintro/>

This free online class is sponsored by Sang Shin, a Sun researcher and
Java evangelist. I used this class to jump start my understanding of
Java, and while most of the concepts are remedial to experienced
programmers, the structured lessons are an excellent way to make sure
you learn Java with no gaps. (Well, start with no gaps, anyway.)

everytime I find something that looks useful it turns out to be
designed for a web-browser aplett and not an actually application that
you can run on your desktop.


Applets and desktop apps are almost exactly the same thing. More
experience with Java will help you move from one to another.

if anyone can point me in the right direction of a web page, or some
modern source code that just shows how to open a window of a specified
size and plot a pixel at x,y coordinates in whatever color I want...
that would be great!


After you learn Java, the following will help you. Windows in Java are
JWindow or JFrame:

<http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html>

All of the links on the left hand side of that page will also help you
understand the various Java window components. Java is multi-threaded.
You'll need to learn about concurrency too when dealing with any
window component:

<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html>

Finally, the graphics x-y stuff is done with a Graphics object:

<http://java.sun.com/docs/books/tutorial/2d/index.html>

You should probably read the rest of those tutorials, and also start
reading the online Java docs. These will be invaluable in the long run.


Good luck.
 
M

Mike Schilling

I'll be writing a command-line Java program whose user will want it to be
runnable as mnay places as possible, certainly including Windows, Linux,
Unix, and Mac. The program itself should run fine on all of these; my
question is about how to make it easy to run it.

For Windows I'll write a batch script that will do the job of looking for
the JRE and starting up the Java program. For Unix and Linux this will be a
shell script. What's the (rough) equivalent for a Mac? (JavaWebStart isn't
a possibility here. I'm not going to host the program anywhere; I'm just
going to hand it over.)
 
R

Roedy Green

Unfortunately, I can't figure out how to do this. Everytime I lookup
info on the web or follow tutorials they are all about creating text
boxes, takling to databases, handing strings, etc.. And on the rare
occasion I find some source code that looks like it may be what I
want, I try to compile it but get tons of errors about missing files,
and especially things telling me that the library I'm trying to use
has been "depricated" Essentially saying the code is obsolete. Also
everytime I find something that looks useful it turns out to be
designed for a web-browser aplett and not an actually application that
you can run on your desktop.

What you want is a simple Canvas. You can then paint pixels on it.
The catch is the OS can ask you to repaint parts of the image at any
time, because, for example a window eclipsing your image has been
removed.

So your program must keep the raw data around, and repaint the clip
region rectangle of the image at any time in the paint or
paintComponent method.

See http://mindprod.com/jgloss/canvas.html
http://mindprod.com/jgloss/paint.html
http://mindprod.com/jgloss/paintcomponent.html

You might look at some of my apps that use a Canvas (or the Swing
equivalent JPanel).

see

http://mindprod.com/products2.html#SCREWS
http://mindprod.com/products2.html#JDISPLAY
http://mindprod.com/products1.html#MASKER
http://mindprod.com/products1.html#FONTSHOWERAWT
--
Roedy Green Canadian Mind Products
http://mindprod.com

When you can’t find a bug, you are probably looking in the wrong place. When you can’t find your glasses, you don’t keep scanning the same spot because you are convinced that is where you left them.
~ Roedy
 
M

markspace

Mike said:
I'll be writing a command-line Java program whose user will want it to be
runnable as mnay places as possible, certainly including Windows, Linux,
Unix, and Mac. The program itself should run fine on all of these; my
question is about how to make it easy to run it.

For Windows I'll write a batch script that will do the job of looking for
the JRE and starting up the Java program. For Unix and Linux this will be a
shell script. What's the (rough) equivalent for a Mac?


Hmm, Mac should be exactly the same as Linux/Unix, yeah? I'm not sure
how to add a desktop icon to a shell script on a Mac, but I get Google
knows how.
 
J

John B. Matthews

"Mike Schilling said:
I'll be writing a command-line Java program whose user will want it to be
runnable as mnay places as possible, certainly including Windows, Linux,
Unix, and Mac. The program itself should run fine on all of these; my
question is about how to make it easy to run it.

For Windows I'll write a batch script that will do the job of looking for
the JRE and starting up the Java program. For Unix and Linux this will be a
shell script. What's the (rough) equivalent for a Mac? (JavaWebStart isn't
a possibility here. I'm not going to host the program anywhere; I'm just
going to hand it over.)

Mac OS X is Unix. It's not rough at all! I generally use the same shell
script on Mac OS X and Linux. Pick a shell, any shell:

$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
 
D

Donkey Hottie

adric22 said:
This may sound like a simple question, but I can't get a
simple answer anywhere. I'm a long-time programmer.
I've used Assembler, Commodore BASIC, Pascal, and C++.
I've decided to stop using C++ and learn Java so that my
applications are cross-platform from now on.

However, I'm having the most difficult time finding
information on how to do what I want to do. I would
rather program using the command- line compiler if at all
possible because the program I want to write is a game
and will essentially only be needing to plot pixels on
the screen and read input from the keyboard and mouse.
Granted, I may eventually want to write text on the
screen or some basic shapes, but most of what I'm going
to be doing will just be plotting a screen-full of
pixels.

Unfortunately, I can't figure out how to do this.
Everytime I lookup info on the web or follow tutorials
they are all about creating text boxes, takling to
databases, handing strings, etc.. And on the rare
occasion I find some source code that looks like it may
be what I want, I try to compile it but get tons of
errors about missing files, and especially things telling
me that the library I'm trying to use has been
"depricated" Essentially saying the code is obsolete.
Also everytime I find something that looks useful it
turns out to be designed for a web-browser aplett and not
an actually application that you can run on your desktop.

I use a Mac primarily these days so that is the
environment I'm in, but I don't think that makes much
difference.

if anyone can point me in the right direction of a web
page, or some modern source code that just shows how to
open a window of a specified size and plot a pixel at x,y
coordinates in whatever color I want... that would be
great!

I can't help you, and I have myself been very pessimistic about Java what
comes to games.

But then there is the game IL2 the Combat Flight Simulator. It is written in
Java, and it performs like the best of them!

It is possible, you just have to be a Russian nerd to do it.
 
M

Mike Schilling

John said:
Mac OS X is Unix. It's not rough at all! I generally use the same
shell script on Mac OS X and Linux. Pick a shell, any shell:

$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

Excellent. Thanks, guys.
 
A

adric22

First, thanks for all the replies.. it will take a while to look
through all the links I was given. But I wanted to take a minute to
reply to some people.
First things first. You need to learn Java basics, so you understand
these errors and know what to do about them. Including a library is a
basic skill in compiling, and if you don't know how to do it, you don't
know anything about Java yet.

Yes, I know how to include libraries as that is part of any other
programming language I've used. It is just irritating that I download
a simple source code off the internet that is only 10 lines long, then
I try to compile it to find that it is using some library that
appearently my version of Java doesn't have and I have no idea where
to get it..
What you want is a simple Canvas. You can then paint pixels on it.
The catch is the OS can ask you to repaint parts of the image at any
time, because, for example a window eclipsing your image has been
removed.

I've done some programming under X-Windows before, and it was exactly
the same there. No problem, really.
 
M

markspace

adric22 said:
Yes, I know how to include libraries as that is part of any other
programming language I've used. It is just irritating that I download
a simple source code off the internet that is only 10 lines long, then
I try to compile it to find that it is using some library that
appearently my version of Java doesn't have and I have no idea where
to get it..


If you really know Java, you should be able to scan 10 lines in about as
many seconds and know if it needs external libraries or not. Just saying'.

Got a link to one example? I'll have a look if you want.
 
A

Arne Vajhøj

adric22 said:
However, I'm having the most difficult time finding information on how
to do what I want to do. I would rather program using the command-
line compiler if at all possible because the program I want to write
is a game and will essentially only be needing to plot pixels on the
screen and read input from the keyboard and mouse. Granted, I may
eventually want to write text on the screen or some basic shapes, but
most of what I'm going to be doing will just be plotting a screen-full
of pixels.
if anyone can point me in the right direction of a web page, or some
modern source code that just shows how to open a window of a specified
size and plot a pixel at x,y coordinates in whatever color I want...
that would be great!

Try the program below. It is rather useless in itself, but should
show something anyway.

Arne

=============================

package october;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class DrawingFun extends JFrame {
private DrawingField df;
public DrawingFun() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
setTitle("Use arrows to move and R G B to change color");
df = new DrawingField();
addKeyListener(df);
getContentPane().add(df);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new DrawingFun();
f.setVisible(true);
}
});
}
}

class DrawingField extends JPanel implements KeyListener {
private final static int W = 640;
private final static int H = 480;
private BufferedImage img;
private int x;
private int y;
private Color color;
public DrawingField() {
img = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, W, H);
setPreferredSize(new Dimension(W, H));
x = 0;
y = 0;
color = Color.BLACK;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
@Override
public void keyPressed(KeyEvent e) {
int oldx = x;
int oldy = y;
switch(e.getKeyCode()) {
case KeyEvent.VK_DOWN:
if(y < H - 1) {
y += 5;
}
break;
case KeyEvent.VK_UP:
if(y > 0) {
y -= 5;
}
break;
case KeyEvent.VK_LEFT:
if(x > 0) {
x -= 5;
}
break;
case KeyEvent.VK_RIGHT:
if(x < W - 1) {
x += 5;
}
break;
case KeyEvent.VK_R:
color = Color.RED;
break;
case KeyEvent.VK_G:
color = Color.GREEN;
break;
case KeyEvent.VK_B:
color = Color.BLUE;
break;
}
Graphics g = img.getGraphics();
g.setColor(color);
g.fillOval(Math.abs(x + oldx) / 2, Math.abs(y + oldy) / 2, 10, 10);
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
 
A

alexandre_paterson

Hmm, Mac should be exactly the same as Linux/Unix, yeah? I'm not sure
how to add a desktop icon to a shell script on a Mac, but I get Google
knows how.

OK, I'm completely confused about the requirements here...

What is a "command-line Java program" in this case? A program
with no GUI, called from the command-line right!?

In that case why would such a program need an icon?

On OS X .jar files are double-clickable. I've packaged
GUI applications self-contained in a single .jar for OS X and
it works (it was just a test).

However the preferred way to do it is to use OS X's Jar Bundler
(which shall create an application installable/removable the
OS X way and allow to fine tune VM parameters etc.) or do to
the equivalent of what OS X's Jar Bundler does (from, say, an
Ant task).


On OS X there's no need to real need look for the JRE. If an
OS X install doesn't have Java then it's a non-compliant OS X.

On OS X, at a shell prompt, you can always directly invoke
Java. So from a shell script:

#!/bin/bash
#

java -version


Once again I'm a bit confused as to what kind of Java app
we're talking about here. A command-line utility has no
icon and needs no icon (what would icons for command-line
utilities in the /usr/bin/... directory need to look like!?)

While an application that has an icon usually doesn't
need to be launched from the command line (I like to
launch them using OS X's Spotlight, but that's just me).

/Alex <-- confused
 
A

alexandre_paterson

@Override

Ouch!

And then instead of having his 10 lines program
not compiling he'll have your example not compiling
because he's not using Java 1.6 :)

For the @Override annotation in 1.5 doesn't consider
that implementing a method defined in an interface
is a form of overriding.

The confused terminology in Java regarding interfaces,
classes, overriding and implementing --reflected as
a case in point in that @Override 1.5/1.6 difference
SNAFU-- is mindboggling.

/me <-- ducks
 
A

alexandre_paterson

I use a Mac primarily these days so that is the environment I'm in,
but I don't think that makes much difference.

Using a Mac has some consequences regarding the Java version
you'll be able to use to develop.

Apple machine pre-64 bit: no Java 6.

Apple machine with Snow Leopard: by default only Java 1.6
is installed (this broke a lot of badly designed/packaged
Java app mandating Java 1.5).
 
T

Tom Anderson

I'll be writing a command-line Java program whose user will want it to be
runnable as mnay places as possible, certainly including Windows, Linux,
Unix, and Mac. The program itself should run fine on all of these; my
question is about how to make it easy to run it.

For Windows I'll write a batch script that will do the job of looking
for the JRE and starting up the Java program. For Unix and Linux this
will be a shell script. What's the (rough) equivalent for a Mac?
(JavaWebStart isn't a possibility here. I'm not going to host the
program anywhere; I'm just going to hand it over.)

In the big Developer Tools package from Apple, there's a tool which makes
OS X applications out of JARs - you give it the JAR, specify JVM flags,
and give it an icon, and it spits out a .app bundle.

tom
 
A

Arne Vajhøj

Ouch!

And then instead of having his 10 lines program
not compiling he'll have your example not compiling
because he's not using Java 1.6 :)

For the @Override annotation in 1.5 doesn't consider
that implementing a method defined in an interface
is a form of overriding.

In that case he just delete them.

The example is not so literal anyway.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top