Results 1 to 17 of 17
  1. #1
    chappa is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default AWT scaling the window to the thing that is painted

    How can that be done?



    For example my code is painting a circle, however that circle is very little, so I would like to scale the window.

    If i do that however, then only the window itself will become bigger but not the circle.


    Any solutions to this?



    Thanks in Advance


    Chappa

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by chappa View Post
    How can that be done?
    For example my code is painting a circle, however that circle is very little, so I would like to scale the window.

    If i do that however, then only the window itself will become bigger but not the circle.
    1) What do you mean by "scale the window"? Do you mean shrink the window so that the circle fills it? Do you mean paint a larger circle? or transform the circle?

    2) Without your code, it's hard to see what you are doing right or wrong.

    3) Why use AWT when this has been outdated for quite a few years. Swing is much more flexible and robust. Also, since many more people use Swing than AWT, the chances of your getting help increases dramatically if you use Swing and ask Swing-related questions.

  3. #3
    chappa is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default

    Hi Fubarable,


    1) I mean that I have a function that will give me out some numbers. These numbers are then plotted. Now the entire plot (f.e. a circle) is relatively small. So i would like to make the window bigger and make that not only the window gets bigger but also the circle

    2) My code wont help much to this issue...

    3) I am very new to Java, and I have been using AWT for my whole little code now. I sure will resettle to Swing after my current coding assignment. (No need to resettle right now while midst of a working code with AWT)

    Thanks for your attention

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    chappa is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default

    So this code below will paint 2 circles (will do more later on) in a window-frame that will open up. Now I want to make that window (with the painted circles) scalable, that means that i can pull the window smaller or bigger and the circle will also become smaller or bigger.

    Java Code:
    import java.lang.*;
    import java.awt.*;
    import javax.swing.*;
    
    class mj2 extends Frame {
      mj2 ( ) {
        add ("Center", new Dumbo( )); 
        setTitle ("A Flag");
        setSize (300, 200);
        setVisible (true);
      }
    
    public static void main (String [ ] args) {
        new mj2 ( );
      }
    }
    class Dumbo extends Canvas {
      public void paint (Graphics g) {
      
    
      int m = 200000;
      int m1 = 6;
      int m2 = 8;
      double gamma = 6.67;
      double r = 15;
      double r2 = 20;
      double vx = 0;
      double vx2 = 0;
      double alpha = 0;
      double alpha2 = 0;
      double x = r;
      double x2 = r2;
      double y = 0;
      double y2 = 0;
      int b = 160;
      int h = 130;
      double mittelpunktx = b/2;
      double mittelpunktx2 = b/2;
      double mittelpunkty = h/2;
      double mittelpunkty2 = h/2;
      double vy = 0.0001 * Math.sqrt(1000 * gamma * m / r);
      double vy2 = 0.0001 * Math.sqrt(1000 * gamma * m / r2);
      double tend = 256;
      double t = 0;
      double pi = 4*Math.atan(1);
      double deltat = 0.01;
      double result = 0;
    
      
     
    while (t <= tend) {
        t = t + deltat;
        r = Math.sqrt(x * x + y * y);
    	r2 = Math.sqrt(x2 * x2 + y2 * y2);
    	
        double a = 0.00001 * gamma * ((m /r) / r);
        double a2 = 0.00001 * gamma * ((m /r2) / r2);
    	//double avon1auf2 = 0.00001 * gamma * ((m2 / rwechselq);
    	//double avon2auf1 = 0.00001 * gamma * ((m1 / rwechselq);
       // alpha = Math.atan(y / x);
       // alpha2 = Math.atan(y2 / x2);
        alpha = winkel(x,y,alpha);
        alpha2 = winkel(x2,y2,alpha2);
    	//double poswechselx = posx2 - posx;
    //	double poswechsely = posy2 - posy;
    //	double rwechselq = poswechselx * poswechselx + poswechsely * poswechsely;
    //	System.out.println(
    
        double ax = a * Math.cos(alpha);
        double ay = a * Math.sin(alpha);
        double ax2 = a2 * Math.cos(alpha2);
        double ay2 = a2 * Math.sin(alpha2);
    
        vx = vx - ax * deltat;
        vy = vy - ay * deltat;
        x = x + vx * deltat;
        y = y + vy * deltat;
    	vx2 = vx2 - ax2 * deltat;
        vy2 = vy2 - ay2 * deltat;
        x2 = x2 + vx2 * deltat;
        y2 = y2 + vy2 * deltat;
        g.setColor(Color.blue);  
        double posx = mittelpunktx + x;
        double posy = mittelpunkty + y;
        double posx2 = mittelpunktx2 + x2;
        double posy2 = mittelpunkty2 + y2;
    	
    	
        g.drawLine((int)(posx),(int)(posy),(int)(posx),(int)(posy));                           
    	g.drawLine((int)(posx2),(int)(posy2),(int)(posx2),(int)(posy2)); 
    	System.out.println(posx + " " + posy + "pos1");
    		}
    	//double poswechselx = posx2 - posx;
    	//double poswechsely = posy2 - posy;
    	//double rwechsel = Math.sqrt(poswechselx * poswechselx + poswechsely * poswechsely);
    	//System.out.println
    	}
      public double winkel(double xposition, double yposition, double result){
    	result = Math.atan(yposition / xposition);
    	double pi = 4*Math.atan(1);
      	if	((xposition < 0) && (yposition < 0)) {
    		result = result + pi;
    	} 
    	if ((xposition < 0) && (yposition > 0)){
    		result = result + pi;
    	}
    	if ((xposition > 0) && (yposition < 0)) {
    		result = result + 2*pi;
    	}
    	return result;
    		}
    }
    Last edited by chappa; 12-13-2009 at 11:17 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Here I would use one class to create two lists: List<Point2D> and then the GUI class to display them. In the GUI class, I'd use a multiplier to scale the data to the graphics. For example (sorry about poor AWT code as I really don't know AWT all that well):

    Dumbo2B.java -- to create the data points
    Java Code:
    import java.awt.geom.Point2D;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Dumbo2B {
      private List<Point2D> pointsA = new ArrayList<Point2D>();
      private List<Point2D> pointsB = new ArrayList<Point2D>();
      
      int m = 200000;
      int m1 = 6;
      int m2 = 8;
      double gamma = 6.67;
      double r = 15;
      double r2 = 20;
      double vx = 0;
      double vx2 = 0;
      double alpha = 0;
      double alpha2 = 0;
      double x = r;
      double x2 = r2;
      double y = 0;
      double y2 = 0;
      int b = 160;
      int h = 130;
      double mittelpunktx = b / 2;
      double mittelpunktx2 = b / 2;
      double mittelpunkty = h / 2;
      double mittelpunkty2 = h / 2;
      double vy = 0.0001 * Math.sqrt(1000 * gamma * m / r);
      double vy2 = 0.0001 * Math.sqrt(1000 * gamma * m / r2);
      double tend = 256;
      double t = 0;
      double pi = 4 * Math.atan(1);
      double deltat = 0.01;
      double result = 0;
    
      Dumbo2B() {
        while (t <= tend) {
          t = t + deltat;
          r = Math.sqrt(x * x + y * y);
          r2 = Math.sqrt(x2 * x2 + y2 * y2);
    
          double a = 0.00001 * gamma * ((m / r) / r);
          double a2 = 0.00001 * gamma * ((m / r2) / r2);
          alpha = winkel(x, y, alpha);
          alpha2 = winkel(x2, y2, alpha2);
    
          double ax = a * Math.cos(alpha);
          double ay = a * Math.sin(alpha);
          double ax2 = a2 * Math.cos(alpha2);
          double ay2 = a2 * Math.sin(alpha2);
    
          vx = vx - ax * deltat;
          vy = vy - ay * deltat;
          x = x + vx * deltat;
          y = y + vy * deltat;
          vx2 = vx2 - ax2 * deltat;
          vy2 = vy2 - ay2 * deltat;
          x2 = x2 + vx2 * deltat;
          y2 = y2 + vy2 * deltat;
    
          double posx = mittelpunktx + x;
          double posy = mittelpunkty + y;
          double posx2 = mittelpunktx2 + x2;
          double posy2 = mittelpunkty2 + y2;
    
          pointsA.add(new Point2D.Double(posx, posy));
          pointsB.add(new Point2D.Double(posx2, posy2));
        }
      }
      
      public List<Point2D> getPointsA() {
        return pointsA;
      }
    
      public List<Point2D> getPointsB() {
        return pointsB;
      }
    
      public double winkel(double xposition, double yposition, double result) {
        result = Math.atan(yposition / xposition);
        double pi = 4 * Math.atan(1);
        if ((xposition < 0) && (yposition < 0)) {
          result = result + pi;
        }
        if ((xposition < 0) && (yposition > 0)) {
          result = result + pi;
        }
        if ((xposition > 0) && (yposition < 0)) {
          result = result + 2 * pi;
        }
        return result;
      }
    
    }

    Mj2B.java -- the GUI Applet code
    Java Code:
    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Panel;
    import java.awt.geom.Point2D;
    import java.awt.image.BufferedImage;
    import java.util.List;
    
    @SuppressWarnings("serial")
    public class Mj2B extends Applet {
      private static final double SCALE = 4.0;
      private static final int SIDE = 400;
      
      // uses a buffered image to save time on redraws
      private BufferedImage image = new BufferedImage(SIDE, SIDE, BufferedImage.TYPE_INT_RGB);
      private Panel panel = new Panel() {
        public void paint(Graphics g) {
          super.paint(g);
          if (image != null) {
            g.drawImage(image, 0, 0, null);
          }
        }
      };
      private Dumbo2B dumbo = new Dumbo2B();
      
      public void init() {
        try {
          javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
              createGUI();
            }
          });
        } catch (Exception e) {
          System.err.println("createGUI didn't successfully complete");
        }
      }
    
      private void myPaint(Graphics g) {
        List<Point2D> pointsA = dumbo.getPointsA();
        List<Point2D> pointsB = dumbo.getPointsB();
        g.setColor(Color.blue);
        for (int i = 0; i < dumbo.getPointsA().size() - 1; i++) {
          Point2D pA0 = pointsA.get(i);
          Point2D pA1 = pointsA.get((i + 1) % pointsA.size());
          int xA0 = (int)(SCALE * pA0.getX());
          int yA0 = (int)(SCALE * pA0.getY());
          int xA1 = (int)(SCALE * pA1.getX());
          int yA1 = (int)(SCALE * pA1.getY());
          Point2D pB0 = pointsB.get(i);
          Point2D pB1 = pointsB.get((i + 1) % pointsB.size());
          int xB0 = (int)(SCALE * pB0.getX());
          int yB0 = (int)(SCALE * pB0.getY());
          int xB1 = (int)(SCALE * pB1.getX());
          int yB1 = (int)(SCALE * pB1.getY());
          
          g.drawLine(xA0, yA0, xA1, yA1);
          g.drawLine(xB0, yB0, xB1, yB1);
        }
      }
    
      private void createGUI() {
        setLayout(new BorderLayout());
        add(panel);
        //panel.setPreferredSize(new Dimension(200, 200));
        System.out.println(panel.getSize());
        Graphics g = image.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, SIDE, SIDE);
        myPaint(g);
        g.dispose();
      }
    }

  7. #7
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    For what it may be worth, here is another example.
    This application displays a window.
    It can be resized with dragging or either of two buttons.
    The resize code is in the method resizeButtons();
    but it also resizes the circle.

    Java Code:
    /**
     * Demonstrate resizing.
     * The window displays a circle and two buttons.
     * The "eat me" button expands the window.
     * The "drink me" button shrinks the window.
     * When the window changes size due to either the buttons or user action,
     * The buttons and circle are adjusted accordingly.
     */
    
    
    package Test;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class DrinkMe  extends JPanel {
    
    	// the window
    	Window winFred;
    
    	// the contents of the window
    	JButton eatMeButton, drinkMeButton;
    	Circle circ;
    
    	final static Dimension BUTTON_SIZE = new Dimension(91,25);
    	final static Dimension WINDOW_SIZE = new Dimension(260,50);
    
    	public DrinkMe() {
    		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    
    		// sandwich the buttonBox between two layers of stretchy glue
    		Box buttonBox = Box.createHorizontalBox();
    		add(Box.createVerticalGlue());
    		add(buttonBox);
    		add(Box.createVerticalGlue());
    		
    		eatMeButton = new JButton("eat me");
    		eatMeButton.setPreferredSize(BUTTON_SIZE);
    		drinkMeButton = new JButton("drink me");
    		drinkMeButton.setPreferredSize(BUTTON_SIZE);
    		circ = new Circle();
    		circ.setPreferredSize(new Dimension(BUTTON_SIZE.height, BUTTON_SIZE.height));
    
    		eatMeButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) { windowResize(1.25); }			
    		});
    		drinkMeButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) { windowResize(.75); }			
    		});
    		
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(circ);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(eatMeButton);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(drinkMeButton);
    		buttonBox.add(Box.createHorizontalGlue());
    
    		// listen for window size changes, especially fro user action
    		addComponentListener(new ComponentAdapter() {
    			@Override
    			public void componentResized(ComponentEvent e) {
    				resizeButtons(winFred.getWidth(), winFred.getHeight());
    			}
    		});
    	}
    
    	@Override
    	public void addNotify() {
    		super.addNotify();
    		// get the window as the whole thing is first displayed
    		winFred = SwingUtilities.getWindowAncestor(this);
    	}
    
    	// resize buttons as  BUTTON_SIZE * (myWin.getSize()/ WINDOW_SIZE)
    	public void resizeButtons(int winW, int winH) {
    		Dimension newSize = new Dimension(
    				BUTTON_SIZE.width * winW/WINDOW_SIZE.width,
    				BUTTON_SIZE.height * winH/WINDOW_SIZE.height);
    		eatMeButton.setPreferredSize(newSize);
    		drinkMeButton.setPreferredSize(newSize);
    		eatMeButton.setMinimumSize(newSize);
    		drinkMeButton.setMinimumSize(newSize);
    		// only the following two actually resize the windows (seems to be a bug)
    		eatMeButton.setMaximumSize(newSize);
    		drinkMeButton.setMaximumSize(newSize);
    		circ.setMinimumSize(new Dimension(newSize.height, newSize.height));
    		circ.setPreferredSize(new Dimension(newSize.height, newSize.height));
    		circ.setMaximumSize(new Dimension(newSize.height, newSize.height));
    	}
    
    	// called when buttons are pressed
    	public void windowResize(double factor) {
    		int w = (int) (winFred.getWidth() * factor);
    		int h = (int) (winFred.getHeight() * factor);
    		winFred.setSize(w, h);
    		resizeButtons(w, h);
    	}
    
    	// the circle
    	final static int thick = 3;
    	final static Stroke CIRCLE_PEN = new BasicStroke((float)3);
    	class Circle extends JPanel {
    		@Override
    		public void paintComponent(Graphics g) {
    			int side = getHeight()-2*thick;
    			Graphics2D gg = (Graphics2D)g;
    			gg.setStroke(CIRCLE_PEN);
    			gg.setColor(Color.BLUE);
    			gg.drawOval(thick, thick, side, side);
    		}
    	}
    
        private static void createAndShowGUI() {
            // Create and set up the window.
            JFrame frame = new JFrame("DrinkMe");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // Create and set up the content pane
            JComponent newContentPane = new DrinkMe();
            frame.setContentPane(newContentPane);
    
            // Display the window.
            frame.pack();
            frame.setVisible(true);
        }
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

  8. #8
    chappa is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default

    Hi and thanks to both of you.



    I have compiled your code Fubarable, however I have 2 problems with it:


    it's an applet now and all I get is 1/4th of a circle.


    Maybe it would be easier to downscale the grid of the window frame e.g. that instead of 1 pixel per 1 pixel in the frame it would show 2 pixel per 1 pixel. However I have no clue how I should implement that...


    Hope somebody can help me out again :(

  9. #9
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    First you need to know that the Graphics argument to paint methods
    can be cast to a Graphics2D object (which is what it really is).

    Second you need to get some understanding of affine transformations.
    This book chapter may be more than you want.
    This tutorial may not be enough.


    Then you write code using affine transformations to size, rotate, shift, and squash visible objects.
    See the JavaDocs.

  10. #10
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    To demonstrate AffineTransform, I've converted my former demo program.

    All the work takes place inside paintComponent.
    The transform is computed to scale the image
    from the nominal size of 10 to the actual size:
    Java Code:
    	double factor = 1.0*Math.min(getWidth(), getHeight())/NOMINAL_SIDE;
    	AffineTransform transform = AffineTransform.getScaleInstance(factor, factor);
    and a bit later the transform is installed with:
    Java Code:
    	gg.transform(transform);
    At least one example on the web uses setTransform() instead of transform().
    This is Wrong; there is already a transform in place to relocate the object.
    Use gg.transform instead of gg.setTransform
    to build on the existing transform instead of replacing it.


    Java Code:
    /**
     * Demonstrate resizing.
     * The window displays a circle and two buttons.
     * The "eat me" button expands the window.
     * The "drink me" button shrinks the window.
     * When the window changes size due to either the buttons or user action,
     * The buttons and circle are adjusted accordingly.
     *
     * This version uses an AffineTransform to resize the circle.
     * Nominally, the circle is always drawn to the same size.
     */
    
    package Test;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;
    import javax.swing.*;
    
    public class DrinkMe  extends JPanel {
    
    	// the window
    	Window winFred;
    
    	// the contents of the window
    	JButton eatMeButton, drinkMeButton;
    	Circle circ;
    
    	final static Dimension BUTTON_SIZE = new Dimension(91,25);
    	final static Dimension WINDOW_SIZE = new Dimension(270,80);
    
    	public DrinkMe() {
    		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    
    		// sandwich the buttonBox between two layers of stretchy glue
    		Box buttonBox = Box.createHorizontalBox();
    		add(Box.createVerticalGlue());
    		add(buttonBox);
    		add(Box.createVerticalGlue());
    		
    		eatMeButton = new JButton("eat me");
    		eatMeButton.setPreferredSize(BUTTON_SIZE);
    		drinkMeButton = new JButton("drink me");
    		drinkMeButton.setPreferredSize(BUTTON_SIZE);
    		circ = new Circle(BUTTON_SIZE.height);
    
    		eatMeButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) { windowResize(1.25); }			
    		});
    		drinkMeButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) { windowResize(.75); }			
    		});
    		
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(circ);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(eatMeButton);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(drinkMeButton);
    		buttonBox.add(Box.createHorizontalGlue());
    
    		// listen for window size changes, especially fro user action
    		addComponentListener(new ComponentAdapter() {
    			@Override
    			public void componentResized(ComponentEvent e) {
    				resizeButtons(winFred.getWidth(), winFred.getHeight());
    			}
    		});
    	}
    
    	@Override
    	public void addNotify() {
    		super.addNotify();
    		// get the window as the whole thing is first displayed
    		winFred = SwingUtilities.getWindowAncestor(this);
    	}
    
    	// resize buttons as  BUTTON_SIZE * (myWin.getSize()/ WINDOW_SIZE)
    	public void resizeButtons(int winW, int winH) {
    		Dimension newSize = new Dimension(
    				BUTTON_SIZE.width * winW/WINDOW_SIZE.width,
    				BUTTON_SIZE.height * winH/WINDOW_SIZE.height);
    		eatMeButton.setPreferredSize(newSize);
    		drinkMeButton.setPreferredSize(newSize);
    		eatMeButton.setMinimumSize(newSize);
    		drinkMeButton.setMinimumSize(newSize);
    		// only the following two actually resize the windows (seems to be a bug)
    		eatMeButton.setMaximumSize(newSize);
    		drinkMeButton.setMaximumSize(newSize);
    		circ.setSide(Math.min(newSize.width, newSize.height));
    	}
    
    	// called when buttons are pressed
    	public void windowResize(double factor) {
    		int w = (int) (winFred.getWidth() * factor);
    		int h = (int) (winFred.getHeight() * factor);
    		winFred.setSize(w, h);
    		resizeButtons(w, h);
    	}
    
    	// the circle
    	final static int PEN_W = 2;
    	final static Stroke CIRCLE_PEN = new BasicStroke((float)PEN_W);
    	final static int NOMINAL_SIDE = 10;
    	static class Circle extends JPanel {
    		
    		public Circle(int s) { setSide(s); }
    		
    		public void setSide(int side) {
    			Dimension cDim = new Dimension(side, side);
    			setMinimumSize(cDim);
    			setPreferredSize(cDim);
    			setMaximumSize(cDim);
    		}
    
    		@Override
    		public void paintComponent(Graphics g) {
    			double factor = 1.0*Math.min(getWidth(), getHeight())/NOMINAL_SIDE;
    			AffineTransform transform = AffineTransform.getScaleInstance(factor, factor);
    			int topLeft = PEN_W;
    			int length = NOMINAL_SIDE -2*PEN_W;
    			g.setColor(Color.red);
    			g.drawRect(0, 0, getWidth()-1, getHeight()-1);
    			Graphics2D gg = (Graphics2D)g;
    //			gg.setTransform(transform);  // no, no, no; must not replace the existing transform
    			gg.transform(transform);
    			gg.setStroke(CIRCLE_PEN);
    			gg.setColor(Color.BLUE);
    			gg.drawOval(topLeft, topLeft, length, length);
    		}
    	}
    
        private static void createAndShowGUI() {
            // Create and set up the window.
            JFrame frame = new JFrame("DrinkMe");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // Create and set up the content pane
            JComponent newContentPane = new DrinkMe();
            frame.setContentPane(newContentPane);
    
            // Display the window.
    		frame.setPreferredSize(WINDOW_SIZE);
            frame.pack();
            frame.setVisible(true);
        }
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

  11. #11
    chappa is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default

    Hi zweibieren,


    thank you very much.


    I have know two approaches to this problem:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class RectangleProgram extends JFrame
    {
        private static final int WIDTH = 400;
        private static final int HEIGHT = 300;
       
        private JLabel lengthL, widthL, areaL;
        private JTextField lengthTF, widthTF, areaTF;
        private JButton calculateB, exitB;
       
        //Button handlers:
        private CalculateButtonHandler cbHandler;
        private ExitButtonHandler ebHandler;
       
        public RectangleProgram()
        {
            lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);
            widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);
            areaL = new JLabel("Area: ", SwingConstants.RIGHT);
           
            lengthTF = new JTextField(10);
            widthTF = new JTextField(10);
            areaTF = new JTextField(10);
           
            //SPecify handlers for each button and add (register) ActionListeners to each button.
            calculateB = new JButton("Calculate");
            cbHandler = new CalculateButtonHandler();
            calculateB.addActionListener(cbHandler);
            exitB = new JButton("Exit");
            ebHandler = new ExitButtonHandler();
            exitB.addActionListener(ebHandler);
           
            setTitle("Sample Title: Area of a Rectangle");
            Container pane = getContentPane();
            pane.setLayout(new GridLayout(4, 2));
           
            //Add things to the pane in the order you want them to appear (left to right, top to bottom)
            pane.add(lengthL);
            pane.add(lengthTF);
            pane.add(widthL);
            pane.add(widthTF);
            pane.add(areaL);
            pane.add(areaTF);
            pane.add(calculateB);
            pane.add(exitB);
           
            setSize(WIDTH, HEIGHT);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
       
        private class CalculateButtonHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {	
    	double length, width, area;
                length = Double.parseDouble(lengthTF.getText()); //We use the getText & setText methods to manipulate the data entered into those fields.
                width = Double.parseDouble(widthTF.getText());
                area = length * width;
                areaTF.setText("" + area);
            }
        }
       
        public class ExitButtonHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        }
    	public void paint(Graphics g) {
        	 int m = 200000;
    	  	 int m1 = 6000;
    	  	 int m2 = 8000;
    	  	 double gamma = 6.67;
    	  	 double r = 15;
    	  	 double r2 = 20;
    	  	 double vx = 0;
    	  	 double vx2 = 0;
    	  	 double alpha = 0;
    	  	 double alpha2 = 0;
    	  	 double alpha3 = 0;
    	  	 double alpha4 = 0;
    	  	 double x = r;
    	  	 double x2 = r2;
    	  	 double y = 0;
    	  	 double y2 = 0;
    	  	 int b = 450;
    	  	 int h = 450;
    	  	 double mittelpunktx = b/2;
    	  	 double mittelpunktx2 = b/2;
    	  	 double mittelpunkty = h/2;
    	  	 double mittelpunkty2 = h/2;
    	  	 double vy = 0.0001 * Math.sqrt(1000 * gamma * m / r);
    	  	 double vy2 = 0.0001 * Math.sqrt(1000 * gamma * m / r2);
    	  	 double tend = 10000;
    	  	 double t = 0;
    	  	 double pi = 4*Math.atan(1);
    	  	 double deltat = 0.01;
    	  	 double result = 0;
    
    
    
    		 while (t <= tend) {
    	    	 t = t + deltat;
    	    	 r = Math.sqrt(x * x + y * y);
    		 r2 = Math.sqrt(x2 * x2 + y2 * y2);
    
    		 double posx = mittelpunktx + x;
    	    	 double posy = mittelpunkty + y;
    	    	 double posx2 = mittelpunktx2 + x2;
    	    	 double posy2 = mittelpunkty2 + y2;
    		 double poswechselx = posx2 - posx;
    		 double poswechsely = posy2 - posy;
    		 double rwechselq = poswechselx * poswechselx + poswechsely * poswechsely;
    	
    
    
    	    	 double a = 0.00001 * gamma * ((m /r) / r);
    	    	 double a2 = 0.00001 * gamma * ((m /r2) / r2);
    		 double avon1auf2 = 0.00001 * gamma * (m2 / rwechselq);
    		 double avon2auf1 = 0.00001 * gamma * (m1 / rwechselq);
    	    	 alpha = winkel(x,y,alpha);
    	    	 alpha2 = winkel(x2,y2,alpha2);
    			 alpha3 = winkel(poswechselx, poswechsely,alpha3);
    			 alpha4 = alpha3 + pi;
    
    	    	 double ax = a * Math.cos(alpha);
    	    	 double ay = a * Math.sin(alpha);
    	    	 double ax2 = a2 * Math.cos(alpha2);
    	    	 double ay2 = a2 * Math.sin(alpha2);
    			 double ax3 = avon1auf2 * Math.cos(alpha3);
    			 double ay3 = avon1auf2 * Math.sin(alpha3);
    			 double ax4 = avon2auf1 * Math.cos(alpha4);
    			 double ay4 = avon2auf1 * Math.sin(alpha4);
    			 ax=ax+ax3;
    			 ay = ay+ay3;
    			 ax2=ax2+ax4;
    			 ay2 = ay2+ay4;
    	    	 vx = vx - ax * deltat;
    	    	 vy = vy - ay * deltat;
    	    	 x = x + vx * deltat;
    	    	 y = y + vy * deltat;
    		 vx2 = vx2 - ax2 * deltat;
    	    	 vy2 = vy2 - ay2 * deltat;
    	    	 x2 = x2 + vx2 * deltat;
    	    	 y2 = y2 + vy2 * deltat;
    	
    		//	g.drawLine((int)(posx),(int)(posy),(int)(posx),(int)(posy));
    		//	g.drawLine((int)(posx2),(int)(posy2),(int)(posx2),(int)(posy2)); 
    			}
    		}
    		
       	public static double winkel(double xposition, double yposition, double result){
    		result = Math.atan(yposition / xposition);
    		double pi = 4*Math.atan(1);
      		if	((xposition < 0) && (yposition < 0)) {
    		result = result + pi;
    		} 
    		if ((xposition < 0) && (yposition > 0)){
    		result = result + pi;
    		}
    		if ((xposition > 0) && (yposition < 0)) {
    		result = result + 2*pi;
    		}
    		return result;
    		}
        public static void main(String[] args)
        {
            RectangleProgram rectObj = new RectangleProgram();
        }
       
    }
    I found this code and I edited my calculations into it, however I didn't figure how to make it paint my points. How do I paint my "graph" into this (preferably with scaling, as the whole window, button etc. is scalable already.)...



    Problem with the code below (zweibieren's code) is again that I cannot figure out how to make it calculate and draw my points. (The calculations seem to work, however I cannot make it draw it).


    Java Code:
     * Demonstrate resizing.
     * The window displays a circle and two buttons.
     * The "eat me" button expands the window.
     * The "drink me" button shrinks the window.
     * When the window changes size due to either the buttons or user action,
     * The buttons and circle are adjusted accordingly.
     *
     * This version uses an AffineTransform to resize the circle.
     * Nominally, the circle is always drawn to the same size.
     */
    
    //package Test;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;
    import javax.swing.*;
    
    public class DrinkMe  extends JPanel {
    
    	// the window
    	Window winFred;
    
    	// the contents of the window
    	JButton eatMeButton, drinkMeButton;
    	Circle circ;
    
    	final static Dimension BUTTON_SIZE = new Dimension(91,25);
    	final static Dimension WINDOW_SIZE = new Dimension(270,80);
    
    	public DrinkMe() {
    		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    
    		// sandwich the buttonBox between two layers of stretchy glue
    		Box buttonBox = Box.createHorizontalBox();
    		add(Box.createVerticalGlue());
    		add(buttonBox);
    		add(Box.createVerticalGlue());
    		
    		eatMeButton = new JButton("eat me");
    		eatMeButton.setPreferredSize(BUTTON_SIZE);
    		drinkMeButton = new JButton("drink me");
    		drinkMeButton.setPreferredSize(BUTTON_SIZE);
    		circ = new Circle(BUTTON_SIZE.height);
    
    		eatMeButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) { windowResize(1.25); }			
    		});
    		drinkMeButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) { windowResize(.75); }			
    		});
    		
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(circ);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(eatMeButton);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(drinkMeButton);
    		buttonBox.add(Box.createHorizontalGlue());
    
    		// listen for window size changes, especially fro user action
    		addComponentListener(new ComponentAdapter() {
    			@Override
    			public void componentResized(ComponentEvent e) {
    				resizeButtons(winFred.getWidth(), winFred.getHeight());
    			}
    		});
    	}
    
    	@Override
    	public void addNotify() {
    		super.addNotify();
    		// get the window as the whole thing is first displayed
    		winFred = SwingUtilities.getWindowAncestor(this);
    	}
    
    	// resize buttons as  BUTTON_SIZE * (myWin.getSize()/ WINDOW_SIZE)
    	public void resizeButtons(int winW, int winH) {
    		Dimension newSize = new Dimension(
    				BUTTON_SIZE.width * winW/WINDOW_SIZE.width,
    				BUTTON_SIZE.height * winH/WINDOW_SIZE.height);
    		eatMeButton.setPreferredSize(newSize);
    		drinkMeButton.setPreferredSize(newSize);
    		eatMeButton.setMinimumSize(newSize);
    		drinkMeButton.setMinimumSize(newSize);
    		// only the following two actually resize the windows (seems to be a bug)
    		eatMeButton.setMaximumSize(newSize);
    		drinkMeButton.setMaximumSize(newSize);
    		circ.setSide(Math.min(newSize.width, newSize.height));
    	}
    
    	// called when buttons are pressed
    	public void windowResize(double factor) {
    		int w = (int) (winFred.getWidth() * factor);
    		int h = (int) (winFred.getHeight() * factor);
    		winFred.setSize(w, h);
    		resizeButtons(w, h);
    	}
    
    	// the circle
    	final static int PEN_W = 2;
    	final static Stroke CIRCLE_PEN = new BasicStroke((float)PEN_W);
    	final static int NOMINAL_SIDE = 10;
    	static class Circle extends JPanel {
    		public Circle(int s) { setSide(s);} 
    		public void setSide(int side) {
    			Dimension cDim = new Dimension(side, side);
    			setMinimumSize(cDim);
    			setPreferredSize(cDim);
    			setMaximumSize(cDim);
    		}
    
    		@Override
    		public void paintComponent(Graphics g) {
    					
    	 int m = 200000;
      int m1 = 6000;
      int m2 = 8000;
      double gamma = 6.67;
      double r = 15;
      double r2 = 20;
      double vx = 0;
      double vx2 = 0;
      double alpha = 0;
      double alpha2 = 0;
      double alpha3 = 0;
      double alpha4 = 0;
      double x = r;
      double x2 = r2;
      double y = 0;
      double y2 = 0;
      int b = 450;
      int h = 450;
      double mittelpunktx = b/2;
      double mittelpunktx2 = b/2;
      double mittelpunkty = h/2;
      double mittelpunkty2 = h/2;
      double vy = 0.0001 * Math.sqrt(1000 * gamma * m / r);
      double vy2 = 0.0001 * Math.sqrt(1000 * gamma * m / r2);
      double tend = 10000;
      double t = 0;
      double pi = 4*Math.atan(1);
      double deltat = 0.01;
      double result = 0;
    
      
     
    while (t <= tend) {
        t = t + deltat;
        r = Math.sqrt(x * x + y * y);
    	r2 = Math.sqrt(x2 * x2 + y2 * y2);
    	
    	double posx = mittelpunktx + x;
        double posy = mittelpunkty + y;
        double posx2 = mittelpunktx2 + x2;
        double posy2 = mittelpunkty2 + y2;
    	double poswechselx = posx2 - posx;
    	double poswechsely = posy2 - posy;
    	double rwechselq = poswechselx * poswechselx + poswechsely * poswechsely;
    //	System.out.println(poswechselx + " " + poswechsely);
    
    
        double a = 0.00001 * gamma * ((m /r) / r);
        double a2 = 0.00001 * gamma * ((m /r2) / r2);
    	double avon1auf2 = 0.00001 * gamma * (m2 / rwechselq);
    	double avon2auf1 = 0.00001 * gamma * (m1 / rwechselq);
        alpha = winkel(x,y,alpha);
        alpha2 = winkel(x2,y2,alpha2);
    	alpha3 = winkel(poswechselx, poswechsely,alpha3);
    	alpha4 = alpha3 + pi;
    
        double ax = a * Math.cos(alpha);
        double ay = a * Math.sin(alpha);
        double ax2 = a2 * Math.cos(alpha2);
        double ay2 = a2 * Math.sin(alpha2);
    	double ax3 = avon1auf2 * Math.cos(alpha3);
    	double ay3 = avon1auf2 * Math.sin(alpha3);
    	double ax4 = avon2auf1 * Math.cos(alpha4);
    	double ay4 = avon2auf1 * Math.sin(alpha4);
    	ax=ax+ax3;
    	ay = ay+ay3;
    	ax2=ax2+ax4;
    	ay2 = ay2+ay4;
        vx = vx - ax * deltat;
        vy = vy - ay * deltat;
        x = x + vx * deltat;
        y = y + vy * deltat;
    	vx2 = vx2 - ax2 * deltat;
        vy2 = vy2 - ay2 * deltat;
        x2 = x2 + vx2 * deltat;
        y2 = y2 + vy2 * deltat;
    
    			double factor = 1.0*Math.min(getWidth(), getHeight())/NOMINAL_SIDE;
    			AffineTransform transform = AffineTransform.getScaleInstance(factor, factor);
    			int topLeft = PEN_W;
    			int length = NOMINAL_SIDE -2*PEN_W;
    			g.setColor(Color.red);
    			g.drawRect(0, 0, getWidth()-1, getHeight()-1);
    			Graphics2D gg = (Graphics2D)g;
    //			gg.setTransform(transform);  // no, no, no; must not replace the existing transform
    			gg.transform(transform);
    			gg.setStroke(CIRCLE_PEN);
    			gg.setColor(Color.BLUE);
    			gg.drawOval(topLeft, topLeft, length, length);
    		}
    		}}
     public static double winkel(double xposition, double yposition, double result){
    	result = Math.atan(yposition / xposition);
    	double pi = 4*Math.atan(1);
      	if	((xposition < 0) && (yposition < 0)) {
    		result = result + pi;
    	} 
    	if ((xposition < 0) && (yposition > 0)){
    		result = result + pi;
    	}
    	if ((xposition > 0) && (yposition < 0)) {
    		result = result + 2*pi;
    	}
    	return result;
    		}
    
        private static void createAndShowGUI() {
            // Create and set up the window.
            JFrame frame = new JFrame("DrinkMe");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // Create and set up the content pane
            JComponent newContentPane = new DrinkMe();
            frame.setContentPane(newContentPane);
    
            // Display the window.
    		frame.setPreferredSize(WINDOW_SIZE);
            frame.pack();
            frame.setVisible(true);
        }
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

    Help would be greatly appreciated :o

  12. #12
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    AffineTransforms in drawing convert coordinates in a "drawing space" into the screen coordinates.
    A client is written to draw with a particular drawing space, say 100x100.
    Then just before doing the drawing,
    the transform is set to map from that 100x100 space to the screen space.
    In DrawMe, the drawing space is 10x10.
    And the affine transform converts to the actual screen space.

    To use the DrawMe code directly for plotting points,
    make sure the point coordinates are in the range 0...NOMINAL_SIDE.
    Do all the drawing in place of the one line:
    Java Code:
    	gg.drawOval(topLeft, topLeft, length, length);
    Note that the symbol that you plot at each point
    will expand or diminish along with the scaling of the points.
    That is the way Graphics2D implements AffineTransforms.
    So if the drawing space is 100x100 and the scrren is 250x250, then
    the point plots will be 2.5 times bigger than on a 100x100 suerface.

    Incidentally, drawing a line of zero length may not result in any pixels being painted.
    It may be better to fill a rectange:
    Java Code:
        fillRect(x,y,1,1);

    ------

    Now in order to make the coordinates of points fit in a 100x100 (or other) space,
    it may be necessary to multiply each value by an appropriate factor.
    If so, you could choose that factor to map to the available screen space.
    And then there would be no need for the AffineTransform.

  13. #13
    chappa is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default

    Hi zweibieren, thanks a lot!


    So I tried these, none working (just shows a red rectangle always :mad:) and the programme is REALLY "slow" :


    fillrect version:

    Java Code:
    			double factor = 1.0*Math.min(getWidth(), getHeight())/NOMINAL_SIDE;
    			AffineTransform transform = AffineTransform.getScaleInstance(factor, factor);
    			int topLeft = PEN_W;
    			int length = NOMINAL_SIDE -2*PEN_W;
    			//g.setColor(Color.red);
    			//g.drawRect(0, 0, getWidth()-1, getHeight()-1);
    			g.setColor(Color.BLUE);
    			Graphics2D gg = (Graphics2D)g;
    //			gg.setTransform(transform);  // no, no, no; must not replace the existing transform
    			gg.transform(transform);
    			gg.setStroke(CIRCLE_PEN);
    			gg.setColor(Color.BLUE);
    			fillRect(posx,posy,1,1);
    		}
    		}


    gg.drawLine version:

    Java Code:
    			double factor = 1.0*Math.min(getWidth(), getHeight())/NOMINAL_SIDE;
    			AffineTransform transform = AffineTransform.getScaleInstance(factor, factor);
    			int topLeft = PEN_W;
    			int length = NOMINAL_SIDE -2*PEN_W;
    			//g.setColor(Color.red);
    			//g.drawRect(0, 0, getWidth()-1, getHeight()-1);
    			g.setColor(Color.BLUE);
    			Graphics2D gg = (Graphics2D)g;
    //			gg.setTransform(transform);  // no, no, no; must not replace the existing transform
    			gg.transform(transform);
    			gg.setStroke(CIRCLE_PEN);
    			gg.setColor(Color.BLUE);
    			gg.drawLine(posx, posy, posx, posy);
    		}
    		}

  14. #14
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    1. Let me stress:
    Do all the drawing in place of the one line:
    gg.drawOval(topLeft, topLeft, length, length);
    Do not repeatedly execute the other lines. Do them once.
    Put the entire drawing loop in place of the one line.

    2. Make sure that all points plotted have coordinates in the range 0 ... NOMINAL SIDE.

  15. #15
    chappa is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default

    Hi zweibieren (and thank you again very very much),


    I can see the "painting" now. However it will not show me the "process" of painting, (I want to see every point being drawn as in the original programme :() and also it should begin scaled up already so that one can actually follow the traces of the planets. (The scaling does still not work out for me...)


    Java Code:
    /**
     * Demonstrate resizing.
     * The window displays a circle and two buttons.
     * The "eat me" button expands the window.
     * The "drink me" button shrinks the window.
     * When the window changes size due to either the buttons or user action,
     * The buttons and circle are adjusted accordingly.
     *
     * This version uses an AffineTransform to resize the circle.
     * Nominally, the circle is always drawn to the same size.
     */
    
    //package Test;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;
    import javax.swing.*;
    
    public class DrinkMe4  extends JPanel {
    
    	// the window
    	Window winFred;
    
    	// the contents of the window
    	JButton eatMeButton, DrinkMe4Button;
    	Circle circ;
    
    	final static Dimension BUTTON_SIZE = new Dimension(45,25);
    	final static Dimension WINDOW_SIZE = new Dimension(350,350);
    
    	public DrinkMe4() {
    		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    
    		// sandwich the buttonBox between two layers of stretchy glue
    		Box buttonBox = Box.createHorizontalBox();
    		add(Box.createVerticalGlue());
    		add(buttonBox);
    		add(Box.createVerticalGlue());
    		
    		eatMeButton = new JButton("eat me");
    		eatMeButton.setPreferredSize(BUTTON_SIZE);
    		DrinkMe4Button = new JButton("drink me");
    		DrinkMe4Button.setPreferredSize(BUTTON_SIZE);
    		circ = new Circle(BUTTON_SIZE.height);
    
    		eatMeButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) { windowResize(1.25); }			
    		});
    		DrinkMe4Button.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) { windowResize(.75); }			
    		});
    		
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(circ);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(eatMeButton);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(DrinkMe4Button);
    		buttonBox.add(Box.createHorizontalGlue());
    
    		// listen for window size changes, especially fro user action
    		addComponentListener(new ComponentAdapter() {
    			@Override
    			public void componentResized(ComponentEvent e) {
    				resizeButtons(winFred.getWidth(), winFred.getHeight());
    			}
    		});
    	}
    
    	@Override
    	public void addNotify() {
    		super.addNotify();
    		// get the window as the whole thing is first displayed
    		winFred = SwingUtilities.getWindowAncestor(this);
    	}
    
    	// resize buttons as  BUTTON_SIZE * (myWin.getSize()/ WINDOW_SIZE)
    	public void resizeButtons(int winW, int winH) {
    		Dimension newSize = new Dimension(
    				BUTTON_SIZE.width * winW/WINDOW_SIZE.width,
    				BUTTON_SIZE.height * winH/WINDOW_SIZE.height);
    		eatMeButton.setPreferredSize(newSize);
    		DrinkMe4Button.setPreferredSize(newSize);
    		eatMeButton.setMinimumSize(newSize);
    		DrinkMe4Button.setMinimumSize(newSize);
    		// only the following two actually resize the windows (seems to be a bug)
    		eatMeButton.setMaximumSize(newSize);
    		DrinkMe4Button.setMaximumSize(newSize);
    		circ.setSide(Math.min(newSize.width, newSize.height));
    	}
    
    	// called when buttons are pressed
    	public void windowResize(double factor) {
    		int w = (int) (winFred.getWidth() * factor);
    		int h = (int) (winFred.getHeight() * factor);
    		winFred.setSize(w, h);
    		resizeButtons(w, h);
    	}
    
    	// the circle
    	final static int PEN_W = 2;
    	final static Stroke CIRCLE_PEN = new BasicStroke((float)PEN_W);
    	final static int NOMINAL_SIDE = 100;
    	static class Circle extends JPanel {
    		public Circle(int s) { setSide(s);} 
    		public void setSide(int side) {
    			Dimension cDim = new Dimension(side, side);
    			setMinimumSize(cDim);
    			setPreferredSize(cDim);
    			setMaximumSize(cDim);
    		}
    
    		@Override
    		public void paintComponent(Graphics g) {
    					
      int m = 200000;
      int m1 = 6000;
      int m2 = 8000;
      double gamma = 6.67;
      double r = 15;
      double r2 = 20;
      double vx = 0;
      double vx2 = 0;
      double alpha = 0;
      double alpha2 = 0;
      double alpha3 = 0;
      double alpha4 = 0;
      double x = r;
      double x2 = r2;
      double y = 0;
      double y2 = 0;
      int b = 100;
      int h = 100;
      double mittelpunktx = b/2;
      double mittelpunktx2 = b/2;
      double mittelpunkty = h/2;
      double mittelpunkty2 = h/2;
      double vy = 0.0001 * Math.sqrt(1000 * gamma * m / r);
      double vy2 = 0.0001 * Math.sqrt(1000 * gamma * m / r2);
      double tend = 10000;
      double t = 0;
      double pi = 4*Math.atan(1);
      double deltat = 0.01;
      double result = 0;
    
      
     
    
    			double factor = 1.0*Math.min(getWidth(), getHeight())/NOMINAL_SIDE;
    			AffineTransform transform = AffineTransform.getScaleInstance(factor, factor);
    			int topLeft = PEN_W;
    			int length = NOMINAL_SIDE -2*PEN_W;
    			//g.setColor(Color.red);
    			//g.drawRect(0, 0, getWidth()-1, getHeight()-1);
    			//g.setColor(Color.BLUE);
    			//g.drawRect(posx, posy, posx, posy);
    			Graphics2D gg = (Graphics2D)g;
    //			gg.setTransform(transform);  // no, no, no; must not replace the existing transform
    			gg.transform(transform);
    			gg.setStroke(CIRCLE_PEN);
    			gg.setColor(Color.BLUE);
    			while (t <= tend) {
    			    t = t + deltat;
    			    r = Math.sqrt(x * x + y * y);
    				r2 = Math.sqrt(x2 * x2 + y2 * y2);
    
    				double posx = mittelpunktx + x;
    			    double posy = mittelpunkty + y;
    			    double posx2 = mittelpunktx2 + x2;
    			    double posy2 = mittelpunkty2 + y2;
    				double poswechselx = posx2 - posx;
    				double poswechsely = posy2 - posy;
    				double rwechselq = poswechselx * poswechselx + poswechsely * poswechsely;
    			//	System.out.println(poswechselx + " " + poswechsely);
    
    
    			    double a = 0.00001 * gamma * ((m /r) / r);
    			    double a2 = 0.00001 * gamma * ((m /r2) / r2);
    				double avon1auf2 = 0.00001 * gamma * (m2 / rwechselq);
    				double avon2auf1 = 0.00001 * gamma * (m1 / rwechselq);
    			    alpha = winkel(x,y,alpha);
    			    alpha2 = winkel(x2,y2,alpha2);
    				alpha3 = winkel(poswechselx, poswechsely,alpha3);
    				alpha4 = alpha3 + pi;
    
    			    double ax = a * Math.cos(alpha);
    			    double ay = a * Math.sin(alpha);
    			    double ax2 = a2 * Math.cos(alpha2);
    			    double ay2 = a2 * Math.sin(alpha2);
    				double ax3 = avon1auf2 * Math.cos(alpha3);
    				double ay3 = avon1auf2 * Math.sin(alpha3);
    				double ax4 = avon2auf1 * Math.cos(alpha4);
    				double ay4 = avon2auf1 * Math.sin(alpha4);
    				ax=ax+ax3;
    				ay = ay+ay3;
    				ax2=ax2+ax4;
    				ay2 = ay2+ay4;
    			    vx = vx - ax * deltat;
    			    vy = vy - ay * deltat;
    			    x = x + vx * deltat;
    			    y = y + vy * deltat;
    				vx2 = vx2 - ax2 * deltat;
    			    vy2 = vy2 - ay2 * deltat;
    			    x2 = x2 + vx2 * deltat;
    			    y2 = y2 + vy2 * deltat;
    			
    			
    				gg.setColor(Color.BLUE);
    				gg.fillRect((int)(posx),(int) (posy), 1,1);
    				gg.setColor(Color.YELLOW);
    				gg.fillRect((int)(posx2),(int) (posy2),1,1);
    					}
    				}
    			}
     public static double winkel(double xposition, double yposition, double result){
    	result = Math.atan(yposition / xposition);
    	double pi = 4*Math.atan(1);
      	if	((xposition < 0) && (yposition < 0)) {
    		result = result + pi;
    	} 
    	if ((xposition < 0) && (yposition > 0)){
    		result = result + pi;
    	}
    	if ((xposition > 0) && (yposition < 0)) {
    		result = result + 2*pi;
    	}
    	return result;
    		}
    
        private static void createAndShowGUI() {
            // Create and set up the window.
            JFrame frame = new JFrame("DrinkMe4");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // Create and set up the content pane
            JComponent newContentPane = new DrinkMe4();
            frame.setContentPane(newContentPane);
    
            // Display the window.
    		frame.setPreferredSize(WINDOW_SIZE);
            frame.pack();
            frame.setVisible(true);
        }
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

  16. #16
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    However it will not show me the "process" of painting, (I want to see every point being drawn as in the original programme )
    This is what Fubarable has been showing you, as he said

    Here I would use one class to create two lists: List<Point2D> and then the GUI class to display them. In the GUI class, I'd use a multiplier to scale the data to the graphics.
    To get progressive painting, one more point each time, use javax.swing.Timer
    class Mj2b needs a global variable, nPointsShowing, to say how many points are displayed.
    It is initialized to zero.
    The paint method plots only points 0 ... nPointsShowing.
    The timer run a little bit of code that increments nPointsShowing and calls repaint().
    Start the timer when the first point is painted.
    Stop the timer after plotting all the points.

    and also it should begin scaled up already so that one can actually follow the traces of the planets.
    I fear the whole Affine Transform thing is a distraction.
    Do scaling by scaling the coordinates.
    What are the minimum and maximum x values? ditto for y values.
    What are the bounds of the screen space? (Answer: 0...getWidth() and 0...getHeight())

    Now write a method to map from x,y to screen position.
    Something like: screenX = (pointX - minPointX) * getWidth / (maxPointX - minPointX)
    and similarly for y. There is only one input variable, pointX;
    the rest are constants depending on the range of pointX values.

  17. #17
    chappa is offline Member
    Join Date
    Dec 2009
    Posts
    19
    Rep Power
    0

    Default

    Thank you a lot zweibieren, your scaling tip worked great!

Similar Threads

  1. Replies: 3
    Last Post: 12-10-2009, 04:07 PM
  2. Scaling a JPEG
    By ScottVal in forum Advanced Java
    Replies: 5
    Last Post: 03-21-2009, 09:47 PM
  3. Scaling-ache and mouse dragging
    By willemjav in forum Java Applets
    Replies: 19
    Last Post: 07-19-2008, 01:17 AM
  4. scaling images
    By willemjav in forum Java Applets
    Replies: 7
    Last Post: 06-19-2008, 10:54 AM
  5. PLz i really need help on this final thing
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 03:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •