Following is my program i need to add scroll to jframe and I have paint method in it
package other;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.ScrollPane;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import javax.swing.*;
public class MainDrawCycle extends JPanel implements ActionListener
{
JButton button;
JLabel label;
JTextField textField;
JScrollPane pane;
JPanel panel;
JFrame frame;
Container c;
static int val;
int xa,ya,xb,yb,x1,y1;
int xc,yc,xd,yd,xe,ye,xf,yf;
int lsb=0,msb=7;
char val1='A',val2='B';
// public MainDrawCycle(JFrame f)
// {
// pane=new //JScrollPane(f,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// add(pane);
// }
public MainDrawCycle()
{
setLayout(null);
//frame=new JFrame();
// panel=new JPanel();
// pane=new //JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
textField=new JTextField();
label=new JLabel("Enter Clock value: ");
button=new JButton("Ok");
System.out.println("in const");
label.setBounds(300,550,80,20);
textField.setBounds(410,550,50,20);
button.setBounds(480,550,70,20);
//add(pane);
add(button);
add(textField);
add(label);
button.addActionListener(this);
}
public void paint(Graphics g)
{
xa=50;ya=60;xb=100;yb=60;x1=100;y1=520;
xc=100;yc=60;xd=100;yd=100;xe=100;ye=100;xf=150;yf=100;
g.setColor(Color.black);
g.drawLine(50,50,50,y1); //Y axis
//g.drawLine(20,500,x1,500); //X axis
g.drawString("CLK",xa-40 ,ya);
g.drawString("DATA", xa-40, 300);
g.drawString("["+msb+":"+lsb+"]", xa-40, 310);
Graphics2D g2d = (Graphics2D)g;
Stroke stroke=new BasicStroke(1f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL,1f, new float[] {2f}, 0f);
Stroke drawingStroke = new BasicStroke(2);
for(int i=1;i<=val;i++)
{
g2d.setStroke(stroke);
Line2D line = new Line2D.Double(xb,yb,xb,500);
g2d.draw (line);
Line2D line2 = new Line2D.Double(xf,yf-50,xf,500);
g2d.draw (line2);
g2d.setStroke(drawingStroke);
if(x1<xf)
{
x1=xf;
g.setColor(Color.black);
g.drawLine(20,500,x1,500); //X axis
}
g.drawString("t "+i,xa+30,ya-10);
g.setColor(Color.blue);
g.drawLine(xa,ya,xb,yb); //h1
g.setColor(Color.blue);
g.drawLine(xc,yc,xd,yd); //v1
g.setColor(Color.blue);
g.drawLine(xe,ye,xf,yf); //h2
if(i%2==0)
g.drawString("A"+val1,(xa+xf)/2, 300);
else
g.drawString("B"+val2,(xa+xf)/2 , 300);
g.drawLine(xa, 300, (xa+xb)/2, 275);
g.drawLine((xa+xb)/2, 275, (xd+xf)/2, 275);
g.drawLine((xd+xf)/2, 275, xf, 300);
g.drawLine((xa+xb)/2, 325, (xd+xf)/2, 325);
g.drawLine(xa, 300, (xa+xb)/2, 325);
g.drawLine((xd+xf)/2, 325, xf, 300);
xa=xf;
ya=ya;
xb=xe+100;
yb=yb;
xc=xb;
yc=yb;
xd=xf+50;
yd=yf;
xe=xd;
ye=yd;
xf=xe+50;
yf=yf;
}
{
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
JScrollPane pane=new JScrollPane(new MainDrawCycle(),JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.getContentPane().add(pane);
// new MainDrawCycle(frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,700);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
val=Integer.parseInt(textField.getText());
repaint();
}
}
}