Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

problem with Adding ScrollBars...............

843806Jan 29 2008 — edited Jan 31 2008
My application contains left pane where there are some buttons which are used to draw on the right side now I need to add scrollbars to right side......

code TreatModuleDraw
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.awt.Scrollbar.*;

import window.arrow.Arrow;

public class TreatModuleDraw extends JFrame implements ActionListener {

	
	private JButton start=new JButton("Start");
	private JButton stop=new JButton("Stop");		
	private JButton linear=new JButton("Linear");
	private JButton andsplit=new JButton("AND-Split");
	private JButton andmerge=new JButton("AND-Merge");
	private JButton orsplit=new JButton("OR-Split");
	private JButton ormerge=new JButton("OR-Merge");
//			ormerge.setSize(20,10);
	
final private int ELLIPSE = 1;
final private int RECT = 2;
private int shape=ELLIPSE;
private int x1=0,y1=70;

	private JPanel shapeSelectionPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

/**Constructor*/
    public TreatModuleDraw() {		shapeSelectionPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        shapeSelectionPanel.setPreferredSize(new Dimension(100, 300));
		add(shapeSelectionPanel,BorderLayout.WEST);

/**adding buttons to the panel*/
		shapeSelectionPanel.add(start);
		shapeSelectionPanel.add(linear);
		shapeSelectionPanel.add(andsplit);
		shapeSelectionPanel.add(andmerge);
		shapeSelectionPanel.add(orsplit);
		shapeSelectionPanel.add(ormerge);
		shapeSelectionPanel.add(stop);
		
/**adding action listners to buttons*/
	  start.addActionListener(this);
	  linear.addActionListener(this);
	  	  andsplit.addActionListener(this);
		  	  andmerge.addActionListener(this);
			  	  orsplit.addActionListener(this);
				  	  ormerge.addActionListener(this);
					  	  stop.addActionListener(this);

					
    setSize(1024,400);


System.out.println("scroll bar aded");

		add(new DrawingPanel());
	}

	public static void main(final String args[]) {
		  TreatModuleDraw obj=new TreatModuleDraw();

		  obj.setVisible(true);
			  obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}

//painting starts here

public void actionPerformed(final ActionEvent e) 
	{
		System.out.print("x="+x1);
		System.out.println("y="+y1);
		x1=x1+150;
        shape = ELLIPSE;
System.out.println("source"+e.getSource());
System.out.println(shape);

        if(e.getSource() == linear) shape = RECT;
    }

class DrawingPanel extends JPanel{// implements AdjustmentListener{

        private Image img;
        private Graphics2D g2;
        Scrollbar sb;

public DrawingPanel(){

JPanel jp=new JPanel();
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
JScrollPane jsp=new JScrollPane(jp,v,h);
getContentPane().add(jsp,BorderLayout.SOUTH);


           addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    drawShape(x1,y1);
                }
            });
      }//end of constructor       		
			
		public void paintComponent(final Graphics g){
            if(img == null){
                img = createImage(getWidth(), getHeight());
                g2 = (Graphics2D)img.getGraphics();
            }
            g.drawImage(img,0,0,null);
        }

        private void drawShape(final int x, final int y) {
			System.out.println(shape);

            switch (shape){
                case RECT: g2.drawRect(x,y,100,50);
				 Arrow.drawHorizArrow(g2, Color.red,    x1-50,  95,  50,  Arrow.RIGHT);
				System.out.println("rect");
				break;

                case ELLIPSE: g2.drawOval(x,y,100,50);
			 Arrow.drawHorizArrow(g2, Color.red,    x1-50,  95,  50,  Arrow.RIGHT);
								System.out.println("ellipse");
            }
            repaint();
        }
    }//drawing class close
}//end of TreatModuleDraw
This is Arrow Class Code
package window.arrow;

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
 

public class Arrow
{
  //  static Font font = new Font("lucida sans demibold", Font.PLAIN, 18);
    public static final int
        UP    = 0,
        LEFT  = 1,
        DOWN  = 2,
        RIGHT = 3;
 
    /**
     * (x,y) is the left end of the arrow
     */
    public static void drawHorizArrow(Graphics2D g2, Color color, int x, int y,
                                     int length, int orientation)
    {
        Color origColor = g2.getColor();
        g2.setPaint(color);
        g2.draw(new Line2D.Double(x, y, x + length, y));
        if(orientation == LEFT)
        {
            g2.draw(new Line2D.Double(x, y, x + 8, y - 8));
            g2.draw(new Line2D.Double(x, y, x + 8, y + 8));
        }
        else
        {
            g2.draw(new Line2D.Double(x + length, y, x + length - 8, y - 8));
            g2.draw(new Line2D.Double(x + length, y, x + length - 8, y + 8));
        }       
    }     
    /**
     * (x,y) is the top of the arrow
     */
    public static void drawVertArrow(Graphics2D g2, Color color, int x, int y,
                                     int length,  int orientation)
    {
        Color origColor = g2.getColor();
        g2.setPaint(color);
        g2.draw(new Line2D.Double(x, y, x, y + length));
        if(orientation == UP)
        {
            g2.draw(new Line2D.Double(x, y, x - 8, y + 8));
            g2.draw(new Line2D.Double(x, y, x + 8, y + 8));
        }
        else
        {
            g2.draw(new Line2D.Double(x, y + length, x - 8, y + length - 8));
            g2.draw(new Line2D.Double(x, y + length, x + 8, y + length - 8));
        }
       
    }
}
Please Help me in adding the scrollbars to the right pane

Thanx in Advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 28 2008
Added on Jan 29 2008
16 comments
96 views