We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,201 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

resizing several graphics objects to scale

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);

	}

}
4
Contributors
3
Replies
9 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
jhamill
Light Poster
25 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

Ezzaral
null
Moderator
16,111 posts since May 2007
Reputation Points: 3,292
Solved Threads: 873
Skill Endorsements: 28

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.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
Skill Endorsements: 1

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);
            }
        });
    }
}
mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0902 seconds using 2.78MB