Loop images, java gui? - java

I'm trying to create a java program that generates 10 playing cards. The playing cards are in gif format. I have tried to put these in an array, but do not know how I get on. The playing cards should ideally be displayed without the strict placement. The cards are left anyway. Would need tips to get ahead. As it stands now, I get no error message. But the box that comes up, is empty. What should I do?
Thanks in advance
cards.java
public abstract class cards extends JPanel {
Random gen = new Random();
int noOfCards = 10;
int line;
int col;
boolean faceUp;
String back = "img/b2fv.gif";
JLabel[] stackLabel = new JLabel[noOfCards];
ImageIcon[] stack = new ImageIcon[noOfCards];
ImageIcon [][] cards = {
{new ImageIcon("img/c1.gif"), new ImageIcon("img/d1.gif"), new ImageIcon("img/h1.gif") , new ImageIcon("img/s1.gif")},
{new ImageIcon("img/c2.gif"), new ImageIcon("img/d2.gif"), new ImageIcon("img/h2.gif") , new ImageIcon("img/s2.gif")},
{new ImageIcon("img/c3.gif"), new ImageIcon("img/d3.gif"), new ImageIcon("img/h3.gif") , new ImageIcon("img/s3.gif")},
{new ImageIcon("img/c4.gif"), new ImageIcon("img/d4.gif"), new ImageIcon("img/h4.gif") , new ImageIcon("img/s4.gif")},
{new ImageIcon("img/c5.gif"), new ImageIcon("img/d5.gif"), new ImageIcon("img/h5.gif") , new ImageIcon("img/s5.gif")},
{new ImageIcon("img/c6.gif"), new ImageIcon("img/d6.gif"), new ImageIcon("img/h6.gif") , new ImageIcon("img/s6.gif")},
{new ImageIcon("img/c7.gif"), new ImageIcon("img/d7.gif"), new ImageIcon("img/h7.gif") , new ImageIcon("img/s7.gif")},
{new ImageIcon("img/c8.gif"), new ImageIcon("img/d8.gif"), new ImageIcon("img/h8.gif") , new ImageIcon("img/s8.gif")},
{new ImageIcon("img/c9.gif"), new ImageIcon("img/d9.gif"), new ImageIcon("img/h9.gif") , new ImageIcon("img/s9.gif")},
{new ImageIcon("img/c10.gif"), new ImageIcon("img/d10.gif"), new ImageIcon("img/h10.gif") , new ImageIcon("img/s10.gif")},
{new ImageIcon("img/cj.gif"), new ImageIcon("img/dj.gif"), new ImageIcon("img/hj.gif") , new ImageIcon("img/sj.gif")},
{new ImageIcon("img/cq.gif"), new ImageIcon("img/dq.gif"), new ImageIcon("img/hq.gif") , new ImageIcon("img/sq.gif")},
{new ImageIcon("img/ck.gif"), new ImageIcon("img/dk.gif"), new ImageIcon("img/hk.gif") , new ImageIcon("img/sk.gif")},
{new ImageIcon("img/jr.gif"), new ImageIcon("img/jb.gif")}
};
public JLabel[] genCards(){
try{
for(int i = 0; i < noOfCards; i++){
line = gen.nextInt(14);
col = gen.nextInt(4);
faceUp = gen.nextBoolean();
stack[i] = cards[line][col];
stackLabel[i] = new JLabel();
ImageIcon img;
stackLabel[i].setIcon(img = stack[i]);
this.add(stackLabel[i]);
}
}catch(ArrayIndexOutOfBoundsException e){
System.out.println(e);
System.out.println("Problem med array");
}
return stackLabel;
}
}
table.java
public class table extends cards {
public void paintComponent(Graphics g){
super.paintComponent(g);
for(int i = 0; i < this.stackLabel.length; i++){
//System.out.println(stack[i]);
}
}

ImageIcon is a Swing component, so just add those ImageIcons to a container like a JPanel (perhaps that Cards extends JPanel you have). The container itself draws the Swing components it contains, so you don't have to manually draw them.

At least you have to call to genCards() somewhere. Probably in constructor or a init method:
public Cards(){
genCards();
}

Related

Creating multiple JToggleButton with indivdual ImageIcon

ImageIcon img_1 = new ImageIcon("src/menu/btn_1.jpg");
ImageIcon img_2 = new ImageIcon("src/menu/btn_2.jpg");
ImageIcon img_3 = new ImageIcon("src/menu/btn_3.jpg");
ImageIcon img_4 = new ImageIcon("src/menu/btn_4.jpg");
ImageIcon img_5 = new ImageIcon("src/menu/btn_5.jpg");
ImageIcon img_6 = new ImageIcon("src/menu/btn_6.jpg");
ImageIcon img_7 = new ImageIcon("src/menu/btn_7.jpg");
ImageIcon img_8 = new ImageIcon("src/menu/btn_8.jpg");
ImageIcon img_9 = new ImageIcon("src/menu/btn_9.jpg");
bgNumbers = new ButtonGroup();
btnNumbers = new JToggleButton[9];
for (int i = 0; i < 9; i++) {
btnNumbers[i] = new JToggleButton(???);
.....
.....
}
How do I insert the individuals ImageIcon img_1, img_2 ....... img_9 into the loop?
Add them to an array instead of individual variables. – Gábor Bakos Apr 4 at 7:35

Error running Java code

I am trying to learn Java and have created a GUI form that will eventually look like a Yahtzee score sheet. For now, it has all the fields, but when I try to run it, I get the following error:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Board.<init>(FiveDice.java:97)
at FiveDice.main(FiveDice.java:9)
Ideas?
Here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FiveDice {
public static void main(String[] args)
{
new Board();
} // main
} // FiveDice
class Board extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel l_Ones, l_Twos, l_Threes, l_Fours, l_Fives, l_Sixes,
l_SubtotalT, l_BonusT, l_TotalT;
private JLabel l_ThreeKind, l_FourKind, l_FullHouse, l_SmStr8, l_LgStr8, l_FiveDice,
l_Chance, l_SubtotalB, l_BonusB, l_GrandTotal;
private JTextField Ones, Twos, Threes, Fours, Fives, Sixes,
SubtotalT, BonusT, TotalT;
private JTextField ThreeKind, FourKind, FullHouse, SmStr8, LgStr8, FiveDice,
Chance, SubtotalB, BonusB, GrandTotal;
private JButton btnClear;
public Board() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Six Dice!!");
// Labels - Top Section
l_Ones = new JLabel("1s: ");
l_Twos = new JLabel("2s: ");
l_Threes = new JLabel("3s: ");
l_Fours = new JLabel("4s: ");
l_Fives = new JLabel("5s: ");
l_Sixes = new JLabel("6s: ");
l_SubtotalT = new JLabel("Total: ");
l_BonusT = new JLabel("Bonus: ");
l_TotalT = new JLabel("Grand Total: ");
// Input Fields - Top Section
Ones = new JTextField(2);
Twos = new JTextField(2);
Threes = new JTextField(2);
Fours = new JTextField(2);
Fives = new JTextField(2);
Sixes = new JTextField(2);
SubtotalT = new JTextField(3);
BonusT = new JTextField(3);
TotalT = new JTextField(3);
// Labels - Bottom Section
l_ThreeKind = new JLabel("3 of a Kind: ");
l_FourKind = new JLabel("4 of a Kind: ");
l_FullHouse = new JLabel("Full House: ");
l_SmStr8 = new JLabel("Sm. Straight: ");
l_LgStr8 = new JLabel("Lg. Straight: ");
l_FiveDice = new JLabel("Five Dice: ");
l_Chance = new JLabel("Chance: ");
l_SubtotalB = new JLabel("Total: ");
l_BonusB = new JLabel("Bonus: ");
l_GrandTotal = new JLabel("Grand Total: ");
// Input Fields - Bottom Section
ThreeKind = new JTextField(2);
FourKind = new JTextField(2);
FullHouse = new JTextField(2);
SmStr8 = new JTextField(2);
LgStr8 = new JTextField(2);
FiveDice = new JTextField(2);
Chance = new JTextField(2);
SubtotalB = new JTextField(3);
BonusT = new JTextField(3);
GrandTotal = new JTextField(3);
// Add to Frame - Top Section
add(l_Ones); add(Ones);
add(l_Twos); add(Twos);
add(l_Threes); add(Threes);
add(l_Fours); add(Fours);
add(l_Fives); add(Fives);
add(l_Sixes); add(Sixes);
add(l_SubtotalT); add(SubtotalT);
add(l_BonusT); add(BonusT);
add(l_TotalT); add(TotalT);
// Add to Frame - Bottom Section
add(l_ThreeKind); add(ThreeKind);
add(l_FourKind); add(FourKind);
add(l_FullHouse); add(FullHouse);
add(l_SmStr8); add(SmStr8);
add(l_LgStr8); add(LgStr8);
add(l_FiveDice); add(FiveDice);
add(l_Chance); add(Chance);
add(l_SubtotalB);
add(SubtotalB);
add(l_BonusB);
add(BonusB);
add(l_GrandTotal);
add(GrandTotal);
// Buttons
btnClear = new JButton( "Clear" );
btnClear.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
clear();
}
}); add( btnClear );
setLayout( new FlowLayout() ); // Choose a layout
pack(); // Get rid of extra space
setLocationRelativeTo(null); // Centers the window
setVisible(true);
} // Board Method
private void clear() {
Ones.setText( "" );
Twos.setText( "" );
Threes.setText( "" );
Fours.setText( "" );
Fives.setText( "" );
Sixes.setText( "" );
SubtotalT.setText( "" );
BonusT.setText( "" );
TotalT.setText( "" );
ThreeKind.setText( "" );
FourKind.setText( "" );
FullHouse.setText( "" );
SmStr8.setText( "" );
LgStr8.setText( "" );
FiveDice.setText( "" );
Chance.setText( "" );
SubtotalB.setText( "" );
BonusB.setText( "" );
GrandTotal.setText( "" );
} // clear
} // Board Class
Line 97 is your problem. BonusB is null, so you cannot add it:
add(BonusB);

How to create JButton array?

I have used below code but did not work properly. I have list of JButton objects on the panel but could not click on each button individually.
for(int i=0; i<udataArr.length(); i++) {
userBtn = new JButton();
userLb = new JLabel();
cur1 = userBtn.getCursor();
userBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
Image imgUO = ImageIO.read(getClass().getResource("/resources/img-std.png"));
userBtn.setIcon(new ImageIcon(imgUO));
userBtn.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
userLb.setText((String) udataArr.getJSONObject(i).get("user_name"));
//button[i].setText((String) udataArr.getJSONObject(i).get("user_name"));
panelLeft.add(userBtn);
panelLeft.add(userLb);
panelLeft.add(Box.createVerticalStrut(15));
}
This is the code to create JButton Array
JButton buttons[];
buttons = new JButton[10];
for(int i = 0; i < 10; i++) {
buttons[i] = new JButton(String.valueOf(i));
}

Java -for loop only repeating last value

I have an Array to hold the value from DB. If I try with some default data it working fine but if I get value from DB, its only showing last value.
Default Data;
MenuList menu_data [] = new MenuList[]{};
menu_data = new MenuList[]
{
new MenuList("test","test1") ,
new MenuList("test","test1") ,
new MenuList("test","test1") ,
new MenuList("test","test1") ,
new MenuList("test","test1") ,
new MenuList("test","test1") ,
new MenuList("test","test1") ,
new MenuList("test","test1") ,
new MenuList("test","test1") ,
new MenuList("test","test1") ,
new MenuList("test","test1")
};
Value from DB,
MenuList menu_data [] = new MenuList[]{};
List<Menu> profiles = db.getAllContacts();
for (Menu cn : profiles) {
menu_data = new MenuList[]
{
new MenuList(cn.getmenuname(), cn.getmenuprice())
};
}
How do I get all the value from DB.
Everytime you go through the loop, you are creating a new array. Hence, only the last value is available. Please try the following
menu_data = new MenuList[profiles.size()];
for (int i = 0; i < menu_data.length; i++) {
Menu cn = profiles.get(i);
menu_data[i] = new MenuList(cn.getmenuname(), cn.getmenuprice());
}
Try this:
List<MenuList> menulists = new ArrayList<MenuList>();
for (Menu cn : db.getAllContacts())
menulists.add(new MenuList(cn.getmenuname(), cn.getmenuprice()));
MenuList[] menu_data = menulists.toArray(new MenuList[0]);
List<Menu> profiles = db.getAllContacts();
int numProfiles = profiles.size();
MenuList[] menu_data = new MenuList[numProfiles];
for (int i = 0; i < numProfiles; i++) {
Menu cn = profiles.get(i);
menu_data[i] = new MenuList(cn.getmenuname(), cn.getmenuprice());
}

Change java applet to java application

I have an applet that runs a GUI. I want to call this GUI from my other program. I know that I need to turn this applet into an application. I have an init() and a actionPerformed(ActionEvent ae). How can I do it?
My code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class survey extends Applet implements ActionListener
{
private TextField question;
private Button enter, start;
int count = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
String text, input;
private Label intro1, intro2, intro3;
private Label qone1, qone2, qone3, qone4, qone5, qone6, qone7, qone8, qone9, qone10, qone11, qone12;
private Label qtwo1, qtwo2, qtwo3, qtwo4, qtwo5, qtwo6, qtwo7, qtwo8, qtwo9, qtwo10, qtwo11, qtwo12;
private Label qthree1, qthree2, qthree3, qthree4, qthree5, qthree6, qthree7, qthree8, qthree9, qthree10, qthree11, qthree12;
private Label qfour1, qfour2, qfour3, qfour4, qfour5, qfour6, qfour7, qfour8, qfour9, qfour10, qfour11, qfour12;
private Label qfive1, qfive2, qfive3, qfive4, qfive5, qfive6, qfive7, qfive8, qfive9, qfive10, qfive11, qfive12;
private Label qsix1, qsix2, qsix3, qsix4, qsix5, qsix6, qsix7, qsix8, qsix9, qsix10, qsix11, qsix12;
private Label qseven1, qseven2, qseven3, qseven4, qseven5, qseven6, qseven7, qseven8, qseven9, qseven10, qseven11, qseven12;
private Label qeight1, qeight2, qeight3, qeight4, qeight5, qeight6, qeight7, qeight8, qeight9, qeight10, qeight11, qeight12;
private Label qnine1, qnine2, qnine3, qnine4, qnine5, qnine6, qnine7, qnine8, qnine9, qnine10, qnine11, qnine12;
private Label qten1, qten2, qten3, qten4, qten5, qten6, qten7, qten8, qten9, qten10, qten11, qten12;
private Label qeleven1, qeleven2, qeleven3, qeleven4, qeleven5, qeleven6,
private Label finish1, finish2, finish3;
public void init()
{
setLayout(null);
start = new Button ("Start");
question = new TextField(10);
enter = new Button ("Enter");
if (count == 0)
{
setBackground( Color.yellow);
intro1 = new Label("Target Advertising", Label.CENTER);
intro1.setFont(new Font("Times-Roman", Font.BOLD, 16));
intro2 = new Label("Welcome to this questionnaire. First, we would like to know more about your personal preferences.");
intro3 = new Label("For each question, Input a rating between 0-9 (zero = least interested, 9 = most interested) in the text box. Click enter for next question.");
add(intro1);
add(intro2);
add(intro3);
intro1.setBounds(0,0,800,20);
intro2.setBounds(15,20,800,20);
intro3.setBounds(15,40,800,20);
add(start);
start.setBounds(370,60,70,23);
start.addActionListener(this);
}
if(count == 1)
{
setBackground( Color.yellow );
qone1 = new Label("Question 1", Label.LEFT);
qone1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qone2 = new Label("How much do you like action movies?");
qone3 = new Label("0");
qone4 = new Label("1");
qone5 = new Label("2");
qone6 = new Label("3");
qone7 = new Label("4");
qone8 = new Label("5");
qone9 = new Label("6");
qone10 = new Label("7");
qone11 = new Label("8");
qone12 = new Label("9");
add(qone1);
add(qone2);
add(qone3);
add(qone4);
add(qone5);
add(qone6);
add(qone7);
add(qone8);
add(qone9);
add(qone10);
add(qone11);
add(qone12);
qone1.setBounds(15,0,800,20);
qone2.setBounds(15,20,800,15);
qone3.setBounds(15,60,800,15);
qone4.setBounds(15,80,800,15);
qone5.setBounds(15,100,800,15);
qone6.setBounds(15,120,800,15);
qone7.setBounds(15,140,800,15);
qone8.setBounds(15,160,800,15);
qone9.setBounds(15,180,800,15);
qone10.setBounds(15,200,800,15);
qone11.setBounds(15,220,800,15);
qone12.setBounds(15,240,800,15);
add(question);
add(enter);
question.setBounds(15,260,70,15);
enter.setBounds(90,260,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if (count == 2)
{
qtwo1 = new Label("Question 2", Label.LEFT);
qtwo1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qtwo2 = new Label("How much do you like Science Fiction?");
qtwo3 = new Label("0");
qtwo4 = new Label("1");
qtwo5 = new Label("2");
qtwo6 = new Label("3");
qtwo7 = new Label("4");
qtwo8 = new Label("5");
qtwo9 = new Label("6");
qtwo10 = new Label("7");
qtwo11 = new Label("8");
qtwo12 = new Label("9");
add(qtwo1);
add(qtwo2);
add(qtwo3);
add(qtwo4);
add(qtwo5);
add(qtwo6);
add(qtwo7);
add(qtwo8);
add(qtwo9);
add(qtwo10);
add(qtwo11);
add(qtwo12);
qtwo1.setBounds(15,0,800,20);
qtwo2.setBounds(15,20,800,15);
qtwo3.setBounds(15,60,800,15);
qtwo4.setBounds(15,80,800,15);
qtwo5.setBounds(15,100,800,15);
qtwo6.setBounds(15,120,800,15);
qtwo7.setBounds(15,140,800,15);
qtwo8.setBounds(15,160,800,15);
qtwo9.setBounds(15,180,800,15);
qtwo10.setBounds(15,200,800,15);
qtwo11.setBounds(15,220,800,15);
qtwo12.setBounds(15,240,800,15);
add(question);
add(enter);
question.setBounds(15,260,70,15);
enter.setBounds(90,260,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 3)
{
qthree1 = new Label("Question 3", Label.LEFT);
qthree1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qthree2 = new Label("How much do you like comedy?");
qthree3 = new Label("0");
qthree4 = new Label("1");
qthree5 = new Label("2");
qthree6 = new Label("3");
qthree7 = new Label("4");
qthree8 = new Label("5");
qthree9 = new Label("6");
qthree10 = new Label("7");
qthree11 = new Label("8");
qthree12 = new Label("9");
add(qthree1);
add(qthree2);
add(qthree3);
add(qthree4);
add(qthree5);
add(qthree6);
add(qthree7);
add(qthree8);
add(qthree9);
add(qthree10);
add(qthree11);
add(qthree12);
qthree1.setBounds(15,0,800,20);
qthree2.setBounds(15,20,800,15);
qthree3.setBounds(15,60,800,15);
qthree4.setBounds(15,80,800,15);
qthree5.setBounds(15,100,800,15);
qthree6.setBounds(15,120,800,15);
qthree7.setBounds(15,140,800,15);
qthree8.setBounds(15,160,800,15);
qthree9.setBounds(15,180,800,15);
qthree10.setBounds(15,200,800,15);
qthree11.setBounds(15,220,800,15);
qthree12.setBounds(15,240,800,15);
add(question);
add(enter);
question.setBounds(15,260,70,15);
enter.setBounds(90,260,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 4)
{
qfour1 = new Label("Question 4", Label.LEFT);
qfour1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qfour2 = new Label("How much do you like luxary cars?");
qfour3 = new Label("0");
qfour4 = new Label("1");
qfour5 = new Label("2");
qfour6 = new Label("3");
qfour7 = new Label("4");
qfour8 = new Label("5");
qfour9 = new Label("6");
qfour10 = new Label("7");
qfour11 = new Label("8");
qfour12 = new Label("9");
add(qfour1);
add(qfour2);
add(qfour3);
add(qfour4);
add(qfour5);
add(qfour6);
add(qfour7);
add(qfour8);
add(qfour9);
add(qfour10);
add(qfour11);
add(qfour12);
qfour1.setBounds(15,0,800,20);
qfour2.setBounds(15,20,800,15);
qfour3.setBounds(15,60,800,15);
qfour4.setBounds(15,80,800,15);
qfour5.setBounds(15,100,800,15);
qfour6.setBounds(15,120,800,15);
qfour7.setBounds(15,140,800,15);
qfour8.setBounds(15,160,800,15);
qfour9.setBounds(15,180,800,15);
qfour10.setBounds(15,200,800,15);
qfour11.setBounds(15,220,800,15);
qfour12.setBounds(15,240,800,15);
add(question);
add(enter);
question.setBounds(15,260,70,15);
enter.setBounds(90,260,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 5)
{
qfive1 = new Label("Question 5", Label.LEFT);
qfive1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qfive2 = new Label("How much do you like trucks?");
qfive3 = new Label("0");
qfive4 = new Label("1");
qfive5 = new Label("2");
qfive6 = new Label("3");
qfive7 = new Label("4");
qfive8 = new Label("5");
qfive9 = new Label("6");
qfive10 = new Label("7");
qfive11 = new Label("8");
qfive12 = new Label("9");
add(qfive1);
add(qfive2);
add(qfive3);
add(qfive4);
add(qfive5);
add(qfive6);
add(qfive7);
add(qfive8);
add(qfive9);
add(qfive10);
add(qfive11);
add(qfive12);
qfive1.setBounds(15,0,800,20);
qfive2.setBounds(15,20,800,15);
qfive3.setBounds(15,60,800,15);
qfive4.setBounds(15,80,800,15);
qfive5.setBounds(15,100,800,15);
qfive6.setBounds(15,120,800,15);
qfive7.setBounds(15,140,800,15);
qfive8.setBounds(15,160,800,15);
qfive9.setBounds(15,180,800,15);
qfive10.setBounds(15,200,800,15);
qfive11.setBounds(15,220,800,15);
qfive12.setBounds(15,240,800,15);
add(question);
add(enter);
question.setBounds(15,260,70,15);
enter.setBounds(90,260,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 7)
{
finish1 = new Label("Thank You." , Label.CENTER);
finish1.setFont(new Font("Times-Roman", Font.BOLD, 50));
finish2 = new Label("Questionnaire Completed.", Label.CENTER);
finish2.setFont(new Font("Times-Roman", Font.BOLD, 50));
add(finish1);
add(finish2);
finish1.setBounds(0,200,800,60);
finish2.setBounds(0,300,800,60);
}
}
public void actionPerformed(ActionEvent ae)
{
String button = ae.getActionCommand();
text = question.getText();
b = 0;
c = 0;
if (count == 6)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qsix1);
remove(qsix2);
remove(qsix3);
remove(qsix4);
remove(qsix5);
remove(qsix6);
remove(qsix7);
remove(qsix8);
remove(qsix9);
remove(qsix10);
remove(qsix11);
remove(qsix12);
try{
FileWriter fstream = new FileWriter("lets3.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(new String(input));
out.write("\n");
out.close();
}
catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
if(input.equals("OL"))
{
b = 1;
count = 7;
init();
}
else
{
b = 2;
count = 7;
init();
}
}
if (count == 5)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qfive1);
remove(qfive2);
remove(qfive3);
remove(qfive4);
remove(qfive5);
remove(qfive6);
remove(qfive7);
remove(qfive8);
remove(qfive9);
remove(qfive10);
remove(qfive11);
remove(qfive12);
try{
FileWriter fstream = new FileWriter("lets3.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(new String(input));
out.write("\n");
out.close();
}
catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
if(input.equals("BR"))
{
b = 1;
count = 6;
init();
}
else
{
b = 2;
count = 6;
init();
}
}
if (count == 4)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qfour1);
remove(qfour2);
remove(qfour3);
remove(qfour4);
remove(qfour5);
remove(qfour6);
remove(qfour7);
remove(qfour8);
remove(qfour9);
remove(qfour10);
remove(qfour11);
remove(qfour12);
try{
FileWriter fstream = new FileWriter("lets3.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(new String(input));
out.write("\n");
out.close();
}
catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
if(input.equals("no"))
{
b = 1;
count = 5;
init();
}
else
{
b = 2;
count = 5;
init();
}
}
if (count == 3)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qthree1);
remove(qthree2);
remove(qthree3);
remove(qthree4);
remove(qthree5);
remove(qthree6);
remove(qthree7);
remove(qthree8);
remove(qthree9);
remove(qthree10);
remove(qthree11);
remove(qthree12);
try{
FileWriter fstream = new FileWriter("lets3.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(new String(input));
out.write("\n");
out.close();
}
catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
if(input.equals("black"))
{
b = 1;
count = 4;
init();
}
else
{
b = 2;
count = 4;
init();
}
}
if (count == 2)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qtwo1);
remove(qtwo2);
remove(qtwo3);
remove(qtwo4);
remove(qtwo5);
remove(qtwo6);
remove(qtwo7);
remove(qtwo8);
remove(qtwo9);
remove(qtwo10);
remove(qtwo11);
remove(qtwo12);
try{
FileWriter fstream = new FileWriter("lets3.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(new String(input));
out.write("\n");
out.close();
}
catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
if(input.equals("yes"))
{
b = 1;
count = 3;
init();
}
else
{
b = 2;
count = 3;
init();
}
}
if (count == 1)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qone1);
remove(qone2);
remove(qone3);
remove(qone4);
remove(qone5);
remove(qone6);
remove(qone7);
remove(qone8);
remove(qone9);
remove(qone10);
remove(qone11);
remove(qone12);
try{
FileWriter fstream = new FileWriter("lets3.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(new String(input));
out.write("\n");
out.close();
}
catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
if(input.equals("i"))
{
b = 1;
count = 2;
init();
}
else
{
b = 1;
count = 2;
init();
}
}
if (count == 0)
{
remove(intro1);
remove(intro2);
remove(intro3);
remove(start);
count = 1;
init();
}
this.validate();
}
}
In all honesty, you need to re-write your program from scratch so that you can incorporate OOP techniques, arrays, collections, and other advantages that Java has to offer. I recommend:
First of all since your code displays a series of questions and prompts for response, don't hard-code the questions in the code but make them part of the data. Have your program read in a text file that holds the questions. This will allow you to change questions or add questions without altering code.
Create a non-GUI Question class that holds questions and user responses and that is used by the GUI as its "model".
Create an ArrayList of Question objects.
For your GUI, code to the JPanel, not the applet or the JFrame. This will give you the option of using your GUI in a JFrame or a JApplet, or even a JDialog or embedded in another JPanel should you so desire.
If you will need to swap display panels, consider using CardLayout for this purpose.
If however all you'll be doing is changing the text of the question, then display the question text in a JLabel and when you want to change it, call setText(...) on the JLabel passing in the new question's text.
Use the user-friendly layout managers to ease your work of laying out components in the GUI.
Your current code has a lot of unnecessary redundencies. Use of arrays and collections such as ArrayLists will remove many of these redudancies and make debugging and upgrading much easier.
As others have stated and as I stated in my earlier comment, you should move up to the Swing library as it is much more flexible and robust than the AWT gui library that you are currently using. The Swing tutorials will show you what you need to know to create beautiful Swing programs.
Just add a main() method, make a Frame for your applet, add the applet to the frame, and call the applet's init() and start() methods. See my Mandelbrot.java for an example: http://unixshell.jcomeau.com/src/java/com/jcomeau/Mandelbrot.java
One advantage of this approach is that it can be used with any existing applet, to allow it to function as either an applet or application. Use a JFrame if you're using Swing components, otherwise it should work pretty nearly the same.
In general, you'll want to change all the non swing components to swing components. For events, they're roughly the same for both applets and swing applications. Hope it helps.
I'm not sure what you are asking, but if you want it inside a window all you need to do is this:
public Object[] startNewSurvey()
{
java.awt.Frame f = new java.awt.Frame("You're title here.");
f.setSize(new Dimension(<Width>, <Height>));
f.setResizable(false);
survey s = new survey();
s.init();
f.add(s);
f.setVisible(true);
return new Object[] { f, s };
}
That will just instance a brand new frame and put another new instance of your survey class in that frame, then display it. It also returns a 2 sized Object[] array containing the new Frame and survey instances made. Hope that helps.
https://way2java.com/applets/applet-to-application/
Following are the changes to be made from Applet to Application
Delete the statement "import java.applet" package
Replace extends Applet with Frame
Replace the init() method with constructor
Check the layout (for Applet, the default layout is FlowLayout and for Frame, it is BorderLayout)
Add setXXX() methods like setSize() etc.
Add the main() method
HTML is not required (delete it)

Categories

Resources