Creating Rectangle inside a Frame

Hello,

What I'm trying to do is create a Rectangle that will expand in the frame when it is maximized. This is the code I've made so far.

import javax.swing.*;

import java.awt.*;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.Frame;

publicclass RectangleAppletextends Frame

{

publicstaticvoid main(String[] args)

{

JFrame frame =new JFrame("Test Frame");

frame.setSize(600,400);

frame.setVisible(true);

Rectangle box =new Rectangle(5, 10, 20, 30);

g2.draw(box);

box.translate(15, 25);

g2.draw(box);

}

}

any help will be much appreciated.

[1377 byte] By [ButcherBaya] at [2007-11-27 11:28:51]
# 1

Short answer: use increments of the current width and height.

import javax.swing.*;

import java.awt.*;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

//import java.awt.Frame;

public class MutableRect

{

public static void main(String[] args)

{

JFrame frame = new JFrame("Test Frame");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new DrawingComponent());

frame.setSize(600,400);

frame.setVisible(true);

}

}

class DrawingComponent extends JPanel

{

protected void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

int w = getWidth();

int h = getHeight();

Rectangle box = new Rectangle(5, 10, w/12, h/8);

g2.draw(box);

box.translate(15, 25);

g2.draw(box);

}

}

crwooda at 2007-7-29 16:24:29 > top of Java-index,Security,Cryptography...
# 2

Hey crwood,

Thanks for the quick response. The program you made for me wasn't the one I was trying to complete but helped me nonetheless. I managed to make the rectangle expand with the help of your code.

The program I made was:

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.applet.Applet;

import java.awt.*;

public class blueRectangle extends Applet

{

public void paint(Graphics g)

{

Graphics2D g2 = (Graphics2D)g;

int w = getWidth();

int h = getHeight();

Rectangle box = new Rectangle(0, 0, w/1, h/2);

g2.draw(box);

g2.setColor(Color.blue);

g2.fill(box);

}

}

If I wanted to make a square in the center of the frame how do I go about it?

Thanks again for any help

ButcherBaya at 2007-7-29 16:24:29 > top of Java-index,Security,Cryptography...
# 3

// <applet code="MutableRect" width="500" height="400"></applet>

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class MutableRect extends Applet

{

DrawingComponent drawingComponent;

public void init()

{

setLayout(new BorderLayout());

drawingComponent = new DrawingComponent();

add(drawingComponent);

}

public static void main(String[] args)

{

Applet applet = new MutableRect();

Frame frame = new Frame();

frame.addWindowListener(closer);

frame.add(applet);

frame.setSize(500,400);

frame.setLocation(200,200);

applet.init();

frame.setVisible(true);

}

private static WindowListener closer = new WindowAdapter()

{

public void windowCloseing(WindowEvent e)

{

System.exit(0);

}

};

}

class DrawingComponent extends Canvas

{

public void paint(Graphics g)

{

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

int w = getWidth();

int h = getHeight();

Rectangle box = new Rectangle(w/4, h/4);

box.x = (w - box.width)/2;

box.y = (h - box.height)/2;

g2.draw(box);

box.translate(15, 25);

g2.draw(box);

}

}

crwooda at 2007-7-29 16:24:29 > top of Java-index,Security,Cryptography...