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;


@SuppressWarnings("serial")
public class Ex1 extends JFrame implements FocusListener {
	/**
	 * @param args

	 */
	public void focusGained(FocusEvent e)
	{
		if(e.getSource() == data)
		{data.setText("");
		}}
	public void focusLost(FocusEvent e)
	{	
	}
	private JTextField data;
	private JButton go;

	public Ex1(int size) {
		super("Face Drawing Program");
		setSize(750, 750);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		data = new JTextField("size of face?");
		data.addFocusListener(this);
		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());
		myPane.add(go);
		myPane.add(data); 
		validate();

	}

	public void makeFace(int size) {
		Graphics g = getGraphics();
		//draw face
		g.drawOval(265,275,275,275);
		//draw left eye, right eye, nose and mouth, respectively
		g.drawOval(320,365,60,35);
		g.drawOval(430,365,60,35);
		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);

	}

}

Recommended Answers

All 3 Replies

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 for hfx642

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.

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;

        @Override
        public void paintComponent(Graphics g) {
            g.setColor(Color.red);
            g.drawOval(0, 0, this.getWidth(), this.getHeight());
        }
    }

    public TestResizeFrame() {
        panel.setPreferredSize(new Dimension(INNER_WIDTH, INNER_HEIGHT));
        add(panel, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

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

            @Override
            public void run() {
                new TestResizeFrame().setVisible(true);
            }
        });
    }
}
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.