hi,
does anyone know how it would be possible to change my code so that the circles drawn by the applet are resized as the applet screen size is changed?

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

@SuppressWarnings("serial")
public class OvalApp extends JApplet
{
	private int width, height, x=10, y=10;
	
	public void init()
	{
		height=getSize().height;
		width=getSize().width;
	}
	
	public void paint(Graphics g)
	{
		g.setColor(Color.green);
		super.paint(g);
		
		g.drawOval(0, 0, width, height);
		g.drawOval(x, y, width - 2*x, height- 2*y);
		g.drawOval(2*x, 2*y, width - 4*x, height-4*y);
		g.drawOval(3*x, 3*y, width - 6*x, height-6*y);
	}

}

Recommended Answers

All 3 Replies

Member Avatar for hfx642

Make the X-Pos, Y-Pos, Width, and Height relative to the size of the window.
If the window gets resized, it should repaint accordingly.

It is not really. What you need now is to get the width & height of the applet window every time right before you draw those ovals. The only relative X, Y position that may need to be added is when the size of applet is very close to your predefined x, y.

public void paint(Graphics g) {
    g.setColor(Color.green);
    super.paint(g);

    width = getSize().width;   // add it here
    height = getSize().height; // add it here
    g.drawOval(0, 0, width, height);
    g.drawOval(x, y, width - 2*x, height- 2*y);
    g.drawOval(2*x, 2*y, width - 4*x, height-4*y);
    g.drawOval(3*x, 3*y, width - 6*x, height-6*y);
  }

hi just to clarify the following program works perfect. In this one Im using squares instead of circles though.

int h = getSize().height;
		int w = getSize().width;
		
		super.paint(g);
		
		for(int i = 0; i < DrawSquare; i++)
		{
			//Draws the number of rectangles given by DrawSquare
			g.drawRect(i * 20,i * 20,(w-(i * 40)),(h-(i * 40)));
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.