1,105,386 Community Members

resizing several graphics objects to scale

Member Avatar
Light Poster
25 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]

So i made a face using several ovals, a arc, and a polyline in the graphics class. I want to be able to change the size of the face based on the number input into the JTextField. Right now the value for the textfield is the variable "size" in the drawface method. Any idea on how I could get the entire face (all of the drawn objects that its made of) to be resized to scale. Heres my code...

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Ex1 extends JFrame implements FocusListener {
	 * @param args

	public void focusGained(FocusEvent e)
		if(e.getSource() == data)
	public void focusLost(FocusEvent e)
	private JTextField data;
	private JButton go;

	public Ex1(int size) {
		super("Face Drawing Program");
		setSize(750, 750);
		data = new JTextField("size of face?");
		go = new JButton("draw it");
		go.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				makeFace(new Integer(data.getText()));            
		Container myPane = getContentPane();
		myPane.setLayout(new FlowLayout());


	public void makeFace(int size) {
		Graphics g = getGraphics();
		//draw face
		//draw left eye, right eye, nose and mouth, respectively
		int[] xArray = {410, 380, 410};
		int[] yArray = {425, 450, 450};
		g.drawPolyline(xArray, yArray, 3);
		g.drawArc(360, 475, 100, 25, 180, 180);

	public static void main(String[] args) {
		new Ex1(400);


Member Avatar
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]

You could use the Graphics2D.scale() method, which scales the entire graphics transform or you can just add a scale multiplier to all of the dimensions of your drawn objects. You'll probably want to use a float for the scaling factor.

I'd also recommend using a JPanel and overriding paintComponent() for your drawing. Painting directly to the JFrame with getGraphics() is too transient. You'll lose your graphics any time the system needs to re-render a portion of the window, such as resizing, etc.

Member Avatar
Posting Pro
515 posts since Nov 2009
Reputation Points: 164 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]

Well... You are sending a size to your makeFace method, but you are NOT using it.
You'll have to incorporate your size parameter into your heights and widths,
as well as your Xs and Ys of your graphic elements.

Member Avatar
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]

that about how to LayoutManagers works, for example

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestResizeFrame extends JFrame {

    private static final long serialVersionUID = 0;
    static final int INNER_WIDTH = 320;
    static final int INNER_HEIGHT = 320;
    private OvalPanel panel = new OvalPanel();

    private class OvalPanel extends JPanel {

        private static final long serialVersionUID = 0;

        public void paintComponent(Graphics g) {
            g.drawOval(0, 0, this.getWidth(), this.getHeight());

    public TestResizeFrame() {
        panel.setPreferredSize(new Dimension(INNER_WIDTH, INNER_HEIGHT));
        add(panel, BorderLayout.CENTER);

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TestResizeFrame().setVisible(true);
This article has been dead for over three months: Start a new discussion instead
Start New Discussion
View similar articles that have also been tagged: