Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple GUI/Animation question...

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple GUI/Animation question...

    I am currently trying to build a very basic animation program. I want to draw an arc, or bending line, based off input from the GUI.

    I'm very new to Java, with some experience in other languages.

    I have the basic code working kind of. My GUI pops up and I can redraw new lines when the user hits the buttons, but the two aren't properly related. I just want my line to be drawn in a box on the same GUI screen as the buttons.

    I'm sure you pros can solve this in a heartbeat. I've been trying to do this by scrapping together other examples I've found online.

    my Main code that starts the GUI:

    package animation;
     
    import javax.swing.*;
     
    public class Main extends JApplet {
     
    	public Main() {
    		add(new Main());
    	}
     
    	public static void main(String[] args) {
    		JFrame win = new JFrame("Strain Gauge Simulation");
    		win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		win.setContentPane(new BBPanel());
    		win.pack();
    		win.setVisible(true); 
    	}
    }

    My code that builds the GUI panel:
    package animation;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
     
    class BBPanel extends JPanel {
     
        private int forces;
        Arc1 m_cc;
     
        BBPanel() {
        	forces = 0;      
            m_cc = new Arc1();
     
            m_cc.init(forces);
     
            m_cc.setPreferredSize(new Dimension(500, 500));
     
            JButton forcedButton = new JButton("Force Down");
            JButton forceuButton = new JButton("Force Up");
     
            forceuButton.addActionListener(new ForceuAction());
            forcedButton.addActionListener(new ForcedAction());
     
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new FlowLayout());
     
            buttonPanel.add(forceuButton);
            buttonPanel.add(forcedButton);
     
            this.setLayout(new BorderLayout());
            this.add(buttonPanel, BorderLayout.NORTH);
            this.add(m_cc       , BorderLayout.CENTER);
        }
     
        class ForceuAction implements ActionListener {
        	public void actionPerformed(ActionEvent e) {
        		forces = forces + 1;
        		JFrame f = new JFrame();
        	      Arc1 curve = new Arc1();
        	      f.setLocation(150+forces*10, 150+forces*10);
        	      f.setSize(500,500);
        	      curve.init(forces);
        	      f.getContentPane().add(curve);
        	      f.setDefaultCloseOperation(1);
        	      f.setVisible(true);
     
        		System.out.println(forces);
        	}
        }
     
        class ForcedAction implements ActionListener {
        	public void actionPerformed(ActionEvent e){
     
        		forces = forces - 1;
        		m_cc.init(forces);
        		JFrame f = new JFrame();
        	     Arc1 curve = new Arc1();
        	      f.setLocation(150+forces*10, 150+forces*10);
        	      f.setSize(500,500);
        	      curve.init(forces);
        	      f.getContentPane().add(curve);
        	      f.setDefaultCloseOperation(1);
        	      f.setVisible(true);
     
        		System.out.println(forces);
        	}
        }  
    }

    And the code I have that draws the line:
    package animation;
     
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.QuadCurve2D;
    import java.awt.geom.Rectangle2D;
    import java.util.Vector;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
    import javax.swing.JApplet;
     
    public class Arc1 extends JApplet{
      DrawingCanvas canvas;
     
     
      public static void main(String[] a){
     
          /*JFrame f = new JFrame();
          Arc1 curve = new Arc1();
          f.setSize(1000,1000);
          curve.init();
          f.getContentPane().add(curve);
          f.setDefaultCloseOperation(1);
          f.setVisible(true);*/
      }
     
      public void init(int forces) {
     
        Container container = getContentPane();
        canvas = new DrawingCanvas(forces);
        container.add(canvas);
      }
     
      class DrawingCanvas extends Canvas {
     
        Vector quadCurves;
        QuadCurve2D selectedCurve = null;
        Rectangle2D boundingRec = null;
     
        public DrawingCanvas(int forces){
     
          quadCurves = new Vector();
          quadCurves.addElement(new QuadCurve2D.Float(1, 100, 301, 100, 401, 100+forces*5));     
        }
     
        public void paint(Graphics g) {
          Graphics2D g2D = (Graphics2D) g;
     
          for (int i = 0; i < quadCurves.size(); i++) {
            g2D.draw((QuadCurve2D) quadCurves.elementAt(i));
          }
     
        }
      }
    }


    Any help would be great, I would really love have that line embedded below the buttons. The example code I'm building off of can be found here

    And I know that I shouldn't rebuild a new frame each time the force is changed by hitting the button, but I added it in so you can see what I would like to appear on the GUI, instead of a new window.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Simple GUI/Animation question...

    Are you making an applet, or are you making a Java application (there is a difference)? Currently it looks like you're coding as if you're making an application. You should only extend the applet class if you're making an applet.

    public Main() {
    		add(new Main());
    	}
    This can be removed. Neither the applet or Java application will make use of this (not to mention you'd get a stackoverflow exception or memory allocation exception). Applets use the init() and start methods to set themselves up. Your main method doesn't create a Main object (it doesn't need to) but rather adds the items into a standard JFrame, so there's no need to created code which would instantiate a Main object.

    Secondly, there's no need for the Arc1 class. You should be doing the painting in BBPanel (override the paintComponent method). Your ActionEvent handler should only change the location of the arc then repaint the panel (conveniently call the repaint method).

    Rather than copy and pasting random code, try to understand what the code is doing. Here are some good resources you can look at to understand Java Swing applications:

    Painting in AWT and Swing
    Performing Custom Painting
    Creating a GUI with JFC/Swing

    Also, the Java SE Javadoc is a great source of information.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple GUI/Animation question...

    Thanks so much for the advice. I've read through a lot of that, but again, its mostly above my head still because I'm so new to Java. Regardless, I made some changes based on what you provided.

    And yes I am coding an application, running in Eclipse.

    It works.... better. Now its a matter of refreshing the newly drawn image. If I resize the window it refreshes, but I can click the 'up/down' buttons all I want and it wont change until I do so.

    Here is the new code per your advice:
    package animation;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.QuadCurve2D;
    import java.awt.geom.QuadCurve2D.Float;
    import java.util.Vector;
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
     
     
    class BBPanel extends JPanel {
        private int forces;
     
        BBPanel() {     
     
            JButton forcedButton = new JButton("Force Down");
            JButton forceuButton = new JButton("Force Up");
     
            forceuButton.addActionListener(new ForceuAction());
            forcedButton.addActionListener(new ForcedAction());
     
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new FlowLayout());
     
            JPanel drawpanel = new JPanel();
            drawpanel.setLayout(new FlowLayout());
            drawpanel.setVisible(true);
            drawpanel.setBorder(BorderFactory.createLineBorder(Color.black));
            drawpanel.setPreferredSize(new Dimension(450,300));
     
            JPanel southpanel = new JPanel();
            southpanel.setLayout(new FlowLayout());
            southpanel.setVisible(true);
            southpanel.setBorder(BorderFactory.createLineBorder(Color.black));
     
            ImageIcon icon = new ImageIcon("C:\\Users\\bmj119\\Pictures\\wsb.jpg"); 
            JLabel label = new JLabel(icon);
            southpanel.add(label);
     
            buttonPanel.add(forceuButton);
            buttonPanel.add(forcedButton);
     
            this.setLayout(new BorderLayout());
            this.add(buttonPanel, BorderLayout.NORTH);
            this.add(drawpanel, BorderLayout.CENTER);
            this.add(southpanel, BorderLayout.SOUTH);
        }
     
        class ForceuAction implements ActionListener {
        	public void actionPerformed(ActionEvent e) {
     
        	    forces = forces + 1; 
     
        		System.out.println(forces);
     
        	}
        }
     
        class ForcedAction implements ActionListener {
        	public void actionPerformed(ActionEvent e){
     
        		forces = forces - 1;
     
        		System.out.println(forces);
     
        	}
        }
     
     
        public void paint(Graphics g) {
        	super.paintComponents(g);
     
            Graphics2D g2D = (Graphics2D) g;
            Vector<Float> quadCurves;
    		quadCurves = new Vector<Float>();
    	    quadCurves.addElement(new QuadCurve2D.Float(1, 100, 301, 100, 401, 100+forces*5));
     
            for (int i = 0; i < quadCurves.size(); i++) {
              g2D.draw((QuadCurve2D) quadCurves.elementAt(i));
            }      
        }
     
    }

    Thanks again, I feel I'm really close.

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple GUI/Animation question...

    Ok, maybe Its not working as well as I thought, if I resize too much it distorts everything. If I can get it to auto-refresh this won't be a problem, but I would like to know why.

    I feel like the drawing is just 'landing' in the intended panel, as opposed to being embedded at the desired position.

Similar Threads

  1. [SOLVED] Simple question from a beginner
    By jimmylee7706 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 09:57 PM
  2. Just a simple question
    By newbie in forum Java Theory & Questions
    Replies: 10
    Last Post: December 6th, 2010, 08:19 PM
  3. SUPER SIMPLE QUESTION!!!
    By Options in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 2nd, 2010, 09:21 PM
  4. simple question...
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 2nd, 2010, 07:29 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM