Skip to main content

Java Applet Layout problem

No replies
rad1
Offline
Joined: 2008-01-24

Hi,

I am having problem with the layout, and I have fixed most of it. But, I am still having a problem with displaying the calculation output after clicking the Optionpan button, the output does not appear.

1) Problem with the layout.
2) calculation output is not displaying.
3) Is there a better way to repaint();

I would appreaciat any help to get the applet to display properly.

thank you.
RS

Below is the code:

import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Business extends JApplet implements ActionListener
{
AudioClip sound;
String sPerty = "JParty Planner";
String sMessage1 = "Party Time";
String sMessage2 = "Reserve your seat";
String sMessage3 = "Best Prices Ever";

JLabel lName = new JLabel("JPartyPlanner");
JButton btnTotal = new JButton("Calculate Total");
JLabel lPersonResult = new JLabel("Cost Per Person");
JLabel lTotalResult = new JLabel("Total Cost");
Font fFont = new Font("Helvetica", Font.ITALIC, 18);

JButton startButton = new JButton("Start");
JButton endButton = new JButton("Stop");
Font Times12Font = new Font("TimesRoman", Font.BOLD, 12);

Label lTitle = new Label("Comon On");
int index = 0;
JPanel labelPane = new JPanel();

int[] horiz = {140,150,160,150,140};
int[] vert = {160,150,140,130,120};

int sleep = 100;

public void init()
{
sound = getAudioClip(getCodeBase(),"Chord_Loop1.wav");
setBackground(Color.GRAY);
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(startButton);
con.add(endButton);
lName.setFont(fFont);
con.add(lName);
con.add(btnTotal);
con.add(lPersonResult);
con.add(lTotalResult);
con.add(lTitle);
startButton.addActionListener(this);
endButton.addActionListener(this);
btnTotal.addActionListener(this);

}
public void paint(Graphics g)
{
//repaint();

Graphics2D g2D = (Graphics2D)g;
Font corFont = new Font("Corsiva", Font.BOLD, 18);
g2D.setFont(corFont);
g2D.setColor(Color.BLACK);
g2D.drawString(sPerty,70,60);
g2D.setFont(Times12Font);
g2D.drawString(sMessage1,95,78);
g2D.setColor(Color.RED);
g2D.setFont(corFont);
g2D.drawString(sMessage2,40,940);
g2D.drawString(sMessage3,68,180);

g2D.drawOval(30,30,80,80);
g2D.drawLine(85,110,85,210);
g2D.drawLine(85,210,40,310);
g2D.drawLine(85,210,130,310);
g2D.drawLine(85,140,40,160);
g2D.drawLine(85,140,horiz

,vert
);
++index;
if(index==horiz.length)
{index=0;}

try
{
Thread.sleep(sleep);
}
catch(InterruptedException e)
{}

//repaint();

}

public void start()
{
int index = 0;
}
public void stop()
{

}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();

if(source == btnTotal)
{
String InputBox = JOptionPane.showInputDialog(null, "Please enter the number of guests");
int iGuests = Integer.parseInt(InputBox);
int iIndividualFee = 0, iEventFee = 0;
if(iGuests <= 10)
{
iIndividualFee = 27;
iEventFee = iGuests * 27;
}
else
{
iIndividualFee = 23;
iEventFee = iGuests * 23;
}
lPersonResult.setText("$" + iIndividualFee + " per person");
lTotalResult.setText("Event cost $" + iEventFee);
}

if (source == endButton)
{
sound.stop();
}
else if (source == startButton)
{
sound.loop();repaint();
}
}
}