adding scrollbars to a Drawingpanel containg java2D shapes
807597Apr 13 2005 — edited Apr 13 2005hi all,
I want to draw a diagram which looks like flowchart diagram with ellipses,lines and rectangles.so for this i am drawing all this shapes using java2D in the the JPanel called DrawingPane and then putting this in to JScrollpane.All these are added in JFrame.but the problem is i am not getting the scrollbars added to scrollpane. so i want the scrollbars added and when i scroll i should see the added shapes in DrawingPane scrolling.I am adding my code below.Please help me how to do this. any kind of help is appreciated.
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*; // For Ellipse2D, etc.
public class weka extends JPanel implements MouseListener
{
private Dimension area; //indicates area taken up by graphics
private JPanel drawingPane;
// Red at (0,0), yellow at (175,175), changes gradually between.
private GradientPaint gradient = new GradientPaint(0, 0, Color.red, 175, 175, Color.yellow,true); // true means to repeat pattern
public weka()
{
super(new BorderLayout());
area = new Dimension(0,0);
//Set up the drawing area.
drawingPane = new DrawingPane();
drawingPane.setBackground(Color.white);
drawingPane.addMouseListener(this);
//Put the drawing area in a scroll pane.
JScrollPane scroller = new JScrollPane(drawingPane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane. HORIZONTAL_SCROLLBAR_ALWAYS);
scroller.setPreferredSize(new Dimension(100,100));
//Lay out this demo.
add(scroller, BorderLayout.CENTER);
}
/** The component inside the scroll pane. */
public class DrawingPane extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
drawGradientCircle(g2d);
}
protected Ellipse2D.Double getCircle(double xe ,double ye ,double we,double he) {
Ellipse2D.Double circle =new Ellipse2D.Double(xe , ye , we, he);
circle.setFrame(xe , ye , we, he);
return(circle);
}
protected Rectangle2D.Double getSquare(double xr ,double yr ,double wr,double hr){
Rectangle2D.Double square =new Rectangle2D.Double(xr , yr , wr, hr);
square.setFrame(xr , yr , wr, hr);
return (square);
}
protected Line2D.Double getLine(double xl1 ,double yl1 , double xl2,double yl2){
Line2D.Double line =new Line2D.Double(xl1 , yl1 , xl2, yl2);
line.setLine(xl1 , yl1 , xl2, yl2);
return (line);
}
protected void drawGradientCircle(Graphics2D g2d)
{
g2d.setPaint(gradient);
g2d.fill(getCircle(100, 10, 150, 50));
g2d.setPaint(Color.black);
g2d.draw(getCircle(100, 10, 150, 50));
g2d.setPaint(gradient);
g2d.fill(getSquare(25, 125, 100, 40));
g2d.setPaint(Color.black);
g2d.draw(getSquare(25, 125, 100, 40));
g2d.draw(getLine(175, 60, 75, 125));
/* g2d.setPaint(gradient);
g2d.fill(getCircle(200, 10, 150, 50));
g2d.setPaint(Color.black);
g2d.draw(getCircle(200, 10, 150, 50));
g2d.setPaint(gradient);
g2d.fill(getSquare(125, 125, 100, 40));
g2d.setPaint(Color.black);
g2d.draw(getSquare(125, 125, 100, 40));
g2d.draw(getLine(305, 60, 375, 125));*/
}
}
//Handle mouse events.
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("WEKA");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new weka();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}