Stack Cards horizontally with some offset - java

How do I stack images (of cards) like this:
This is what I have so far and obviously I am trying to set the location of the JLabel cardIcon which gets replaced each time I guess.
JPanel tableMat = new JPanel();
for (CardSet card : playersHand) {
String path = dirPath + card.suit().toString()+"-"+card.rank().toString()+".gif";
File file = new File(path);
if (!file.exists()) {
System.out.println(path);
throw new IllegalArgumentException("file " + file + " does not exist");
} else {
BufferedImage icon = ImageIO.read(new File(file.getAbsolutePath()));
JLabel cardIcon = new JLabel(new ImageIcon(icon));
cardIcon.setLocation(300,300);
tableMat.add(cardIcon);
}
}

tableMat = new JPanel() initialises it with the default FlowLayout, so cardIcon.setLocation(300, 300) will be ignored - the layout manager will decide the position when tableMat.add(cardIcon) is called.
You need to remove the layout manager from tableMat, e.g. tableMat = new JPanel(null).
Of course, you also need to update the x co-ordinate to stagger them left-to-right.
See https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

I ended up doing like this and it works well for me.
JLayeredPane tableMat = new JLayeredPane();
int i =0;
int x_offset = 15;
for (CardSet card : playersHand) {
String path = dirPath + card.suit().toString()+"-"+card.rank().toString()+".gif";
File file = new File(path);
if (!file.exists()) {
System.out.println(path);
throw new IllegalArgumentException("file " + file + " does not exist");
} else {
BufferedImage icon = ImageIO.read(new File(file.getAbsolutePath()));
JLabel cardIcon = new JLabel(new ImageIcon(icon));
cardIcon.setBounds(x_offset,20,300,300);
tableMat.add(cardIcon, new Integer(i));
i++;
x_offset += 15;
}
}
And hence the output is :

Related

Captured frame is rotating 90 degree right in the Server

Frame or image is captured from the Video sent by the android mobile, Used
Jcodec(<artifactId>jcodec</artifactId> and <artifactId>jcodec-javase</artifactId> Version 0.2.2) to capture the image in java. Everything is working fine but while displaying the photo is tilted to right 90 degree. I am not able to find that rotating is happening while capturing the frame or while displaying it!
In local server(tomcat7) working fine(image is in potrait itself) but this issue occured when I push the code to AWS it has tomcat8. AND after downloaded, size of the image(JPEG) from AWS is 28kb, from local server is 118kb.
I am sharing my code here Anyone Please tell me where it is going wrong and any links to solve this issue will be greatful.
Frame Capturing Code:
int frameNumber = 1;
Picture picture = FrameGrab.getFrameFromFile(file, frameNumber);
BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture);
ImageIO.write(bufferedImage, "jpg", new File(id + File.separator + fileName + "_" + fileName + ".jpg"));
Image displying code:
public ResponseEntity<Resource> getPhoto(#PathVariable(value = "id") Integer id) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
String absolutePath = new File(".").getAbsolutePath();
File file = new File(Paths.get(absolutePath).getParent() + "/" + id);
if (null != file) {
FilenameFilter beginswithm = new FilenameFilter() {
public boolean accept(File directory, String filename) {
return filename.startsWith("photo");
}
};
File[] files = file.listFiles(beginswithm);
if (null != files && files.length > 0) {
Resource resource = null;
for (final File f : files) {
headers.set("Content-Disposition", "inline; filename=" + f.getName());
resource = appContext.getResource("file:"
+ Paths.get(absolutePath).getParent() + "/" + id + "/" + f.getName());
return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}
}
}
RecruiterResponseBean resBean = new RecruiterResponseBean();
resBean.setStatusMessage(Constants.FAILED);
resBean.setStatusCode(Constants.FAILED_CODE);
return new ResponseEntity(resBean, HttpStatus.NOT_FOUND);
}
In android, You can use ExifInterface to find out if the image/video has a rotation.
ExifInterface exifInterface = new ExifInterface(mFile.getPath());
int orientation = exifInterface.getAttributeInt(TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(270);
break;
default:
break;
}

Strange combobox behavior in PDFBox

I have this code which creates a combobox in a PDF file. There are two problems with it.
Special characters (like ö) are displayed properly when the combobox is opened but then are not displayable when the combobox is closed.
When I open the PDF in Acrobat, change the value and save the PDF, the combobox is somehow gone. When I open the PDF again it is not displayed anymore.
Did I mess up something with the PDFBox classes or what may be the problem?
Here is a picture in the opened state:
and here is one in the closed state:
public class ComboTest {
public static void main(String[] args) {
PDFont font = PDType1Font.HELVETICA;
Color color = Color.BLACK;
float fontSize = 12;
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDAcroForm acroForm = new PDAcroForm(document);
PDComboBox comboBox = new PDComboBox(acroForm);
comboBox.setPartialName("test");
String defaultAppearanceString = "/" + font.getName() + " " + fontSize + " Tf "
+ 0 + " " + 0 + " " + 0 + " rg";
comboBox.setDefaultAppearance(defaultAppearanceString);
PDAnnotationWidget widget = new PDAnnotationWidget();
widget.setRectangle(new PDRectangle(200, 200, 100, 20));
widget.setAnnotationFlags(4);
widget.setPage(page);
widget.setParent(comboBox);
List<String> exportValues = new ArrayList<>();
List<String> displayValues = new ArrayList<>();
displayValues.add("öne");
displayValues.add("two");
displayValues.add("thrée");
exportValues.add("1");
exportValues.add("2");
exportValues.add("3");
comboBox.setOptions(exportValues, displayValues);
List<PDAnnotationWidget> widgets = new ArrayList<>();
widgets.add(widget);
try {
page.getAnnotations().add(widget);
} catch (IOException e) {
e.printStackTrace();
}
comboBox.setWidgets(widgets);
try {
FileOutputStream output = new FileOutputStream("test.pdf");
document.save(output);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Near the end of your code, add this:
acroForm.getFields().add(comboBox);
document.getDocumentCatalog().setAcroForm(acroForm);
this makes sure that your acroform and its field is known to the PDF.
Re the special character, replace the name of the Helvetica font with "Helv", which is a standard name for Adobe.
Better, cleaner solution: set up the default resources.
PDResources dr = new PDResources();
dr.put(COSName.getPDFName("Helv"), font);
acroForm.setDefaultResources(dr);
Instead of "Helv" you can also use COSName.getPDFName(font.getName()), but it has to be the same in your default appearance string.
So the full code is now:
public class ComboTest
{
public static void main(String[] args)
{
PDFont font = PDType1Font.HELVETICA;
Color color = Color.BLACK;
float fontSize = 12;
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDAcroForm acroForm = new PDAcroForm(document);
PDComboBox comboBox = new PDComboBox(acroForm);
comboBox.setPartialName("test");
// Helv instead of Helvetica
String defaultAppearanceString = "/Helv " + fontSize + " Tf "
+ 0 + " " + 0 + " " + 0 + " rg";
comboBox.setDefaultAppearance(defaultAppearanceString);
PDAnnotationWidget widget = new PDAnnotationWidget();
widget.setRectangle(new PDRectangle(200, 200, 100, 20));
widget.setAnnotationFlags(4);
widget.setPage(page);
widget.setParent(comboBox);
List<String> exportValues = new ArrayList<>();
List<String> displayValues = new ArrayList<>();
displayValues.add("öne");
displayValues.add("two");
displayValues.add("thrée");
exportValues.add("1");
exportValues.add("2");
exportValues.add("3");
comboBox.setOptions(exportValues, displayValues);
List<PDAnnotationWidget> widgets = new ArrayList<>();
widgets.add(widget);
try
{
page.getAnnotations().add(widget);
}
catch (IOException e)
{
e.printStackTrace();
}
comboBox.setWidgets(widgets);
// new
acroForm.getFields().add(comboBox);
document.getDocumentCatalog().setAcroForm(acroForm);
PDResources dr = new PDResources();
dr.put(COSName.getPDFName("Helv"), font);
acroForm.setDefaultResources(dr);
try
{
FileOutputStream output = new FileOutputStream("test.pdf");
document.save(output);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

Display DICOM file on screen

I am having some trouble displaying a dicom file. I have tried many things to display the picture onto the frame but all it does is make an error which is "image is empty". Any suggestions would be appreciated. (I know that the image is not empty and that it is able to access the image from the laptop).
Thank you.
Here is my code (which is coded in java):
class Open{
public static void main(String s) throws IOException{
String [] array = s.split("/");
String k = array[0];
k+= "/";
k += array[1];
k+= "/";
k += array[2];
k+= "/";
k+= array[3];
k+= "/";
k+= array[4];
k+= "/";
System.out.println(k);
System.out.println(s.length());
if(s.length() == 0){
System.out.println("The path and filename are empty!");
System.exit(0);
}
File source = new File(k, array[5]);
final Image image = ImageIO.read(source);
//final BufferedImage image = ImageIO.read(new File(k, array[5]));
if(image == null){
System.out.println("The image is empty or can't be read!");
System.exit(0);
}
JFrame frame = new JFrame();
final Rectangle bounds = new Rectangle(0,0,240, 240);
JPanel panel = new JPanel();
//{
// public void paintComponent(Graphics g){
//// Rectangle box = g.getClipBounds();
//// ((Graphics2D)g).fill(box);
////
//// if(bounds.intersects(box)){
// g.drawImage(image,0,0,null);
//// }
// }
// };
JLabel b = new JLabel(new ImageIcon(image));
panel.add(b);
frame.getContentPane().add(panel);
panel.setPreferredSize(new Dimension(300, 300));
frame.pack();
frame.setVisible(true);
}
}
You can do it with PixelMed. The documentation is pretty much non-existent, but there are javadocs.
If you primarily intend to display the image, you should be able to use SourceImage and SingleImagePanel from the com.pixelmed.display package to load and display the image. If you want to parse DICOM attributes, you could start with one of the read methods in com.pixelmed.dicom.AttributeList.

How can I get an Image and then Use it to make an ImageIcon in Java GUI

I am trying to make a card game and Am running into an issue with my getImage() function.
I have an String Array of the Cards ex:
private static String[] hearts = {"ah.gif","2h.gif","3h.gif","4h.gif","5h.gif","6h.gif","7h.gif","8h.gif","9h.gif","th.gif","jh.gif","qh.gif","kh.gif"};
My getImage() looks like this:
public String getImage(Card card){
if (0 == suit){
img = hearts[rank];
}
else if (1 == suit){
img = spades[rank];
}
else if (2 == suit){
img = diamond[rank];
}
else if (3 == suit){
img = clubs[rank];
}
However, because it is stored as a string, I get an error when I try to use the img as an ImgIcon Ex:
ImageIcon image = (card.getImage(card));
JLabel label = new JLabel(image);
Any Ideas? Thanks
Create an array of ImageIcons and use the String array and a for loop to create the Icon array. Simple. :)
// in declaration section
private ImageIcon heartsIcons = new ImageIcon[hearts.length];
// in code section
try {
for (int i = 0; i < hearts.length; i++) {
// you may need different code to get the Image file vs. resource.
BufferedImage img = ImageIO.read(getClass().getResource(hearts[i]));
heartsIcons[i] = new ImageIcon(img);
}
} catch (IOException e) {
e.printStackTrace();
}

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