Cubic Curve using JFrame


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Cubic Curve using JFrame

  1. #1
    Join Date
    Nov 2004
    Posts
    14

    Cubic Curve using JFrame

    I am not very familiar with JFrame and I was just practicing with this program and I am not sure how to fix it. It only shows the new cubic curve when you resize the window and the labels do not appear and the sliders only appear if you click on where one should be.

    Here is my code if needed:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.swing.*;
    import java.applet.*;
    import java.awt.geom.CubicCurve2D;
    
    class CubicFrame extends JFrame
    {
    	public CubicFrame()
    	{
    		cubicPanel = new JPanel();
    		cubicPanel.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
    		
    		getContentPane().add(cubicPanel, BorderLayout.CENTER);
    		createControlPanel();
    		setSampleCubic();
    		pack();
    	}
    	
    	public void createControlPanel()
    	{
    		class  CubicListener implements ChangeListener
    		{
    			public void stateChanged(ChangeEvent event)
    			{
    				setSampleCubic();
    			}
    		}
    		
    		ChangeListener listener = new CubicListener();
    		
    		x1Slider = new JSlider(0,300,50);
    		x1Slider.addChangeListener(listener);
    		y1Slider = new JSlider(0,300,150);
    		y1Slider.addChangeListener(listener);
    		ctrlx1Slider = new JSlider(0,300,100);
    		ctrlx1Slider.addChangeListener(listener);
    		ctrly1Slider = new JSlider(0,300,200);
    		ctrly1Slider.addChangeListener(listener);
    		ctrlx2Slider = new JSlider(0,300,75);
    		ctrlx2Slider.addChangeListener(listener);
    		ctrly2Slider = new JSlider(0,300,10);
    		ctrly2Slider.addChangeListener(listener);
    		x2Slider = new JSlider(0,300,200);
    		x2Slider.addChangeListener(listener);
    		y2Slider = new JSlider(0,300,300);
    		y2Slider.addChangeListener(listener);
    		
    		JPanel controlPanel = new JPanel();
    		controlPanel.setLayout(new GridLayout(8,1));
    		
    		controlPanel.add(new JLabel("x1",SwingConstants.LEFT));
    		controlPanel.add(x1Slider);
    		controlPanel.add(new JLabel("y1",SwingConstants.LEFT));
    		controlPanel.add(y1Slider);
    		controlPanel.add(new JLabel("ctrlx1", SwingConstants.LEFT));
    		controlPanel.add(ctrlx1Slider);
    		controlPanel.add(new JLabel("ctrly1",SwingConstants.LEFT));
    		controlPanel.add(ctrly1Slider);
    		controlPanel.add(new JLabel("ctrlx2",SwingConstants.LEFT));
    		controlPanel.add(ctrlx2Slider);
    		controlPanel.add(new JLabel("ctrly2", SwingConstants.LEFT));
    		controlPanel.add(ctrly2Slider);
    		controlPanel.add(new JLabel("x2",SwingConstants.LEFT));
    		controlPanel.add(x2Slider);
    		controlPanel.add(new JLabel("y2",SwingConstants.LEFT));
    		controlPanel.add(y2Slider);
    		
    		getContentPane().add(controlPanel, BorderLayout.SOUTH);	
    	}
    	
    	public void paint(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D)g;
    		cubic = new CubicCurve2D.Double(x1,y1,ctrlx1,ctrly1,ctrlx2,ctrly2,x2,y2);
    		g2.draw(cubic);
    	}
    	public void setSampleCubic()
    	{
    		x1 = x1Slider.getValue();
    		y1 = y1Slider.getValue();
    		ctrlx1 = ctrlx1Slider.getValue();
    		ctrly1 = ctrly1Slider.getValue();
    		ctrlx2 = ctrlx2Slider.getValue();
    		ctrly2 = ctrly2Slider.getValue();
    		x2 = x2Slider.getValue();
    		y2 = y2Slider.getValue();
    		cubicPanel.repaint();
    	}	
    	private int x1,x2,y1,y2,ctrlx1,ctrlx2,ctrly1,ctrly2;
    	private CubicCurve2D cubic;
    	private JPanel cubicPanel;
    	private JSlider x1Slider,y1Slider,ctrlx1Slider,ctrly1Slider,ctrlx2Slider,ctrly2Slider,x2Slider,y2Slider;
    	private static final int PANEL_WIDTH = 300;
    	private static final int PANEL_HEIGHT = 300;
    }
    Code:
    import javax.swing.*;
    
    public class CubicTest
    {
    	public static void main(String[] args)
    	{
    		CubicFrame panel = new CubicFrame();
    		panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		panel.show();
    	}
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You should split this in two, one standard JPanel for the sliders, and one JPanel extension where you override
    the paint/update method drawing the curve,
    and put these two into a JFrame.

    As far as I have seen its not possible to mix standard
    components/layoutmanages and at the
    same time doing "freehand" drawing in the same
    container
    .

    I'd be very happy if anyone could show me a failsafe
    method for doing that.
    eschew obfuscation

  3. #3
    Join Date
    Nov 2004
    Posts
    14
    I am not sure how to go about doing this. I tried and succeeded in making the sliders and labels work right but then the CubicCurve would not appear, so you could give a slight bit more explanation on how to make the JPanel extension.

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I have made the splitup here. Now the curve panels
    paint method is used, not the JFrame's. I also
    made the curvepanel a listener to the slider changes,
    after all, why bother the jframe code w. that ? In order
    to make that work I made the sliders public. Note the
    wipeout that is being done in the paint method, without
    that little code fragment it doesn't look good

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.swing.*;
    import java.applet.*;
    import java.awt.geom.CubicCurve2D;
    
    class CubicPanel extends JPanel implements ChangeListener {
    
      CubicFrame frame=null;
    
      public CubicPanel(CubicFrame frame) {
        this.frame=frame;
      }
      public void stateChanged(ChangeEvent event) {
        repaint();
      }
    
      public void paint(Graphics g) {
        int x1 = frame.x1Slider.getValue();
        int y1 = frame.y1Slider.getValue();
        int ctrlx1 = frame.ctrlx1Slider.getValue();
        int ctrly1 = frame.ctrly1Slider.getValue();
        int ctrlx2 = frame.ctrlx2Slider.getValue();
        int ctrly2 = frame.ctrly2Slider.getValue();
        int x2 = frame.x2Slider.getValue();
        int y2 = frame.y2Slider.getValue();
    
        // For each paint job, first wipe out the last picture
    
        Color c=g.getColor(); // save graphics current color
        g.setColor(Color.white); // set white
        g.fillRect(0,0,this.getSize().width, this.getSize().height); // wipe clean
        g.setColor(c); // reset graphics current color
    
        Graphics2D g2 = (Graphics2D) g;
        CubicCurve2D cubic = new CubicCurve2D.Double(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2,
                                        y2);
        g2.draw(cubic);
      }
    
    }
    
    public class CubicFrame
        extends JFrame {
    
        private CubicPanel cubicPanel;
        public JSlider x1Slider, y1Slider, ctrlx1Slider, ctrly1Slider;
        public JSlider ctrlx2Slider, ctrly2Slider, x2Slider, y2Slider;
        private static final int PANEL_WIDTH = 300;
        private static final int PANEL_HEIGHT = 300;
    
      public CubicFrame() {
        cubicPanel = new CubicPanel(this);
        cubicPanel.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
    
        getContentPane().add(cubicPanel, BorderLayout.CENTER);
        createControlPanel();
        cubicPanel.repaint();
        pack();
      }
    
      public void createControlPanel() {
    
        x1Slider = new JSlider(0, 300, 50);
        x1Slider.addChangeListener(cubicPanel);
        y1Slider = new JSlider(0, 300, 150);
        y1Slider.addChangeListener(cubicPanel);
        ctrlx1Slider = new JSlider(0, 300, 100);
        ctrlx1Slider.addChangeListener(cubicPanel);
        ctrly1Slider = new JSlider(0, 300, 200);
        ctrly1Slider.addChangeListener(cubicPanel);
        ctrlx2Slider = new JSlider(0, 300, 75);
        ctrlx2Slider.addChangeListener(cubicPanel);
        ctrly2Slider = new JSlider(0, 300, 10);
        ctrly2Slider.addChangeListener(cubicPanel);
        x2Slider = new JSlider(0, 300, 200);
        x2Slider.addChangeListener(cubicPanel);
        y2Slider = new JSlider(0, 300, 300);
        y2Slider.addChangeListener(cubicPanel);
    
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new GridLayout(8, 1));
    
        controlPanel.add(new JLabel("x1", SwingConstants.LEFT));
        controlPanel.add(x1Slider);
        controlPanel.add(new JLabel("y1", SwingConstants.LEFT));
        controlPanel.add(y1Slider);
        controlPanel.add(new JLabel("ctrlx1", SwingConstants.LEFT));
        controlPanel.add(ctrlx1Slider);
        controlPanel.add(new JLabel("ctrly1", SwingConstants.LEFT));
        controlPanel.add(ctrly1Slider);
        controlPanel.add(new JLabel("ctrlx2", SwingConstants.LEFT));
        controlPanel.add(ctrlx2Slider);
        controlPanel.add(new JLabel("ctrly2", SwingConstants.LEFT));
        controlPanel.add(ctrly2Slider);
        controlPanel.add(new JLabel("x2", SwingConstants.LEFT));
        controlPanel.add(x2Slider);
        controlPanel.add(new JLabel("y2", SwingConstants.LEFT));
        controlPanel.add(y2Slider);
    
        getContentPane().add(controlPanel, BorderLayout.SOUTH);
      }
    
    
      public static void main(String[] args) {
        CubicFrame frame = new CubicFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center