Help needed with layout and drawing rectangles


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Help needed with layout and drawing rectangles

  1. #1
    Join Date
    Jan 2007
    Posts
    60

    Help needed with layout and drawing rectangles

    I am having trouble with a layout manager to lay out a gridbag but it's not working properly.

    I am trying to have a combo box in grid (0,0)
    a label in grid (1,0)
    a text field in grid (0,1)
    and a button in grid (1,1)

    all the components are currently on 1 line...is this because of the JPanel?

    also I have drawn 2 rectangles and I would like to fill them which I can do but I would like to fill them so there is still a border around the rectangles.

    sjalle did help me with a similar problem but I am trying to make sense of it by recreating my own version which I can understand

    I got the examples combined i got from a book so I need someone to fill in the gaps

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    
    public class ProgressBar extends JApplet implements Runnable, ActionListener
    {
    	Thread runner;
    	JButton play;
    	JButton stop;
    	JTextField text;
    	JLabel textLabel;
    	
      	public void init() 
    	{	
    		Container contentArea = getContentPane();	
        	             GridLayout grid = new GridLayout(2,1);
    		contentArea.setLayout(grid);				
    		contentArea.setBackground(Color.white);
    				
    		JPanel console = new JPanel();
        	             GridBagLayout gridbag = new GridBagLayout();
        	             GridBagConstraints pos = new GridBagConstraints();
    		console.setLayout(gridbag);
    		console.setBackground(Color.white);
    				
    		ProgressPanel animation = new ProgressPanel();
    		
    		JComboBox standards = new JComboBox();
    		standards.addItem("Corfu");
    		standards.addItem("Kefalonia");
    		standards.addItem("Crete");
    		standards.addItem("Rhodes");
    		standards.addItem("Paxos");
    		standards.addItem("Mykonos");
    		pos.gridwidth = 3;
    		pos.gridx = 0; pos.gridy = 0;
    
    		textLabel = new JLabel("Fun programming :");
    		pos.gridx = 1; pos.gridy = 0;
    		
    		text = new JTextField("", 15);
    		pos.gridx = 0; pos.gridy = 1;
    						
    		play = new JButton("Click To Start");
    		play.addActionListener(this);
    		pos.gridx = 1; pos.gridy = 1;
    			
    		console.add(standards);
    		console.add(play);
    		console.add(textLabel);
    		console.add(text);
    		contentArea.add(console);
    		contentArea.add(animation);
        	             setContentPane(contentArea);	
      	}
    		
    	public void actionPerformed(ActionEvent event)
    	{
    		if(event.getSource()==stop) stop();
    		if(event.getSource()==play) start();
    	}
    
      	class ProgressPanel extends JPanel
      	{
    		public void paintComponent(Graphics painter)
        	{				 
    			Graphics2D painter2D = (Graphics2D) painter;
    			
    			painter2D.setColor(Color.black);
    			
    			Rectangle2D.Float progressBar1 = new Rectangle2D.Float(30F, 30F, 360F, 30F);
    			painter2D.draw(progressBar1);
    			painter2D.drawString("Theoretical Transfer Speed", 30, 25);
    			
    			Rectangle2D.Float progressBar2 = new Rectangle2D.Float(30F, 90F, 360F, 30F);
    			painter2D.draw(progressBar2);
    			painter2D.drawString("Realistic Transfer Speed", 30, 85);	
    		}
    	}
    	
    	public void start()
    	{
    		if(runner == null) 
    		{
    			runner = new Thread(this);
    			runner.start();
    		}
    	} 
    	 
    	public void stop()
    	{
    		if(runner != null) runner = null;
    	}
    		
      	public void run()
    	{
    		while (runner == Thread.currentThread() )
        	{
    			repaint();
    			try
    			{
    				Thread.sleep(100);
    			}
    			catch(InterruptedException e)
    			{
    			}
    		}
    	}		
    }

  2. #2
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    I Think You Need This One

    /**
    * @param g : the displays graphical context
    */
    public void update (Graphics g) {
    /**
    * The dimensioning of the buffered image is depending on the size of the
    * canvas. So we have to create it here, since invocation of this method
    * only occurs when the component is displayed, - i.e. has a size. If we had
    * done this in the constructor the size would have been 0,0
    */
    if (memG==null) { // is only true first time around
    prepareDoubleBuffering();

    AND MORE....

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