tags:

views:

154

answers:

4

Just started to getting familiar with graphics in Java.

What is the simplest way to draw primitives (lines, rectangles) in Java without OpenGL (JOGL)?

Looking for methods like putPixel, lineTo, etc.

UPD.
Where to paint? Canvas? Panel? Bitmap?

+5  A: 

The built in interface is called "Graphics2D".

Here's a link to a Java Graphics2D Tutorial: http://java.sun.com/docs/books/tutorial/2d/index.html

Paul Tomblin
Thanks for adding the link, Andy.
Paul Tomblin
+1  A: 

The original Java user interface classes are called AWT. These were "heavyweight" components, that sometimes acted differently on different systems (Windows, Mac, Unix). These components were difficult to use to make a GUI.

Sun developed Swing, which is a set of "lightweight" components that, to the maximum degree possible, work the same on different systems. These components made GUI development somewhat easier.

In order to have a canvas for graphics, you start with a javax.swing.JFrame. You add a child javax.swing.JPanel to the JFrame. You draw on the JPanel by overriding the paint method.

The JPanel paint method takes a java.awt.Graphics as input. You can cast Graphics to java.awt.Graphics2D. The methods of Graphics2D allow you to draw rectangles, images, text, lines, and arbitrary polygons.

You can find out more about Swing by reading Sun's Creating a GUI with JFC/Swing tutorial. You can find out more about 2D Graphics by reading Sun's 2D Graphics tutorial. More details on the Java classes I've mentioned can be found in the Javadoc.

Gilbert Le Blanc
"overriding the print method" - I think you mean "paint method".
Paul Tomblin
You're correct. I've corrected my answer. Thanks for pointing out the mistake.
Gilbert Le Blanc
+1  A: 

My recent question about horizontal scrolling in Java includes a tiny little graphics example source code that you could use as a base to work from. There are both AWT and Swing implementations. The AWT doesn't support horizontal scrolling, so I'll be using swing.

Not recommending these as best practice or anything, they were a quick demonstration of my particular issue, but it might be enough to get you started.

Link is http://stackoverflow.com/questions/2843314/how-to-use-my-trackpad-for-horizontal-mousewheel-scrolling-in-a-java-awt-scrollpa

blissapp
+3  A: 

You can get a Graphics/Graphics2D instance from any AWT/Swing component via the paint method. JPanel is probably best since it fits well with swing and is lightweight, meaning that only one native window is created - for the top level window. Swing components can also be double-buffered, meaning that the painting is done to an offscreen buffer first, before being transferred to the screen. This gives a smoother appearance and avoids flickering that can happen when painting directly to the screen, and is particularly important for smooth animation.

You can specifically draw to an offscreen buffer ("bitmap") that you can use afterwards, e.g. to draw an image for saving as a file later:

   BufferedImage offImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
   Grapics2D g2 = offImg.createGraphics();

    // .. optionally set attributes ..
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
mdma