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

    Default AWT: Painting buttons and text areas in a canvas

    Hello,


    is it possible to paint buttons and text areas in a canvas? What is the best way to make so the button is fixed to a constant and then when pressed the programme will run using the fixed constants? Same for the text area?

    I have googled already but I will always only find descriptions for swing or applets using awt.


    Help is greatly appreciated


    chappa

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

    Default

    This my the code I am trying to get along with at the moment :

    Java Code:
    import java.lang.* ;
    import java.awt.*  ;
    
    
    import javax.swing.* ;
    
    
    class mj10 extends Frame{
    	mj10(){
    		add ("Center", new Dumbo());
    		setTitle("mambo");
    		setSize(900,900);
    		setVisible(true);	
    	}
    	
    	public static void main (String [] args) {
    		new mj10 ();
    		//new Testframe
    	}
    }
    class Dumbo extends Canvas{
    	public void paint(Graphics g) {
    		
    		//* calculations which I took out and also the painting thingie itself*// 
    			
    		}	
    	}	
    METHOD1 (for painting in the canvas)
    		}
    METHOD2 (for painting in the canvas)
    		}
    METHOD3 (for painting in the canvas)
    		}
    METHOD4 (for painting in the canvas)
    		}
    METHOD5 (for painting in the canvas)
    		}
    
    
    }
    
    class Frame2 extends Frame{
    	private Button button1 = new Button( "ok");
    	
    	public Frame2( String Beschriftung){
    		super(Beschriftung);
    		setLayout(new FlowLayout());
    		add(button1);
    	}
    	static public void main(String [] args){
    		Frame2 f = new Frame2( "Demo");
    		f.setSize(234, 123);
    		f.show();
    	}
    }

    I am trying to first of all build a button/textarea at all, then the second step would be to assign these button/textarea a variable that will change the calculation output in the end.

    However I don't even seem to be able to figure out how to build a button in my frame under the canvas-painting (or in the canvas).
    Last edited by chappa; 01-07-2010 at 04:32 AM.

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

    Default

    I have cheated my way through this by (ab)using the code of the user zweibieren from this forum (thanks!).

    The whole thing looks as follows now:
    Java Code:
    /* most of the GUI-code was written by the user  "zweibieren" from the java-forums.org/
    **/ 
    /*************************************************
    Zeit in 10^6 s
    Massen in 10^24 kg
    Distanzen in 10^10m
    **************************************************/
    
    
    
    package Test;
    
    import java.awt.*;
    import java.awt.event.*;
    
    
    import javax.swing.*;
    
    public class Success  extends JPanel {
    
    	// the window
    	Window winFred;
    
    	// the contents of the window
    	JButton eatMeButton, SuccessButton;
    	Circle circ;
    
    	final static Dimension BUTTON_SIZE = new Dimension(125,75);
    	final static Dimension WINDOW_SIZE = new Dimension(1024,768);
    	final static Dimension CIRC_SIZE = new Dimension(900,900);
    
    	public Success() {
    		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);
    		SuccessButton = new JButton("drink me");
    		SuccessButton.setPreferredSize(BUTTON_SIZE);
    		circ = new Circle();
    		circ.setPreferredSize(new Dimension(CIRC_SIZE.height, CIRC_SIZE.height));
    
    		
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(circ);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(eatMeButton);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(SuccessButton);
    		buttonBox.add(Box.createHorizontalGlue());
    
    	}
    
    
    
    	// the circle
    
    	static class Circle extends JPanel {
    		
    
    		@Override
    		public void paintComponent(Graphics g) {
    			g.setColor(Color.red);
    			Graphics2D gg = (Graphics2D)g;
    		int m = 200000;
    		int m1 = 6000;
    		int m2 = 8000;
    		int b = 900;
    		int h = 900;
    		
    		
    		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;
    		double mittelpunktx = b/2;
    		double mittelpunkty = h/2;
    		double vy = 0.0001 * Math.sqrt(1000 * gamma * m / r);
    		double vy2 = 0.0001 * Math.sqrt(1000* gamma * m / r2);
    		double tend = 500;
    		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 = mittelpunktx + x2;
    			double posy2 = mittelpunkty + 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;
    			
    			double finalx = skalierenx(posx);
    			double finaly = skaliereny(posy);
    			double finalx2 = skalierenx2(posx2);
    			double finaly2 = skaliereny2(posy2);
    			double finalmittelpunktx = skalierenx(mittelpunktx);
    			double finalmittelpunkty = skaliereny(mittelpunkty);
    			
    			
    			g.setColor(Color.blue);
    			g.drawLine((int)(finalx),(int)(finaly),(int)(finalx),(int)(finaly));
    			g.setColor(Color.red);
    			g.drawLine((int)(finalx2),(int)(finaly2),(int)(finalx2),(int)(finaly2));
    			g.drawLine((int)(finalmittelpunktx),(int)(finalmittelpunkty),(int)(finalmittelpunktx),(int)(finalmittelpunkty));
    			}
    		}
    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;
    		}
    
    
    public double skalierenx(double posx){
    	double minPointX = 508.0;
    	double maxPointX = 366.594;
    	double screenX = (posx - minPointX) * getWidth() / (maxPointX - minPointX);
    	
    	return screenX;
    		}
    public double skaliereny(double posy){
    	double minPointY = 497.66;
    	double maxPointY = 337.468;
    	double screenY = (posy - minPointY) * getWidth() / (maxPointY - minPointY);
    	
    	return screenY;
    		}
    public double skalierenx2(double posx2){
    	double minPointX2 = 508.0;
    	double maxPointX2 = 366.594;
    	double screenX2 = (posx2 - minPointX2) * getWidth() / (maxPointX2 - minPointX2);
    	
    	return screenX2;
    		}
    public double skaliereny2(double posy2){
    	double minPointY2 = 497.66;
    	double maxPointY2 = 337.468;
    	double screenY2 = (posy2 - minPointY2) * getWidth() / (maxPointY2 - minPointY2);
    	
    	return screenY2;
    		}		
    	}
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("Success");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JComponent newContentPane = new Success();
            frame.setContentPane(newContentPane);
    
    		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();
                }
            });
        }
    }

    My issue is now how I can change that the programme starts up, and then I can press the button and the variable changes and the programme calculates with the value which is stored with the button.

    Or a textarea for a variable which the programme will then take from that textarea and calculate and paint with.


    How do I implement such a thing?

    Any help is greatly appreciated.


    chippa


    PS: please note that I put an equal thread on another forum (which can be found here)
    Last edited by chappa; 01-07-2010 at 11:13 PM.

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

    Default

    Take all the computations out of paintComponent.
    Let me emphasize that by saying:
    Take all the computations out of paintComponent.

    Put them in an object of a new class.
    Class variables in that class should be the values to be displayed from paintComponent (in the last five lines.
    The input values like m1 should also be class variables.
    The button should change the value of m1 in this object.
    When m1 changes, the computations should be done and repait called.
    PaintComponent then fetchs the display variables from the computation object.

    Lesson: No method should do very much.

    Some people say no more than ten lines to a method.
    I seldom go that low.
    The computation method using the code in the above would be fairly long.
    BUT, the computation method will only do ONE thing: the computation.
    It will call other methods to set the display variables.
    These other methods will call repaint() or listeners.

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

    Default

    Hello and thank you again zweibieren,


    I do not fully understand what you mean; do you mean I should put the calculations and the methods in the class object OR do you mean I should put the variables in the object and the calculations in a method as well?

    To clarify my confusion I will post the examples:

    first version:

    Java Code:
    package Test;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    
    
    public class Stop extends JPanel {
    	
    	JButton m1Button, m2Button;
    	JTextfield typeMeField;
    	JLabel typeMeFieldlabel;
    	ActionListener a1 = new myButtonListener();
    	
    	final static Dimension BUTTON_SIZE = new Dimension(125,75);
    	final static Dimension WINDOWS_SIZE = new Dimension(1024,768);
    	final static Dimension CIRC_SIZE = new Dimension(900,900);
    	final static Dimension FIELD_SIZE = new Dimension(150,95);
    	
    	
    	public Stop(){
    		setLayout(new BoxLayout(this, Box.Layout.PAGE_AXIS/2));
    		
    		Box buttonBox = Box.createHorizontalBox();
    		add(Box.createVerticalGlue());
    		add(buttonBox);
    		add(Box.createVerticalGlue());
    		
    		m1Button = new JButton("m1=10000");
    		m1Button.addActionListener(a1);
    		m1Button.setPreferredSize(BUTTON_SIZE);
    		m2Button = new JButton("m2=15000");
    		m2Button.setPreferredSize(BUTTON_SIZE);
    		
    		typeMeFieldlabel = new JLabel("type new m1");
    		typeMeField = new JTextField(15);
    		typeMeField.setPreferredSize(FIELD_SIZE);
    		
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(m1Button);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(m2Button);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(typeMeFieldlabel);
    		buttonBox.add(typeMeField);
    		typeMeFieldlabel.setLabelFor(typeMeField);
    	}
    	
    	static class Painting extends JPanel{
    		@Override
    		public void paintComponent(Graphics g){
    			Graphics2D gg = (Graphics2D)g;
    			
    			g.setColor(Color.blue);
    			g.drawLine((int)(finalx),(int)(finaly),(int)(finalx),(int)(finaly));
    			g.setColor(Color.red);
    			g.drawLine((int)(finalx2),(int)(finaly2),(int)(finalx2),(int)(finaly2));
    			g.drawLine((int)(finalmittelpunktx),(int)(finalmittelpunkty),(int)(finalmittelpunktx),(int)(finalmittelpunkty));
    		}
    	}
    	
    	
    	class Calculations(){
    		
    		int m, m1, m2, 
    		int b = 900;
    		int h = 900 ;
    		
    		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;
    		double mittelpunktx = b/2;
    		double mittelpunkty = h/2;
    		double vy = 0.0001 * Math.sqrt(1000 * gamma * m / r);
    		double vy2 = 0.0001 * Math.sqrt(1000* gamma * m / r2);
    		double tend = 500;
    		double t = 0;
    		double pi = 4*Math.atan(1);
    		double deltat = 0.01;
    		double result = 0;
    	}
    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;
    		}
    
    
    
    public double skalierenx(double posx){
    	double minPointX = 508.0;
    	double maxPointX = 366.594;
    	double screenX = (posx - minPointX) * getWidth() / (maxPointX - minPointX);
    	
    	return screenX;
    		}
    public double skaliereny(double posy){
    	double minPointY = 497.66;
    	double maxPointY = 337.468;
    	double screenY = (posy - minPointY) * getWidth() / (maxPointY - minPointY);
    	
    	return screenY;
    		}
    	}
    }
    
    unfinished code ! ..................

    second version ( I have not figured out what datatypes to use for the return values so I set it to double) :

    Java Code:
    package Test;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    
    
    public class Stop extends JPanel {
    	
    	JButton m1Button, m2Button;
    	JTextfield typeMeField;
    	JLabel typeMeFieldlabel;
    	ActionListener a1 = new myButtonListener();
    	
    	final static Dimension BUTTON_SIZE = new Dimension(125,75);
    	final static Dimension WINDOWS_SIZE = new Dimension(1024,768);
    	final static Dimension CIRC_SIZE = new Dimension(900,900);
    	final static Dimension FIELD_SIZE = new Dimension(150,95);
    	
    	
    	public Stop(){
    		setLayout(new BoxLayout(this, Box.Layout.PAGE_AXIS/2));
    		
    		Box buttonBox = Box.createHorizontalBox();
    		add(Box.createVerticalGlue());
    		add(buttonBox);
    		add(Box.createVerticalGlue());
    		
    		m1Button = new JButton("m1=10000");
    		m1Button.addActionListener(a1);
    		m1Button.setPreferredSize(BUTTON_SIZE);
    		m2Button = new JButton("m2=15000");
    		m2Button.setPreferredSize(BUTTON_SIZE);
    		
    		typeMeFieldlabel = new JLabel("type new m1");
    		typeMeField = new JTextField(15);
    		typeMeField.setPreferredSize(FIELD_SIZE);
    		
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(m1Button);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(m2Button);
    		buttonBox.add(Box.createHorizontalGlue());
    		buttonBox.add(typeMeFieldlabel);
    		buttonBox.add(typeMeField);
    		typeMeFieldlabel.setLabelFor(typeMeField);
    	}
    	
    	static class Painting extends JPanel{
    		@Override
    		public void paintComponent(Graphics g){
    			Graphics2D gg = (Graphics2D)g;
    			
    			g.setColor(Color.blue);
    			g.drawLine((int)(finalx),(int)(finaly),(int)(finalx),(int)(finaly));
    			g.setColor(Color.red);
    			g.drawLine((int)(finalx2),(int)(finaly2),(int)(finalx2),(int)(finaly2));
    			g.drawLine((int)(finalmittelpunktx),(int)(finalmittelpunkty),(int)(finalmittelpunktx),(int)(finalmittelpunkty));
    		}
    	}
    	
    	
    	class Calculations(){
    		
    		int  m1, m2 ;
    		int finalx = resultx, finaly = resulty, finalx2 = resultx2, finaly2 = resulty2, finalmittelpunktx = resultmiddlex, finalmittelpunkty = resultmiddley;
    		
    		public double maincalculation(int m1, int m2, int resultx, int resulty, int resultx2, int resulty2, int resultmiddlex, int resultmiddley){
    		
    		int m = 200000;
    		int m1, m2;
    		
    		int b = 900;
    		int h = 900;
    		
    		
    		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;
    		double mittelpunktx = b/2;
    		double mittelpunkty = h/2;
    		double vy = 0.0001 * Math.sqrt(1000 * gamma * m / r);
    		double vy2 = 0.0001 * Math.sqrt(1000* gamma * m / r2);
    		double tend = 500;
    		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 = mittelpunktx + x2;
    			double posy2 = mittelpunkty + 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;
    			
    			double resultx = skalierenx(posx);
    			double resulty = skaliereny(posy);
    			double resultx2 = skalierenx(posx2);
    			double resulty2 = skaliereny(posy2);
    			double resultmittelpunktx = skalierenx(mittelpunktx);
    			double resultmittelpunkty = skaliereny(mittelpunkty);
    
    		}
    		return resultx, resulty, resultx2, resulty2, resultmittelpunktx, resultmittelpunkty;
    	}
    	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;
    		}
    
    
    
    public double skalierenx(double posx){
    	double minPointX = 508.0;
    	double maxPointX = 366.594;
    	double screenX = (posx - minPointX) * getWidth() / (maxPointX - minPointX);
    	
    	return screenX;
    		}
    public double skaliereny(double posy){
    	double minPointY = 497.66;
    	double maxPointY = 337.468;
    	double screenY = (posy - minPointY) * getWidth() / (maxPointY - minPointY);
    	
    	return screenY;
    		}
    	}
    }
    
    unfinished code! .........

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

    Default

    A class has class variables and methods.

    Java Code:
    class Calculations {
        double m1; 
        double result;
    
        public void calculate() {
             // I think most of the variables are temporaries.
             // they get declared in here:
             double alpha, ax, a, ...
            
             then the calculation is done here setting 'result' and the other output variables.
        }
    }
    (In future you should avoid posting code that does not at least
    compile without syntax errors. Use an IDE like NetBeans of Eclipse.)

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

    Default

    Thank you again zweibieren,


    thanks to the user "R. Johnson" from the other forum the problem has been solved as following:

    Java Code:
     import java.awt.*;  
    import java.awt.event.*;  
    import javax.swing.*;  
      
    public class Success2  extends JPanel {  
    	  
        // the contents of the window  
        JButton eatMeButton, drinkmeButton;  
        Circle circ=new Circle();  
        ActionListener al = new myButtonListener(circ);  
      
        final static Dimension BUTTON_SIZE = new Dimension(125,75);  
        final static Dimension WINDOW_SIZE = new Dimension(1024,768);  
        final static Dimension CIRC_SIZE = new Dimension(900,900);  
      
        public Success2() {  
            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("m1=1500");  
            eatMeButton.addActionListener(al);  
            eatMeButton.setPreferredSize(BUTTON_SIZE);  
            drinkmeButton = new JButton("drink me");  
            drinkmeButton.setPreferredSize(BUTTON_SIZE);  
             
            circ.setPreferredSize(new Dimension(CIRC_SIZE.height, CIRC_SIZE.height));  
      
      
            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());  
      
        }  
      
      
      
        // the circle  
      
         static class Circle extends JPanel {  
      
            int m1=6000;  
            @Override  
            public void paintComponent(Graphics g) {  
                g.clearRect(0, 0, this.getWidth(), this.getHeight());  
                g.setColor(Color.red);  
                Graphics2D gg = (Graphics2D)g;  
            int m = 200000;  
            //int m1 = 6000;  
            int m2 = 8000;  
            int b = 900;  
            int h = 900;  
      
      
            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;  
            double mittelpunktx = b/2;  
            double mittelpunkty = h/2;  
            double vy = 0.0001 * Math.sqrt(1000 * gamma * m / r);  
            double vy2 = 0.0001 * Math.sqrt(1000* gamma * m / r2);  
            double tend = 500;  
            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 = mittelpunktx + x2;  
                double posy2 = mittelpunkty + 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;  
      
                double finalx = skalierenx(posx);  
                double finaly = skaliereny(posy);  
                double finalx2 = skalierenx2(posx2);  
                double finaly2 = skaliereny2(posy2);  
                double finalmittelpunktx = skalierenx(mittelpunktx);  
                double finalmittelpunkty = skaliereny(mittelpunkty);  
    
    			
                     
      
                g.setColor(Color.blue);  
                g.drawLine((int)(finalx),(int)(finaly),(int)(finalx),(int)(finaly));  
                g.setColor(Color.red);  
                g.drawLine((int)(finalx2),(int)(finaly2),(int)(finalx2),(int)(finaly2));  
                g.drawLine((int)(finalmittelpunktx),(int)(finalmittelpunkty),(int)(finalmittelpunktx),(int)(finalmittelpunkty));  
            //  g.drawOval((int)(finalx),(int)(finaly),);  
                }  
            }  
    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;  
            }  
      
      
    public double skalierenx(double posx){  
        double minPointX = 508.0;  
        double maxPointX = 366.594;  
        double screenX = (posx - minPointX) * getWidth() / (maxPointX - minPointX);  
      
        return screenX;  
            }  
    public double skaliereny(double posy){  
        double minPointY = 497.66;  
        double maxPointY = 337.468;  
        double screenY = (posy - minPointY) * getWidth() / (maxPointY - minPointY);  
      
        return screenY;  
            }  
    public double skalierenx2(double posx2){  
        double minPointX2 = 508.0;  
        double maxPointX2 = 366.594;  
        double screenX2 = (posx2 - minPointX2) * getWidth() / (maxPointX2 - minPointX2);  
      
        return screenX2;  
            }  
    public double skaliereny2(double posy2){  
        double minPointY2 = 497.66;  
        double maxPointY2 = 337.468;  
        double screenY2 = (posy2 - minPointY2) * getWidth() / (maxPointY2 - minPointY2);  
      
        return screenY2;  
            }  
        }  
      
        private static void createAndShowGUI() {  
            JFrame frame = new JFrame("Success2");  
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
      
            JComponent newContentPane = new Success2();  
            frame.setContentPane(newContentPane);  
      
            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();  
                }  
            });  
        }  
    class myButtonListener implements ActionListener{  
        Circle c;  
       myButtonListener(Circle c){  
           this.c=c;  
       }  
        public void actionPerformed(ActionEvent ae){  
            c.m1 = 8000;  
            c.repaint();  
            }  
        }  
      
    }

    but thanks for your help!

Similar Threads

  1. JPanel not always painting everything
    By ekted in forum AWT / Swing
    Replies: 0
    Last Post: 11-26-2009, 11:24 AM
  2. Replies: 0
    Last Post: 07-05-2009, 12:31 PM
  3. Jpanel painting problem
    By kcakir in forum AWT / Swing
    Replies: 3
    Last Post: 04-15-2009, 10:21 PM
  4. text areas, printstream, and "\n"
    By diggitydoggz in forum New To Java
    Replies: 10
    Last Post: 12-26-2008, 04:03 PM
  5. Searching a string from a text file using Swing Buttons
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 2
    Last Post: 09-15-2008, 09:50 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
  •