Click to See Complete Forum and Search --> : visual java without applets


agent_x91
10-07-2004, 02:18 PM
how can a java application be made to use graphics, without using an applet?

AdamGundry
10-07-2004, 04:02 PM
Do you mean on a web page, or as a standalone application? If the former case, you must have an applet (even if it is a tiny invisible one), whereas in the latter you can create a subclass of JFrame or a similar class and design a graphical form.

Adam

buntine
10-07-2004, 09:41 PM
You may want to take a look at the MediaTracker object, which loads media in a seperate thread to maximize performance.

Also, overriding the paint and paintComponent methods can be helpful when draeing primitive objects.

Regards.

agent_x91
10-08-2004, 12:50 PM
okay thanks, both of you.

or as a stand alone application

and yeah, I meant as a stand-alone application, not on the web.

agent_x91
10-08-2004, 01:49 PM
Originally posted by AdamGundry
Do you mean on a web page, or as a standalone application? If the former case, you must have an applet (even if it is a tiny invisible one), whereas in the latter you can create a subclass of JFrame or a similar class and design a graphical form.

Adam

How do I create a subclass of JFrame? Do I simply make a class which extends it?

Also, which package is JFrame located in?

Thanks for the help.

HaganeNoKokoro
10-08-2004, 01:52 PM
Yes, you just extend it.

JFrame is in javax.swing I think...

agent_x91
10-08-2004, 01:53 PM
that's what I thought he meant by subclass, just checking. ah, i should have thought of javax.swing... thanks.

agent_x91
10-08-2004, 02:07 PM
okay, I've made a file which extends javax.swing.JFrame, but I'm still not sure how to make any graphics. help?

buntine
10-08-2004, 09:39 PM
Ok, im a tad shady here. Do you want to create an interface (such as form controls) or do you want to actually print graphics (gif, jpg) to the JFrame?

Or, do you want to draw lines, rectangles, and other shapes on the frame?

Regards.

agent_x91
10-09-2004, 03:05 AM
I'm mainly interested in being able to print graphics and draw shapes.

buntine
10-09-2004, 03:43 AM
Ok, your going to want to override the paint(Graphics g) and paintComponent(Graphics g) methods from java.awt.Container, whcih is a parent class of JFrame.

import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;

public class GraphicsTest extends JFrame
{
public GraphicsTest()
{
super("Graphics Test"); // Call super.
setSize(300, 300);
setVisible(true);
}

/*
* Override the paint method.
*/
public void paint(Graphics g)
{
// Format: fillRect(xpos, ypos, width, height)
g.setColor(Color.RED);
g.fillRect(30, 30, 100, 100);
}
}

I often find its more useful to cast the Graphics object to a Graphics2D object, which has much more methods.

Graphics2D g2d = (Graphics2D)g;

Regards.

buntine
10-09-2004, 03:45 AM
Do a Google search for JFrame and click the first returned link for the Java Docs. There you will find detains about all the methods you need to know about.

Also, check out the Graphics object. The MediaTracker object (i mentioned earlier) will also be helpful for displaying images.

Regards.

agent_x91
10-09-2004, 04:08 AM
okay thanks a lot buntine I'll try that now.

agent_x91
10-09-2004, 04:17 AM
hmm I checked that out before but it doesn't give me any clues of how to use it. Can someone give me an example source?

HaganeNoKokoro
10-09-2004, 07:06 AM
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.*;

public class GraphicsTest extends JFrame
{
public static void main(String args[]) {
GraphicsTest gt=new GraphicsTest();
}

public GraphicsTest()
{
super("Graphics Test"); // Call super.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
setSize(300, 300);
setVisible(true);
}

/*
* Override the paint method.
*/
public void paint(Graphics g)
{
// Format: fillRect(xpos, ypos, width, height)
g.setColor(Color.RED);
g.fillRect(30, 30, 100, 100);
}
}


The red part will make it run from the command line. Just compile and then java GraphicsTest. The Blue part allows you to handle window events (in this case all I do is when the window closes, I exit the program)

EDIT: Whoops, forgot to change that one FrameDemo into a GraphicsTest in the red part. I had been playing with the example in a file called FrameDemo.java (which was another frame demo thing I was playing with) and must have missed that one when changing them all back to GraphicsTest for consistency with the previous example). Derp!

agent_x91
10-09-2004, 07:22 AM
thanks a lot. I think my problem was I was writing so a Frame but I didn't know how to actually make it visible... I'll experiment with this for a while. Thanks again.

agent_x91
10-09-2004, 07:27 AM
Problem when compiling -

cannot resolve symbol
symbol : class FrameDemo
location : class GraphicsTest


it doesn't like your "FrameDemo" variable.

buntine
10-09-2004, 09:09 AM
Here ya go.

public static void main(String args[]) {
new GraphicsTest();
}

Regards.

agent_x91
10-09-2004, 09:20 AM
cheers, I'll try that.

agent_x91
10-09-2004, 09:30 AM
woohoo it works thanks buntine:D btw sorry I didn't notice the code you posted earlier lol :confused::o

agent_x91
10-09-2004, 09:53 AM
when you use drawing functions in the paint() function, 0 on the y coordinate starts at the title bar. is there any way of detecting the height of the title bar to avoid this problem?

thanks in advance.

buntine
10-09-2004, 09:57 AM
Well, the height should never change, so you can hard code any modifications. I think its height is about 22 pixels.

g.fillRect(0, 23, 100, 100);

Regards.

agent_x91
10-09-2004, 09:59 AM
okay thanks I'll just declare final int title_height=22 then.

agent_x91
10-09-2004, 10:11 AM
The actual width was 29, making 0 of the editable area 30. Thanks for the help.

agent_x91
10-09-2004, 10:15 AM
when I tried to add a mouse listener to the window, it threw a few errors. Something about the variable this could not be accessed from a static context. A little help?

I tried to use addMouseListener(this);

agent_x91
10-09-2004, 10:16 AM
never mind, it worked when I added the mouse listener inside the init() function.

agent_x91
10-09-2004, 10:44 AM
the mouselistener isn't working. did I do it wrong? I put it inside the init() function, but is init() just a standard function for Applet, not JFrame too?

agent_x91
10-09-2004, 11:00 AM
ah, I declared it with the windowListener in the constructor and it worked fine. thanks for everyone who's helped with this.

HaganeNoKokoro
10-09-2004, 02:07 PM
FrameDemo. Silly me. I had been playing with another example (FrameDemo.java), then saved over it with the GraphicsTest Code and changed all the GraphicsTest names for FrameDemo. Then, for consistency with the previous code, I (tried to) change all the FrameDemos back when I posted, but missed one. It was 4am here. That's my excuse :D

agent_x91
10-09-2004, 05:10 PM
poor excuse! hang your head in shame!:D


by the way, do you know how to capture mouse and key events fired outside the application window? see my other post. :) thx

buntine
10-09-2004, 09:38 PM
Its best to seperate handlers from the object. Atleast, I find it more readible.

Also, init() is only used in Applets. In an application, we use main(String args[]).

addMouseListener(new GraphicsTestMouseHandler())

Make a new file called GraphicsTextMouseHandler:

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;

public class GraphicsTextMouseHandler implements MouseListener
{
public void mouseClicked(MouseEvent me)
{
JOptionPane.showMessageDialog(null, "You clicked the frame");
}

public void mouseEntered(MouseEvent me)
{
}

public void mouseExited(MouseEvent me)
{
}

public void mousePressed(MouseEvent me)
{
}

public void mouseReleased(MouseEvent me)
{
}
}

Just place code in the methods that you need. Because your implementing an interface, you need to declare each method, but its up to you which ones contain code.
The methods will be automatically called when the used performs actions.

Regards.

agent_x91
10-10-2004, 05:27 AM
yeah, don't worry I figured that out already now.

I just declared the listeners in the wrong place - I tried to add them from inside the static main function.