Hi All,
I am trying to add buttons and I am not able to see them while running. Please let me know where I am going wrong.
Thanks in advance.
KiranJyothi
<CODE>
//KiranJyothi
import java.awt.GridBagLayout;
import javax.swing.*;
import java.awt.Component;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AddButtons extends JFrame implements ActionListener
{
Color bgColor;
public JButton sendPacket, incSpeed, descSpeed, pause, reset, error, kill;
private boolean isError, isKill;
public AddButtons( boolean errorOccur, boolean killed )
{
bgColor = Color.PINK;
JButton sendPacket = new JButton("Transmit Packet");
sendPacket.setBackground(bgColor);
sendPacket.setActionCommand("snp");
sendPacket.addActionListener(this);
JButton kill = new JButton("Kill Packet");
kill.setBackground(bgColor);
kill.setActionCommand("kl");
kill.addActionListener(this);
kill.setEnabled(false);
JButton error = new JButton("Create Error");
error.setBackground(bgColor);
error.setActionCommand("err");
error.addActionListener(this);
JButton pause = new JButton("Pause");
pause.setBackground(bgColor);
pause.setActionCommand("paus");
pause.addActionListener(this);
JButton incSpeed = new JButton("Increase Packet Speed");
incSpeed.setBackground(bgColor);
incSpeed.setActionCommand("fast");
incSpeed.addActionListener(this);
JButton descSpeed = new JButton("Descrease Packet Speed");
descSpeed.setBackground(bgColor);
descSpeed.setActionCommand("slow");
descSpeed.addActionListener(this);
JButton reset = new JButton("Reset Animation");
reset .setBackground(bgColor);
reset.setActionCommand("rst");
reset.addActionListener(this);
add( sendPacket );
add( kill );
add( error );
add( pause );
add( incSpeed );
add( descSpeed );
add( reset );
isError = errorOccur;
isKill = killed;
}
public void setError( boolean errorOccur )
{
isError = errorOccur;
}
public boolean isErrorOccur()
{
return isError;
}
public void setKillPacket( boolean killed )
{
isKill = killed;
}
public boolean ifPacketKilled()
{
return isKill;
}
public void actionPerformed( ActionEvent event )
{//body}
}
//KiranJyothi
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.Color;
public class PacketComponent extends JComponent
{
int horizontalMove, verticleMove;
Color senderPacketColor, recieverPacketColor;
AddButtons buttons = new AddButtons( false, false );
public void paintComponent( Graphics g )
{
Graphics2D g2 = (Graphics2D) g;
horizontalMove = 0;
verticleMove = 0;
senderPacketColor = Color. BLUE;
recieverPacketColor = Color.CYAN;
buttons.draw(g2);
g2.drawString("Transmitter ", horizontalMove+10, verticleMove+80);
for( int count = 1; count <=20; count++ )
{
Packet sender = new Packet( false, false, false, false, 0, 0, horizontalMove+80, verticleMove+80, senderPacketColor );
horizontalMove +=40;
sender.draw( g2 );
}
horizontalMove = 0;
verticleMove = 0;
g2.setColor(Color.BLACK);
g2.drawString("Reciever ", horizontalMove+10, verticleMove+500);
for( int count = 1; count <=20; count++ )
{
Packet reciever = new Packet( false, false, false, false, 0, 0, horizontalMove+80, verticleMove+500, recieverPacketColor );
horizontalMove +=40;
reciever.draw( g2 );
}
}
}
//KiranJyothi
import javax.swing.*;
public class PacketTest
{
public static void main( String a[] )
{
JFrame frame = new JFrame();
final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 700;
frame.setSize( FRAME_WIDTH, FRAME_HEIGHT );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
PacketComponent component = new PacketComponent();
frame.add( component );
frame.setVisible( true );
}
}
</CODE>