I'm just starting to learn Java and I need help. I'm trying to draw a rectangle in an applet window and have it centered so that if the applet is repainted when it is resized, the rectangle will stay centered in the applet window. I know how to retrieve the size of the applet, but I'm not quite sure what to do to center the rectangle. Can someone link me to a tutorial that explains something like this?

Recommended Answers

All 7 Replies

The math isn't difficult. You have the dimensions of the applet, so you know the center point. That center point is also the center point of the rectangle.
A rectangle in Swing is defined by the top left point and its two sides. The sides will not change - all you need to do is work out the offset for the top left point, relative to the center point. That should be pretty easy.

I should mention that in this course, our instructor hasn't covered anything related to graphics. He literally just told us how to make an applet and gave us instructions on how to draw a rectangle. He hasn't covered any of the basics of Java, and it's frustrating. I'm used to C++, so I don't understand Java yet. None of what he is asking us to do has been covered in the book yet, so I don't know why he is doing it.

Ugh. Well, it's good training for dealing with managerial sorts later on.

Okay, so all of your methods for putting shapes on the screen are in the Graphics class (java.awt.Graphics). Read through that, particularly the drawRectangle() method, and my previous post will probably make more sense.

Sun has tutorials that are pretty good - see what you can find there on graphics and applets, and then come back with any questions.

But how do I change the rectangle as someone resizes the window? Would I just increment the origin point by 1 for each pixel the window is resized or something? I'm really frustrated with my professor right now for starting us out with this...

I figured out how to get it to conform to the size of the applet, but it's kind of just floating to the bottom right.

/**
 * @(#)GraphicsEx.java
 *
 * GraphicsEx Applet application
 *
 * @author Darin Beaudreau
 * @version 1.00 2010/9/15
 */

import java.awt.*;
import java.applet.*;

public class GraphicsEx extends Applet {

	public void init() {
		setSize(200,200);
	}

	public void paint(Graphics g) {

		int appW = getSize().width;
		int appH = getSize().height;
		g.drawRect(appW-150,appH-175,100,150);

	}
}

I figured that if I placed it centered to begin with and had it subtract from the total applet size each time it resizes, that it would increment 1 pixel for each pixel it was resized and stay centered. Obviously, I don't quite have it yet.

I think the problem may be that the variables are not getting the new size each time the canvas is repainted... am I right? If so, how would I go about getting the new size each time it's repainted?

Calculate the center point (appW/2, appH/2) and then use those to figure where to place your upper left corner of the rect.

There we go! Yeah, I didn't quite understand the center point tip at first.

/**
 * @(#)GraphicsEx.java
 *
 * GraphicsEx Applet application
 *
 * @author Darin Beaudreau
 * @version 1.00 2010/9/15
 */

import java.awt.*;
import java.applet.*;

public class GraphicsEx extends Applet {

	public void init() {
		setSize(200,200);
		repaint();
	}

	public void paint(Graphics g) {

		int appW = getSize().width;
		int appH = getSize().height;
		int center1 = appW/2;
		int center2 = appH/2;
		g.drawRect(center1-50,center2-75,100,150);

	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.