I have a tensorflow lite model which use,
input is {1,320,320,3}
output array dataType is {FLOAT32,FLOAT32,INT32}.
Interpreter#runForMultipleInputsOutputs crashed when resized the input to {1,224,320,3}.
My tensorflow lite version:
implementation('org.tensorflow:tensorflow-lite:0.0.0-nightly') { changing = true }
implementation('org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly') { changing = true }
implementation('org.tensorflow:tensorflow-lite-support:0.0.0-nightly') { changing = true }
Create the input tensor. Prepare the output tensor:
inputImageBuffer = new TensorImage(imageDataType);
outputArr = new TensorBuffer[tflite.getOutputTensorCount()];
resizeOutputArr = new TensorBuffer[tflite.getOutputTensorCount()];
for (int i = 0, count = tflite.getOutputTensorCount(); i < count; i++) {
DataType pDataType = tflite.getOutputTensor(i).dataType();
if (pDataType == DataType.INT32) pDataType = DataType.FLOAT32;
outputArr[i] = TensorBuffer.createFixedSize(i == 0 ? new int[]{imageSizeY, imageSizeX} : new int[]{imageSizeX}, pDataType);
resizeOutputArr[i] = TensorBuffer.createDynamic(pDataType);
}
Resize the input imagebuffer
// Loads bitmap into a TensorImage.
inputImageBuffer.load(bitmap);
float sc = bitmap.getWidth() / 320.0f;
int scaleWid = Math.round(bitmap.getWidth() / sc);
int scaleHei = Math.round(bitmap.getHeight() / sc);
imageSizeY = scaleHei / 32 * 32;
// Creates processor for the TensorImage.
int numRotation = sensorOrientation / 90;
this.processor =
new ImageProcessor.Builder()
.add(new ResizeOp(scaleHei, scaleWid, ResizeOp.ResizeMethod.NEAREST_NEIGHBOR))
.add(new ResizeWithCropOrPadOp(imageSizeY, imageSizeX))
.add(new Rot90Op(numRotation))
.add(new GrayOp(imageSizeY, imageSizeX))
.add(getPreprocessNormalizeOp())
.build();
processor.process(inputImageBuffer);
Run:
int imageTensorIndex = 0;
int[] imageShape = tflite.getInputTensor(imageTensorIndex).shape(); // {1, height, width, 3}
if (imageSizeY != imageShape[1] || imageShape[1] == 0) { // imageSizeY changed
tflite.resizeInput(imageTensorIndex, new int[]{1, imageSizeY, imageSizeX, 3}); //resize inputTensor
for (int i = 0, count = tflite.getOutputTensorCount(); i < count; i++) { //recreate outputTensor
DataType pDataType = tflite.getOutputTensor(i).dataType();
if (pDataType == DataType.INT32) pDataType = DataType.FLOAT32;
outputArr[i] = TensorBuffer.createFixedSize(i == 0 ? new int[]{imageSizeY, imageSizeX} : new int[]{imageSizeX}, pDataType);
}
}
Map<Integer, Object> outputMap = new HashMap<>();
for (int i = 0, cnt = outputArr.length; i < cnt; i++) { //rewind outputBuffer
outputMap.put(i, outputArr[i].getBuffer().rewind());
resizeOutputArr[i].getBuffer().rewind();
}
try {
tflite.runForMultipleInputsOutputs(new Object[]{inputImageBuffer.getBuffer()}, outputMap);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return null;
}
for (int i = 0, count = tflite.getOutputTensorCount(); i < count; i++) { //resize outputArr with new shape
int[] shape = tflite.getOutputTensor(i).shape();
outputArr[i].getBuffer().limit(computeFlatSize(shape) * DataType.FLOAT32.byteSize());
resizeOutputArr[i].loadBuffer(outputArr[i].getBuffer(), shape);
outputArr[i].getBuffer().clear();
if (i == count - 1 && shape != null && shape[0] <= 0) return null;
}
return resizeOutputArr;
protected static int computeFlatSize(#NonNull int[] shape) {
SupportPreconditions.checkNotNull(shape, "Shape cannot be null.");
int prod = 1;
for (int i = 0; i < shape.length; ++i) {
prod *= shape[i];
}
return prod;
}
I got blew Exception when there is no recognized object in the Bitmap:
W: java.lang.IllegalArgumentException: Internal error: Tensor hasn't been allocated.
W: at org.tensorflow.lite.Tensor.buffer(Native Method)
W: at org.tensorflow.lite.Tensor.buffer(Tensor.java:493)
W: at org.tensorflow.lite.Tensor.copyTo(Tensor.java:264)
W: at org.tensorflow.lite.Tensor.copyTo(Tensor.java:254)
W: at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:170)
W: at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:343)
W: at com.fotric.irdetector.qf.tflite.Classifier.recognizeImage(Classifier.java:283)
This exception reduces the recognition performance, Someone help me, thanks!!!
My stacked bar chart in MPAndroidChart works very well but I'm trying to remove or edit the values inside the bars. But I can't find any code which makes sense to modify.
I don't think that my code is very helpful, but here it is...
private void initStackedBarChartStuff() {
mMonths = dfs.getShortMonths();
mChart = (BarChart) mContext.findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
mChart.setDescription("");
// if more than 60 entries are displayed in the chart, no values will be
// drawn
mChart.setMaxVisibleValueCount(30);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(false);
// change the position of the y-labels
YAxis yLabels = mChart.getAxisLeft();
yLabels.setValueFormatter(new MyYAxisValueFormatter());
mChart.getAxisRight().setEnabled(false);
mChart.getAxisLeft().setEnabled(false);
XAxis xLabels = mChart.getXAxis();
xLabels.setPosition(XAxis.XAxisPosition.TOP);
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.BELOW_CHART_RIGHT);
l.setFormSize(8f);
l.setFormToTextSpace(4f);
l.setXEntrySpace(6f);
// mChart.setDrawLegend(false);
}
fill the stackedBarChart
private void fillStackedBarChart(Vector drawingData, Vector drawingIntervall) {
if (drawingData == null || drawingData == null)
return;
int anzBdata = dataSizes[1];
int intervall = globalIntervall;
ArrayList<String> xVals = new ArrayList<String>();
// Strings über die Blöcke schreiben ###########################################
{
long zeit = 0;
String[] months = new String[12];
String[] days = new String[7];
boolean einmal = true;
for (int i = 0; i < drawingIntervall.size(); i++)
zeit += ((Long) drawingIntervall.get(i)).longValue();
if (bigData) // > 2 Jahre
months = null;
else if (zeit > 31623000) // > 1 Jahre
months = dfs.getShortMonths();
else
months = dfs.getMonths();
days = dfs.getWeekdays();
for (int i = 0; i < drawingData.size(); i++) {
long[] pack = (long[]) drawingData.get(i);
String s = "";
// Sonderfall !! In Monatsansicht den Monat dazu schreiben
if ((einmal) && (intervall == 2)) {
if (etStart.getText().toString().trim().startsWith("01.")) {
Calendar calE = convStringToDate(etEnd.getText().toString().trim(), true, true);
int jahr = calE.get(Calendar.YEAR);
int monat = calE.get(Calendar.MONTH);
Calendar calX = myGetInstance();
calX.set(Calendar.YEAR, jahr);
calX.set(Calendar.MONTH, monat);
calX = getLastDayOfMonth(calX);
if (calE.getTimeInMillis() == calX.getTimeInMillis()) {
einmal = false;
s = months[calE.get(Calendar.MONTH)];
}
}
}
switch (intervall) {
case 0:
s += df.format(convMilliToDate(pack[0] * 1000, false, false).getTime());
break;
case 1:
s += convMilliToString(pack[0] * 1000);
break;
case 2:
s += "KW " + convMilliToDate(pack[0] * 1000, false, false).get(Calendar.WEEK_OF_YEAR);
break;
case 3:
if (bigData)
s += convMilliToDate(pack[0] * 1000, false, false).get(Calendar.YEAR) + "";
else
s += months[convMilliToDate(pack[0] * 1000, false, false).get(Calendar.MONTH)];
break;
}
xVals.add(s);
}
}
// nun alle Balken/Blöcke zeichnen ##########################################
{
// 1 Y-Wert entspricht z.B. 1 Tag, o. 1 Woche,... jeder Y-Wert hat so viele floats wie B-Daten vorhanden sind ###########################################
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < drawingData.size(); i++) {
long[] pack = (long[]) drawingData.get(i);
long intervallSek = ((Long) drawingIntervall.get(i)).longValue() + 1;
long pastSek = 0;
float[] f = new float[anzBdata + 1];
int b = 0;
for (int j = 0; j < pack.length; j++) {
if ((j >= dataSizes[0]) && (j <= (anzBdata - 1 + dataSizes[0]))) {
f[b++] = pack[j];
pastSek += pack[j];
}
}
f[b++] = intervallSek - pastSek; // zeichnet einen Block indem "keine Daten" aufgezeichnet wurden. (wenn Maschine ohne Strom o.ä.)
yVals1.add(new BarEntry(f, i));
}
// BarDataSet set1 = new BarDataSet(yVals1, "Statistics Business Data");
BarDataSet set1 = new BarDataSet(yVals1, "");
set1.setColors(getColors());
set1.setStackLabels(barChartLegend);
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
dataSets.add(set1);
BarData data = new BarData(xVals, dataSets);
data.setValueFormatter(new MyValueFormatter());
mChart.setData(data);
mChart.invalidate();
}
}
This solution is not really pretty, may somone else find a better one...
The class BarChart extends from BarLineChartBase. There is a line in BarLineChartBase which can be annotated or removed to get the desired solution.
mRenderer.drawValues(canvas);
There are basically two ways:
Use the ValueFormatter to custom format your values (edit or remove)
Use dataSet.setDrawValues(false) to completely remove all values
i've created a BarChart using jfreechart in java. The bar is plotted according to the days in a month. for example this month March contains 31 days, so there will be 31 bars . The problem is that when i click the next button the month changes to April which contains 30 days, ie 30 bars. How can we change the BarChart according to the days in a month on button click.
Can anyone please tell me how to do that.
You need to update your dataset with each change. I've added an updateDataset() method and called it in several key places.
private void updateDataset() {
dataset.clear();
for (int i = 1; i <= finalday; i++) {
dataset.setValue(i, "Marks", "" + i);
}
Notes:
Do not use absolute layout; let the layout do its work.
Do not do date arithmetic yourself; use Calendar, for example.
Do not call overridable methods in the constructor.
Do re-factor your code to limit the number and scope of variables.
Do use meaningful names, especially for instance variables.
SSCCE, incompletely revised:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
public class BarGraph {
public int count = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0;
public int contstatus = 2;
public int date, year, mon, fn, show = 0, finalday, leapcn = 0, task = 0;
String startdte, enddte, monthweek, leavedates = "", nneed = "", year4enab, month4enab, days;
ChartFrame frame;
public static int st = 0;
JButton left = new JButton("<");
JButton right = new JButton(">");
JComboBox month = new JComboBox();
JSpinner yearspin = new javax.swing.JSpinner();
JLabel monthLabel = new javax.swing.JLabel();
JLabel yearLabel = new javax.swing.JLabel();
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
BarGraph() {
task = 1;
Calendar cal;
left.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
left.setRequestFocusEnabled(false);
left.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
leftActionPerformed(evt);
}
});
right.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
right.setRequestFocusEnabled(false);
right.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
rightActionPerformed(evt);
}
});
month.setMaximumRowCount(12);
month.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}));
month.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
month.addItemListener(new java.awt.event.ItemListener() {
#Override
public void itemStateChanged(java.awt.event.ItemEvent evt) {
monthItemStateChanged(evt);
}
});
yearspin.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
yearspin.setValue(year);
yearspin.addChangeListener(new javax.swing.event.ChangeListener() {
#Override
public void stateChanged(javax.swing.event.ChangeEvent evt) {
yearspinStateChanged(evt);
}
});
monthLabel.setBackground(new java.awt.Color(255, 255, 255));
monthLabel.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
monthLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
monthLabel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, java.awt.Color.black, null, null));
monthLabel.setOpaque(true);
monthLabel.setText("SEPTEMBER");
yearLabel.setBackground(new java.awt.Color(255, 255, 255));
yearLabel.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
yearLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
yearLabel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, java.awt.Color.black, null, null));
yearLabel.setOpaque(true);
yearLabel.setText("2013");
cal = new GregorianCalendar();
year = cal.get(Calendar.YEAR);
mon = cal.get(Calendar.MONTH);
date = cal.get(Calendar.DATE);
yearspin.setValue(year);
month.setSelectedIndex(mon);
calendar(year, mon);
month4enab = getMonth(monthLabel.getText());
year4enab = yearLabel.getText();
startdte = year4enab + "-" + month4enab + "-01";
enddte = year4enab + "-" + month4enab + "-" + finalday;
}
public static void main(String[] args) {
new BarGraph().showBar();
}
private void leftActionPerformed(ActionEvent evt) {
show = 0;
if (mon == 0) {
year--;
mon = 11;
} else {
mon--;
}
yearspin.setValue(year);
month.setSelectedIndex(mon);
display();
calendar(year, mon);
displayCalendar();
updateDataset();
}
private void rightActionPerformed(ActionEvent evt) {
show = 0;
if (mon == 11) {
year++;
mon = 0;
} else {
mon++;
}
yearspin.setValue(year);
month.setSelectedIndex(mon);
display();
calendar(year, mon);
displayCalendar();
updateDataset();
}
private void displayCalendar() {
month4enab = getMonth(monthLabel.getText());
year4enab = yearLabel.getText();
startdte = year4enab + "-" + month4enab + "-01";
enddte = year4enab + "-" + month4enab + "-" + finalday;
}
private void monthItemStateChanged(ItemEvent evt) {
show = 0;
String s = (String) month.getSelectedItem();
if ("January".equals(s)) {
mon = 0;
}
if ("February".equals(s)) {
mon = 1;
}
if ("March".equals(s)) {
mon = 2;
}
if ("April".equals(s)) {
mon = 3;
}
if ("May".equals(s)) {
mon = 4;
}
if ("June".equals(s)) {
mon = 5;
}
if ("July".equals(s)) {
mon = 6;
}
if ("August".equals(s)) {
mon = 7;
}
if ("September".equals(s)) {
mon = 8;
}
if ("October".equals(s)) {
mon = 9;
}
if ("November".equals(s)) {
mon = 10;
}
if ("December".equals(s)) {
mon = 11;
}
display();
calendar(year, mon);
updateDataset();
}
private void yearspinStateChanged(ChangeEvent evt) {
show = 0;
year = (Integer) yearspin.getValue();
display();
calendar(year, mon);
updateDataset();
}
public void display() {
String month = "", day = "";
if (mon < 9) {
month = "0" + (mon + 1);
} else {
month = "" + (mon + 1);
}
if (nneed.length() < 2) {
day = "0" + nneed;
} else {
day = "" + nneed;
}
String datss = year + "-" + (month) + "-" + day;
switch (mon) {
case 0:
monthLabel.setText("JANUARY");
break;
case 1:
monthLabel.setText("FEBRUARY");
break;
case 2:
monthLabel.setText("MARCH");
break;
case 3:
monthLabel.setText("APRIL");
break;
case 4:
monthLabel.setText("MAY");
break;
case 5:
monthLabel.setText("JUNE");
break;
case 6:
monthLabel.setText("JULY");
break;
case 7:
monthLabel.setText("AUGUST");
break;
case 8:
monthLabel.setText("SEPTEMBER");
break;
case 9:
monthLabel.setText("OCTOBER");
break;
case 10:
monthLabel.setText("NOVEMBER");
break;
case 11:
monthLabel.setText("DECEMBER");
break;
}
yearLabel.setText(String.valueOf(year));
}
public void calendar(int year, int mon) {
int year1 = year;
int count1 = 1, fun = 0, day, day1 = 0, key = 0, k = -1, s = 1, ck = 2, cheak, y = 0;
day1 = year % 100;
fun = 1 + (day1 / 4);
switch (mon) {
case 0:
key = 1;
k = -1;
break;
case 1:
key = 4;
k = 0;
break;
case 2:
key = 4;
k = 0;
break;
case 3:
key = 0;
k = 0;
break;
case 4:
key = 2;
k = 0;
break;
case 5:
key = 5;
k = 0;
break;
case 6:
key = 0;
k = 0;
break;
case 7:
key = 3;
k = 0;
break;
case 8:
key = 6;
k = 0;
break;
case 9:
key = 1;
k = 0;
break;
case 10:
key = 4;
k = 0;
break;
case 11:
key = 6;
k = 0;
break;
}
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
k = -1;
day = 29;
leapcn = 0;
if (mon == 0 || mon == 1) {
leapcn = 1;
}
} else {
k = 0;
day = 28;
leapcn = 1;
}
if (year >= 2000) {
y = 6;
}
if (year <= 1900 && year > 2000) {
y = 0;
}
fun = fun + key + k + y;
fun = (fun + day1) % 7;
if (fun == 0) {
fun = 7;
}
if (mon == 0 || mon == 2 || mon == 4 || mon == 6 || mon == 7 || mon == 9 || mon == 11) {
day = 31;
}
if (mon == 3 || mon == 5 || mon == 8 || mon == 10) {
day = 30;
}
finalday = day;
System.out.println("DAYS IN THIS MONTHS:" + finalday);
cheak = fun + 1;
s++;
}
public static String getMonth(String s) {
String mont = "56";
if ("January".equalsIgnoreCase(s)) {
mont = "01";
} else if ("February".equalsIgnoreCase(s)) {
mont = "02";
} else if ("March".equalsIgnoreCase(s)) {
mont = "03";
} else if ("April".equalsIgnoreCase(s)) {
mont = "04";
} else if ("May".equalsIgnoreCase(s)) {
mont = "05";
} else if ("June".equalsIgnoreCase(s)) {
mont = "06";
} else if ("July".equalsIgnoreCase(s)) {
mont = "07";
} else if ("August".equalsIgnoreCase(s)) {
mont = "08";
} else if ("September".equalsIgnoreCase(s)) {
mont = "09";
} else if ("October".equalsIgnoreCase(s)) {
mont = "10";
} else if ("November".equalsIgnoreCase(s)) {
mont = "11";
} else if ("December".equalsIgnoreCase(s)) {
mont = "12";
}
return mont;
}
private void showBar() {
for (int i = 1; i <= finalday; i++) {
dataset.setValue(i, "Marks", "" + i);
}
JFreeChart chart = ChartFactory.createBarChart(" ", "Student", "Marks",
dataset, PlotOrientation.VERTICAL, false, true, false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(new Color(221, 223, 238));
plot.setRangeGridlinePaint(Color.white);
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setSeriesPaint(0, new Color(231, 175, 61));
renderer.setSeriesPaint(1, Color.green);
renderer.setDrawBarOutline(false);
renderer.setShadowVisible(false);
chart.setBackgroundPaint(Color.WHITE);
chart.getTitle().setPaint(Color.blue);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.BLUE);
frame = new ChartFrame("Bar Chart", chart);
frame.add(month);
frame.add(left);
frame.add(right);
frame.add(yearspin);
frame.add(monthLabel);
frame.add(yearLabel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void updateDataset() {
dataset.clear();
for (int i = 1; i <= finalday; i++) {
dataset.setValue(i, "Marks", "" + i);
}
}
}
I have created a java GUI that works as a 6-question questionnaire. For each of the six question, I have 4 choices , i, ii, iii, iv for the user to choose, and then type out one of the four choices in a text box, then click a button "enter" to go to the next question. After all six questions have been answered, it will jump to a finish page.
I want to write the input value (in the text box) of each question to a text file. So After clicking on the "enter button" to answer all six questions I can see something like "i ii iii i ii iii" in a text file.
Is there a way to do it?
Here is my code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class HTML 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;
private Label qone1, qone2, qone3, qone4, qone5, qone6;
private Label qtwo1, qtwo2, qtwo3, qtwo4, qtwo5, qtwo6;
private Label qthree1, qthree2, qthree3, qthree4, qthree5, qthree6;
private Label qfour1, qfour2, qfour3, qfour4, qfour5, qfour6;
private Label qfive1, qfive2, qfive3, qfive4, qfive5, qfive6;
private Label qsix1, qsix2, qsix3, qsix4, qsix5, qsix6;
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. In order to show the most appropriate advertisement, we would like to know more about your personal preferences.");
add(intro1);
add(intro2);
intro1.setBounds(0,0,800,20);
intro2.setBounds(15,20,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("On average, How many hours do you spend on playing sports every week?");
qone3 = new Label("i.0-2");
qone4 = new Label("ii.3-6");
qone5 = new Label("iii.7-10");
qone6 = new Label("iv.10+");
add(qone1);
add(qone2);
add(qone3);
add(qone4);
add(qone5);
add(qone6);
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);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,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("On average, How many hours do you spend on qsixening to music every week?");
qtwo3 = new Label("i. 0-4 ");
qtwo4 = new Label("ii. 5-10");
qtwo5 = new Label("iii. 11-20");
qtwo6 = new Label("iv. 20+");
add(qtwo1);
add(qtwo2);
add(qtwo3);
add(qtwo4);
add(qtwo5);
add(qtwo6);
qtwo1.setBounds(15,20,800,15);
qtwo2.setBounds(15,40,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);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,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("On average, How many hours do you spend on using computers every week?");
qthree3 = new Label("i.0-2");
qthree4 = new Label("ii.3-10");
qthree5 = new Label("iii.11-15");
qthree6 = new Label("iv.20+");
add(qthree1);
add(qthree2);
add(qthree3);
add(qthree4);
add(qthree5);
add(qthree6);
qthree1.setBounds(15,20,800,20);
qthree2.setBounds(15,40,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);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,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("On average, How many hours do you spend on groceries every week?");
qfour3 = new Label("i.0-2");
qfour4 = new Label("ii.3-10");
qfour5 = new Label("iii.11-15");
qfour6 = new Label("iv.20+");
add(qfour1);
add(qfour2);
add(qfour3);
add(qfour4);
add(qfour5);
add(qfour6);
qfour1.setBounds(15,20,800,15);
qfour2.setBounds(15,40,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);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,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("On average, How many hours do you spend on watching TV every week?");
qfive3 = new Label("i.0-2");
qfive4 = new Label("ii.3-10");
qfive5 = new Label("iii.11-15");
qfive6 = new Label("iv.20+");
add(qfive1);
add(qfive2);
add(qfive3);
add(qfive4);
add(qfive5);
add(qfive6);
qfive1.setBounds(15,20,800,15);
qfive2.setBounds(15,40,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);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 6)
{
qsix1 = new Label("Question 6", Label.LEFT);
qsix1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qsix2 = new Label("On average, How many times do you spend with family every week?");
qsix3 = new Label("i.0-2");
qsix4 = new Label("ii.3-10");
qsix5 = new Label("iii.11-15");
qsix6 = new Label("iv.20+");
add(qsix1);
add(qsix2);
add(qsix3);
add(qsix4);
add(qsix5);
add(qsix6);
qsix1.setBounds(15,20,800,15);
qsix2.setBounds(15,40,800,15);
qsix3.setBounds(15,60,800,15);
qsix4.setBounds(15,80,800,15);
qsix5.setBounds(15,100,800,15);
qsix6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
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);
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);
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);
}
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);
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);
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);
if(input.equals("i"))
{
b = 1;
count = 2;
init();
}
else
{
b = 1;
count = 1;
init();
}
}
if (count == 0)
{
remove(intro1);
remove(intro2);
remove(start);
count = 1;
init();
}
this.validate();
}
}
Sure, something like
OutputStream out = new FileOutputStream("/path/to/your/file");
out.write(answer.getBytes());
out.close();