I hav added a JScrollpane to JPanel and the JPanel is added to a JFrame.Then i painted/drawn some rectangles using paintComponet().There r many rectangles drawn but I cant scroll down the JPanel. Is it possible to scroll down a JPanel to see all paintings down this area where some paintings hav been made during runtime.And what if I want to change the color of each rectangle during runtime. I also want to add a button which being pressed will do all this drawing on the JPanel. Or is there any component to draw this way.I hav given the code I hav tried my best, Pls help me, actually I m trying to make a GantChart.
import java.awt.*;
import javax.swing.*;
publicclass drawGraph extends JPanel {
JFrame frm=new JFrame();
JScrollPane sPane=new JScrollPane();
JButton b=new JButton();
public drawGraph() {
this.setBorder(BorderFactory.createLineBorder(Color.red,3));
this.setBackground(Color.darkGray);
this.setBounds(90,290,100,100);
frm.getContentPane().add(sPane,BorderLayout.CENTER);
b.setBounds(30,30,90,190);
sPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sPane.getViewport().add(this);
//sPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frm.setSize(new Dimension(800, 600));
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
frm.setLocation((d.width-800)/2,(d.height-600)/2);
frm.setVisible(true);
}
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
int width=15;
int hight=12;
int down=60;
int strX=140;
int lineX=140;
//double cval;
int cR,cG,cB;
//cval=Math.random();
///////////////////////////FOR DATE TITLE/////////////////////////
for( int i=1; i<32 ; i++ )
{
g.setColor(Color.red);
String st=new String().valueOf(i);
g.setFont(new Font("Monospaced",Font.BOLD+Font.ITALIC,13));
g.drawString(st,strX,45);
g.setColor(Color.magenta);
strX=strX+22;
}
for( int i=1; i<50 ; i++ )
{ g.drawLine(lineX,37,lineX,1000);
lineX=lineX+20;
}
//////////////////////////FOR BAR CHART/TIMELINE/////////////////////////
for( int a=1; a<60 ; a++ ){
cR=160;
cG=46;
cB=98;
g.setColor(new Color(cR,cG,cB));
g.fill3DRect(140,down,width,hight,true);
down=down+20;
width=width+20;
cR=cR+15;
cG=cG+5;
cB=cB+35;
}
}
publicstaticvoid main(String[] args) {
drawGraph dGraph = new drawGraph();
}
}