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.