I face a problem with getting data from CandleStickChart and adding this into LineChart.
Ok, so look at this method. I got data from CandleStick and configure data to LineChart. Everything work perfectly. I have generated 2 chars like you guys see on Image:
But the problem is when i trying to add dynamicData to chars. My method only add data to CandleStickChart.
Here is method to setData on LineChart, here CandleEntry lastEntry = set1.getEntryForIndex(i); i know what value is on bars.
private void setData() {
int prog = 50;
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(i);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(i, lastOpenCloseMax));
}
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.TRANSPARENT);
xAxis.setDrawGridLines(false);
YAxis leftAxis = lineChart.getAxisLeft();
leftAxis.setLabelCount(12, false);
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMaximum(1.6f);
leftAxis.setAxisMinimum(0.4f);
leftAxis.setDrawAxisLine(true);
leftAxis.setTextColor(Color.TRANSPARENT);
YAxis rightAxis = lineChart.getAxisRight();
rightAxis.setEnabled(false);
Collections.sort(entries, new EntryXComparator());
lineDataSet = new LineDataSet(entries, "# of Calls");
lineData = new LineData(lineDataSet);
lineDataSet.setColor(Color.GRAY);
lineDataSet.setDrawCircles(false);
// dataset.setDrawFilled(true);
lineData.setValueTextColor(Color.TRANSPARENT);
lineChart.getLegend().setEnabled(false);
lineChart.getDescription().setEnabled(false);
lineChart.setBackgroundColor(Color.TRANSPARENT);
lineChart.setData(lineData);
lineChart.animateX(4000);
}
Here i addLineEntry, but this not work and i dont know why, do you guys have any idea?
private void addLineEntry() {
lineData = lineChart.getData();
if (lineDataSet == null) {
lineDataSet = createLineSet();
lineData.addDataSet(lineDataSet);
}
int prog = 1;
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(yVals1.size() - 1);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(lineDataSet.getXMax() +1, lastOpenCloseMax));
}
lineDataSet.notifyDataSetChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
mChart.moveViewTo(mChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
And the last is similar method from CandleStickChart to addEntry.
private void addEntry(boolean start) {
data = mChart.getData();
if (set1 == null) {
set1 = createSet();
data.addDataSet(set1);
}
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
int prog = 1;
int xMax = (int) set1.getXMax();
CandleEntry lastEntry = set1.getEntryForIndex(yVals1.size() - 1);
for (int i = 0; i < prog; i++) {
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
float currentOpenCloseMax = Math.max(open, close);
float currentOpenCloseMin = Math.min(open, close);
float high = open + 0.3f;
float low = close - 0.3f;
if (currentOpenCloseMax < lastOpenCloseMax) {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMax, currentOpenCloseMin));
} else {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMin, currentOpenCloseMax));
}
mChart.notifyDataSetChanged();
mChart.invalidate();
mChart.moveViewTo(mChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
}
And here is method to remove Entry from CandleStickChart and LineChart, and this work good.
private void removeLastEntry() {
CandleData data = mChart.getData();
if (data != null) {
ICandleDataSet set = data.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
LineData lineData = lineChart.getData();
if (lineData != null) {
ILineDataSet set = lineData.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
}
}
Do you guys have any idea whats wrong?
I started a bounty:
Here isfull class:
public class MainGameFragment extends Fragment {
#BindView(R.id.spinner_money)
Spinner spinnerData;
#BindView(R.id.text_profit_ill)
TextView text_profit;
#BindView(R.id.button_cash)
Button btnCashCurrency;
#BindView(R.id.restart_game)
Button restartGame;
#BindView(R.id.butonCurrency)
Button buttonCurrency;
#BindView(R.id.chart)
CandleStickChart mChart;
#BindView(R.id.progress_bar)
ProgressBar progress;
#BindView(R.id.btn_buy)
Button btnBuy;
#BindView(R.id.btn_sell)
Button btnSell;
#BindView(R.id.drawer_settings)
ImageButton openDrawerSettings;
#BindView(R.id.chartLine)
LineChart lineChart;
public static ArrayList<String> HISTORYTRANSACTION = new ArrayList<>();
public static ArrayList<String> LEADERBOARDUSER = new ArrayList<>();
public static String userNameAndScore;
private Handler handler;
private Handler handlerLast;
private String buttonPosition;
int pos = 0;
LostDialogFragment lostFragment = LostDialogFragment.newInstance(1);
WinDialogFragment winFragment = WinDialogFragment.newInstance(1);
SettingsFragment settingsFragment = SettingsFragment.newInstance(1);
String DIALOG_WIN = "WinDialogFragment";
String DIALOG_LOST = "LostDialogFragment";
String DIALOG_SETTINGS = "settingsFragment";
private CandleData data;
private LineData lineData;
private LineDataSet lineDataSet;
private CandleDataSet set1;
private Drawer result;
public static StorageReference storageReference;
private Runnable r;
private Runnable rLast;
ArrayList<CandleEntry> yVals1 = new ArrayList<>();
ArrayList<Entry> entries = new ArrayList<>();
public MainGameFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
buttonPosition = getArguments().getString("button_position", "value");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_game, container, false);
ButterKnife.bind(this, view);
setText();
configureSpinnerDataAndLogic();
configureChart();
configureColorProgresBar();
openDrawer();
configureDrawer();
welcomeMessage();
configureHandler(5000);
storageReference = FirebaseStorage.getInstance().getReference();
setData();
return view;
}
private void setData() {
int prog = 50;
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(i);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(i, lastOpenCloseMax));
}
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.TRANSPARENT);
xAxis.setDrawGridLines(false);
YAxis leftAxis = lineChart.getAxisLeft();
leftAxis.setLabelCount(12, false);
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMaximum(1.6f);
leftAxis.setAxisMinimum(0.4f);
leftAxis.setDrawAxisLine(true);
leftAxis.setTextColor(Color.TRANSPARENT);
YAxis rightAxis = lineChart.getAxisRight();
rightAxis.setEnabled(false);
Collections.sort(entries, new EntryXComparator());
lineDataSet = new LineDataSet(entries, "# of Calls");
lineData = new LineData(lineDataSet);
lineDataSet.setColor(Color.GRAY);
lineDataSet.setDrawCircles(false);
// dataset.setDrawFilled(true);
lineData.setValueTextColor(Color.TRANSPARENT);
lineChart.getLegend().setEnabled(false);
lineChart.getDescription().setEnabled(false);
lineChart.setBackgroundColor(Color.TRANSPARENT);
lineChart.setData(lineData);
lineChart.animateX(4000);
}
private void welcomeMessage() {
Toast.makeText(getContext(), "Welcome " + MainActivity.getUsername(getContext()).trim() + "!"
+ " Getting data from last hour..",
Toast.LENGTH_LONG).show();
}
private void configureDrawer() {
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(getActivity())
.withProfileImagesClickable(false)
.withHeaderBackground(R.drawable.logo_white)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
#Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
return false;
}
})
.build();
result = new DrawerBuilder()
.withSliderBackgroundColor(Color.GRAY)
.withAccountHeader(headerResult)
.withActivity(getActivity())
.withDisplayBelowStatusBar(false)
.withDrawerGravity(Gravity.LEFT)
.withHeaderPadding(true)
.addDrawerItems(
new SectionDrawerItem().withName("Options"),
new PrimaryDrawerItem().withName("Trading History").withIcon(R.drawable.trading_history).withIdentifier(2),
new PrimaryDrawerItem().withName("Leader Board").withIcon(R.drawable.leade_board).withIdentifier(3),
new PrimaryDrawerItem().withName("Special offer").withIcon(R.drawable.special_icon).withIdentifier(4),
new PrimaryDrawerItem().withName("Video tutorials").withIcon(R.drawable.video_tutorials).withIdentifier(5),
new PrimaryDrawerItem().withName("FAQ").withIcon(R.drawable.faq_icon).withIdentifier(6),
new PrimaryDrawerItem().withName("CONTACT").withIcon(R.drawable.contact_icon).withIdentifier(7)
)
.buildForFragment();
result.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.addToBackStack(null);
SettingsFragment.POSITION = position;
result.closeDrawer();
if (settingsFragment != null) {
settingsFragment.show(ft, DIALOG_SETTINGS);
}
return true;
}
});
result.getDrawerLayout().setFitsSystemWindows(false);
result.getSlider().setFitsSystemWindows(false);
}
private CandleDataSet createSet() {
set1 = new CandleDataSet(null, "DataSet 1");
set1.setColor(Color.rgb(240, 99, 99));
set1.setHighLightColor(Color.rgb(190, 190, 190));
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setValueTextSize(10f);
return set1;
}
private LineDataSet createLineSet() {
lineDataSet = new LineDataSet(null, "DataSet 1");
lineDataSet.setColor(Color.rgb(240, 99, 99));
lineDataSet.setHighLightColor(Color.rgb(190, 190, 190));
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet.setValueTextSize(10f);
return lineDataSet;
}
public void openDrawer() {
openDrawerSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.openDrawer();
}
});
}
private void addLineEntry() {
lineData = lineChart.getData();
if (lineDataSet == null) {
lineDataSet = createLineSet();
lineData.addDataSet(lineDataSet);
}
int prog = 1;
int xMax = (int) set1.getXMax();
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(xMax -1);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(lineData.getDataSetCount() +1, lastOpenCloseMax));
}
lineDataSet.notifyDataSetChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
lineChart.moveViewTo(lineChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
private void addEntry(boolean start) {
data = mChart.getData();
if (set1 == null) {
set1 = createSet();
data.addDataSet(set1);
}
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
int prog = 1;
int xMax = (int) set1.getXMax();
CandleEntry lastEntry = set1.getEntryForIndex(yVals1.size() - 1);
for (int i = 0; i < prog; i++) {
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
float currentOpenCloseMax = Math.max(open, close);
float currentOpenCloseMin = Math.min(open, close);
float high = open + 0.3f;
float low = close - 0.3f;
if (currentOpenCloseMax < lastOpenCloseMax) {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMax, currentOpenCloseMin));
} else {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMin, currentOpenCloseMax));
}
mChart.notifyDataSetChanged();
mChart.invalidate();
mChart.moveViewTo(mChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
}
private void removeLastEntry() {
CandleData data = mChart.getData();
if (data != null) {
ICandleDataSet set = data.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
LineData lineData = lineChart.getData();
if (lineData != null) {
ILineDataSet set = lineData.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
}
}
}
public String getUserInfoAndSave() {
userNameAndScore = MainActivity.getUsername(getContext()).trim() + ": "
+ btnCashCurrency.getText().toString().trim();
return userNameAndScore;
}
private void configureHandlerWithoutRemoveLastEntry(final int time) {
handlerLast = new Handler();
rLast = new Runnable() {
public void run() {
// removeLastEntry();
addEntry(true);
handler.postDelayed(this, time);
}
};
handlerLast.postDelayed(rLast, time);
}
private void configureHandler(final int time) {
handler = new Handler();
r = new Runnable() {
public void run() {
removeLastEntry();
addEntry(true);
addLineEntry();
handler.postDelayed(this, time);
}
};
handler.postDelayed(r, time);
}
public void stopLast() {
handlerLast.removeCallbacks(rLast);
}
public void stop() {
handler.removeCallbacks(r);
}
private void configureChart() {
mChart.getDescription().setEnabled(false);
mChart.getLegend().setTextColor(Color.WHITE);
mChart.setMaxVisibleValueCount(50);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.WHITE);
xAxis.setDrawGridLines(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setLabelCount(12, false);
leftAxis.setDrawGridLines(false);
leftAxis.setDrawAxisLine(true);
leftAxis.setTextColor(Color.WHITE);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setEnabled(false);
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
int prog = 50;
float last = Float.NEGATIVE_INFINITY;
String date = String.valueOf(android.text.format.DateFormat.format("yyyy-MM-dd", new java.util.Date()));
for (int i = 0; i < prog; i++) {
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float max = Math.max(open, close);
if (last < max) {
float tmp = open;
open = close;
close = tmp;
}
last = max;
float high = open + 0.3f;
float low = close - 0.3f;
yVals1.add(new CandleEntry(i, high, low, open, close));
}
set1 = new CandleDataSet(yVals1, date);
data = new CandleData(set1);
mChart.setData(data);
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(Color.rgb(80, 80, 80));
set1.setIncreasingColor(Color.GREEN);
set1.setIncreasingPaintStyle(Paint.Style.FILL);
set1.setDecreasingColor(Color.RED);
set1.setDecreasingPaintStyle(Paint.Style.FILL);
set1.setNeutralColor(Color.BLUE);
set1.setBarSpace(0.2f);
set1.setValueTextColor(Color.TRANSPARENT);
mChart.notifyDataSetChanged();
mChart.animateX(4000);
}
private void setText() {
buttonCurrency.setText("Assets: \n" + buttonPosition);
}
private void configureColorProgresBar() {
progress.getIndeterminateDrawable().setColorFilter(
getResources().getColor(R.color.white),
android.graphics.PorterDuff.Mode.SRC_IN);
}
#OnClick(R.id.invest_text)
public void invest() {
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float high = open + 0.3f;
float low = close - 0.3f;
if (pos == 0) {
yVals1.add(new CandleEntry(50, high, low, open, close));
pos++;
} else if (pos == 1) {
yVals1.add(new CandleEntry(51, high, low, open, close));
pos++;
} else if (pos == 2) {
yVals1.add(new CandleEntry(52, high, low, open, close));
pos++;
} else if (pos == 3) {
yVals1.add(new CandleEntry(53, high, low, open, close));
pos++;
} else if (pos == 4) {
yVals1.add(new CandleEntry(54, high, low, open, close));
pos++;
}
mChart.invalidate();
}
#OnClick({R.id.btn_buy, R.id.btn_sell})
public void onGameButtonsClicked() {
final FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.addToBackStack(null);
String cashText = btnCashCurrency.getText().toString();
final int[] cash = {Integer.valueOf(cashText)};
if (cash[0] <= 0) {
restartGame.setVisibility(View.VISIBLE);
Toast.makeText(getContext(), "Your cash is on -, u cant play. Please restart game.", Toast.LENGTH_SHORT).show();
} else if (Integer.valueOf(spinnerData.getSelectedItem().toString()) >= Integer.valueOf(btnCashCurrency.getText().toString())) {
Toast.makeText(getContext(), "You not have available cash, change the money in Invest Section.", Toast.LENGTH_SHORT).show();
} else {
int min = 0;
int max = 2;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
String text = spinnerData.getSelectedItem().toString();
final int temp = Integer.parseInt(text);
final int temp2 = temp * 2;
disableAndEnableButtons(false);
if (i1 == 0 || i1 == 1) {
progress.setVisibility(View.VISIBLE);
stop();
configureHandler(900);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
lostFragment.show(ft, DIALOG_LOST);
cash[0] -= temp2;
btnCashCurrency.setText(cash[0] + "");
progress.setVisibility(View.GONE);
disableAndEnableButtons(true);
String score = "LOSE " + MainActivity.getUsername(getContext()).trim() + ": "
+ btnCashCurrency.getText().toString().trim() + " -" + temp2;
HISTORYTRANSACTION.add(score);
getUserInfoAndSave();
stop();
configureHandler(5000);
}
}, 5000);
} else {
progress.setVisibility(View.VISIBLE);
stop();
configureHandler(900);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
winFragment.show(ft, DIALOG_WIN);
cash[0] += temp2;
btnCashCurrency.setText(cash[0] + "");
progress.setVisibility(View.GONE);
disableAndEnableButtons(true);
String score = "WIN " + MainActivity.getUsername(getContext()).trim()
+ ": " + btnCashCurrency.getText().toString().trim() + " +" + temp2;
HISTORYTRANSACTION.add(score);
getUserInfoAndSave();
stop();
configureHandler(5000);
}
}, 5000);
}
}
}
private void disableAndEnableButtons(boolean on) {
btnBuy.setEnabled(on);
btnSell.setEnabled(on);
}
#OnClick(R.id.restart_game)
public void restartGame() {
Intent intent = new Intent(getContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
MainGameFragment.LEADERBOARDUSER.add(MainGameFragment.userNameAndScore);
((CurrencySelectActivity) getContext()).hideGameFragment();
((CurrencySelectActivity) getContext()).closeCurrencyActivity();
SharedPreferences prefs = getContext().getSharedPreferences("app.forex", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
Set<String> set = new HashSet<>();
set.add(String.valueOf(MainGameFragment.LEADERBOARDUSER));
edit.putStringSet("user_and_score", set);
HISTORYTRANSACTION.clear();
edit.apply();
}
private void configureSpinnerDataAndLogic() {
String[] arraySpinner = new String[]{
"50", "100", "150", "200", "250", "300", "400", "500"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_list_item_1, arraySpinner);
spinnerData.setAdapter(adapter);
spinnerData.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);
String text = spinnerData.getSelectedItem().toString();
int temp = Integer.parseInt(text);
text_profit.setText((temp * 2) + " $ " + "100%");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
AAnd here is xml files
<com.github.mikephil.charting.charts.CandleStickChart
android:id="#+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/butonCurrency"
android:layout_margin="30dp" />
<com.github.mikephil.charting.charts.LineChart
android:id="#+id/chartLine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/butonCurrency"
android:layout_margin="30dp" />
And thiswork like:
Highly help needed
Instead of adding the new entry to your entries array.. you could try using the method lineData.addEntry().
Something like this :
private void addEntry() {
LineData data = mChart.getData();
ILineDataSet set = data.getDataSetByIndex(0);
if (set == null) {
set = createSet();
data.addDataSet(set);
}
data.addEntry(new Entry(data.getDataSetByIndex(dataSetIndex).getEntryCount(), yValue), dataSetIndex);
data.notifyDataChanged();
mChart.notifyDataSetChanged();
}
your "dataSetIndex" will always be 0 if you only have a single dataset in your linechart.
For more information, you can check this class
Problem is here, after i change, everything work good
private void addLineEntry() {
lineData = lineChart.getData();
if (lineDataSet == null) {
lineDataSet = createLineSet();
lineData.addDataSet(lineDataSet);
}
int prog = 1;
int xMax = (int) lineDataSet.getXMax();
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(yVals1.size() - 1);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(xMax + 1, lastOpenCloseMax));
}
lineDataSet.notifyDataSetChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
lineChart.moveViewTo(xMax, 2f, YAxis.AxisDependency.RIGHT);
}
Related
I'm simulating dummy real-time data using DynamicTimeSeriesCollection, like this. During random intervals, the data being passed to the plot should 'dropout' to simulate a network connection loss. At this point, this plot should stop drawing and only start plotting the data after the dropout has subsided.
I subclassed XYLineAndShapeRenderer and overrode the getItemLineVisible() method:
#Override
public boolean getItemLineVisible(int series, int item){
if(offline){
return false;
}else{
return true;
}
}
However when offline is true, all points are still being drawn on the graph.
public class Test extends ApplicationFrame {
private static final String TITLE = "Dynamic Series";
private static final String START = "Start";
private static final String STOP = "Stop";
private static final int COUNT = 1000*60;
private static final int FAST = 1; //1000/FAST = occurrences per second real time
private static final int REALTIME = FAST * 1000;
private static final Random random = new Random();
private static final double threshold = 35;
private double gateStart = ThreadLocalRandom.current().nextInt(0, 101);
private boolean returning = false;
private boolean offline = false;
private Timer timer;
private Calendar startDate;
private static final int simulationSpeed = 1000/FAST;
private final TimeSeries seriesA = new TimeSeries("A");
public Test(final String title) throws ParseException {
super(title);
SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy HH:mm", Locale.ENGLISH);
PriceParser parser = new PriceParser();
List<List<String>> priceData = parser.parse();
Date date = formatter.parse(priceData.get(0).get(0));
startDate = Calendar.getInstance();
startDate.setTime(date);
Calendar timeBaseStartDate = Calendar.getInstance();
timeBaseStartDate.setTime(startDate.getTime());
timeBaseStartDate.add(Calendar.SECOND, -COUNT);
final TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(this.seriesA);
JFreeChart chart = createChart(dataset);
final JComboBox combo = new JComboBox();
combo.addItem("Fast");
combo.addItem("Real-time");
combo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if ("Fast".equals(combo.getSelectedItem())) {
timer.setDelay(FAST);
} else {
timer.setDelay(REALTIME);
}
}
});
JFrame frame = new JFrame("Test");
JLabel label = new JLabel("Network connectivity lost.");
this.add(new ChartPanel(chart), BorderLayout.CENTER);
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(combo);
JPanel test = new JPanel();
test.add(label);
this.add(btnPanel, BorderLayout.SOUTH);
// frame.add(btnPanel);
//frame.add(test);
timer = new Timer(FAST, new ActionListener() {
Date timeToCheck = formatter.parse(priceData.get(0).get(0));
Calendar pauseResume = Calendar.getInstance();
Calendar offlineTime = Calendar.getInstance();
boolean paused = false;
boolean waiting = false;
//boolean offline = false;
double currentPrice;
float[] newData = new float[1];
PopupFactory pf = PopupFactory.getSharedInstance();
Popup popup;
#Override
public void actionPerformed(ActionEvent e) {
Date datasetTime = new Date();
if(offline){
System.out.println("Offline: "+offlineTime.getTime());
System.out.println("Current: "+datasetTime);
if(offlineTime.getTime().compareTo(datasetTime) == 0){
offline = false;
System.out.println("Im no longer offline");
popup.hide();
}
}
if(ThreadLocalRandom.current().nextInt(0, 1001) > 999 && !offline){
offline = true;
offlineTime.setTime(datasetTime);
offlineTime.add(Calendar.SECOND, ThreadLocalRandom.current().nextInt(1, 5)*10);
// dataset.addValue(0, 0, null);
popup = pf.getPopup(btnPanel, label, 900, 300);
popup.show();
}
if(timeToCheck.compareTo(datasetTime) == 0){
currentPrice = Double.valueOf(priceData.get(0).get(1));
paused = currentPrice >= threshold;
priceData.remove(0);
try {
timeToCheck = formatter.parse(priceData.get(0).get(0));
} catch (ParseException ex) {
ex.printStackTrace();
}
}
if(!paused) {
if (Math.round(gateStart) * 10 / 10.0 == 100d) {
returning = true;
} else if (Math.round(gateStart) * 10 / 10.0 == 0) {
returning = false;
}
if (returning) {
gateStart -= 0.1d;
} else {
gateStart += 0.1d;
}
}else{
if(datasetTime.compareTo(pauseResume.getTime()) == 0 && currentPrice < threshold){
paused = false;
waiting = false;
}else{
if(Math.round(gateStart)*10/10.0 == 0 || Math.round(gateStart)*10/10.0 == 100){
if(!waiting){
pauseResume.setTime(datasetTime);
pauseResume.add(Calendar.SECOND, 120);
}
waiting = true;
}else{
if(Math.round(gateStart)*10/10.0 >= 50){
gateStart += 0.1d;
}else if(Math.round(gateStart)*10/10.0 < 50){
gateStart -= 0.1d;
}
}
}
}
newData[0] = (float)gateStart;
seriesA.addOrUpdate(new Second(), gateStart);
}
});
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
TITLE, "Time", "Shearer Position", dataset, true, true, false);
final XYPlot plot = result.getXYPlot();
plot.setDomainZeroBaselineVisible(false);
XYLineAndShapeRendererTest renderer = new XYLineAndShapeRendererTest(true, false);
plot.setRenderer(renderer);
DateAxis domain = (DateAxis)plot.getDomainAxis();
Calendar endDate = Calendar.getInstance();
endDate.setTime(new Date());
endDate.add(Calendar.HOUR_OF_DAY, 12);
System.out.println(new Date());
System.out.println(endDate.getTime());
domain.setRange(new Date(), endDate.getTime());
domain.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 1));
domain.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
ValueAxis range = plot.getRangeAxis();
range.setRange(0, 100);
return result;
}
private class XYLineAndShapeRendererTest extends XYLineAndShapeRenderer {
private boolean drawSeriesLineAsPath;
public XYLineAndShapeRendererTest(boolean line, boolean shapes){
super(line, shapes);
}
#Override
public Paint getItemPaint(int row, int col) {
if(!offline){
return super.getItemPaint(row, col);
}else{
return new Color(0, 0, 0);
}
}
}
private void start() {
timer.start();
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
Test demo = null; //pass date in from csv
try {
demo = new Test(TITLE);
} catch (ParseException e) {
e.printStackTrace();
}
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
demo.start();
}
});
}
}
What am I doing wrong?
One approach would be to append an appropriate baseline value when off line. Starting from this example, the range is centered on a value of zero millivolts. The modification below adds zeroes between 60 and 90 milliseconds:
private float[] gaussianData() {
float[] a = new float[COUNT];
for (int i = 0; i < a.length; i++) {
if (i > 60 && i < 90) a[i] = 0;
else a[i] = randomValue();
}
return a;
}
In my instance, a period of offline should effectively stop graphing.
Use the approach suggested here, which uses setMaximumItemAge() to limit the number of displayed records. Add null values, as suggested here, to interrupt the display. Starting from this example, I got this display with these changes:
seriesA.setMaximumItemCount(120);
seriesB.setMaximumItemCount(120);
…
int i;
…
public void addNull() {
this.seriesA.add(new Millisecond(), null);
this.seriesB.add(new Millisecond(), null);
}
#Override
public void actionPerformed(ActionEvent e) {
if (i > 60 && i < 90) {
demo.addNull();
} else {
…
}
i++;
}
I have JFrame with a start button, which triggers the calculation of a Julia Set.
The code that is executed when the start button is clicked is as follows:
public void actionPerformed(ActionEvent aActionEvent)
{
String strCmd = aActionEvent.getActionCommand();
if (strCmd.equals("Start"))
{
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
this.handleCalculation();
}
else if (aActionEvent.getSource() == m_cTReal)
Which used to work fine, except that the application could not be closed anymore. So I tried to use m_bRunning in a separate method so that actionPerformed() isn't blocked all the time to see if that would help, and then set m_bRunning = false in the method stop() which is called when the window is closed:
public void run()
{
if(m_bRunning)
{
this.handleCalculation();
}
}
The method run() is called from the main class in a while(true) loop.
Yet unfortunately, neither did that solve the problem, nor do I now have any output to the canvas or any debug traces with System.out.println(). Could anyone point me in the right direction on this?
EDIT:
Here are the whole files:
// cMain.java
package juliaSet;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
public class cMain {
public static void main(String[] args)
{
int windowWidth = 1000;//(int)screenSize.getWidth() - 200;
int windowHeight = 800;//(int)screenSize.getHeight() - 50;
int plotWidth = 400;//(int)screenSize.getWidth() - 600;
int plotHeight = 400;//(int)screenSize.getHeight() - 150;
JuliaSet cJuliaSet = new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
cJuliaSet.setVisible(true);
while(true)
{
cJuliaSet.run();
}
}
}
// JuliaSet.java
package juliaSet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Random;
import java.io.*;
import java.lang.ref.*;
public class JuliaSet extends JFrame implements ActionListener
{
private JButton m_cBStart;
private JTextField m_cTReal;
private JTextField m_cTImag;
private JTextField m_cTDivergThresh;
private JLabel m_cLReal;
private JLabel m_cLImag;
private JLabel m_cLDivergThresh;
private int m_iDivergThresh = 10;
private String m_cMsgDivThresh = "Divergence threshold = " + m_iDivergThresh;
private JuliaCanvas m_cCanvas;
private int m_iPlotWidth; // number of cells
private int m_iPlotHeight; // number of cells
private Boolean m_bRunning = false;
private double m_dReal = 0.3;
private double m_dImag = -0.5;
private String m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
private String m_cMsgIter = "x = 0, y = 0";
private Complex m_cCoordPlane[][];
private double m_dAbsSqValues[][];
private int m_iIterations[][];
private Complex m_cSummand;
private BufferedImage m_cBackGroundImage = null;
private FileWriter m_cFileWriter;
private BufferedWriter m_cBufferedWriter;
private String m_sFileName = "log.txt";
private Boolean m_bWriteLog = false;
private static final double PLOTMAX = 2.0; // we'll have symmetric axes
// ((0,0) at the centre of the
// plot
private static final int MAXITER = 0xff;
JuliaSet(String aTitle, int aFrameWidth, int aFrameHeight, int aPlotWidth, int aPlotHeight)
{
super(aTitle);
this.setSize(aFrameWidth, aFrameHeight);
m_iPlotWidth = aPlotWidth;
m_iPlotHeight = aPlotHeight;
m_cSummand = new Complex(m_dReal, m_dImag);
m_cBackGroundImage = new BufferedImage(aFrameWidth, aFrameHeight, BufferedImage.TYPE_INT_RGB);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
stop();
super.windowClosing(e);
System.exit(0);
}
});
GridBagLayout cLayout = new GridBagLayout();
GridBagConstraints cConstraints = new GridBagConstraints();
this.setLayout(cLayout);
m_cCanvas = new JuliaCanvas(m_iPlotWidth, m_iPlotHeight);
m_cCanvas.setSize(m_iPlotWidth, m_iPlotHeight);
m_cBStart = new JButton("Start");
m_cBStart.addActionListener(this);
m_cTReal = new JTextField(5);
m_cTReal.addActionListener(this);
m_cTImag = new JTextField(5);
m_cTImag.addActionListener(this);
m_cTDivergThresh = new JTextField(5);
m_cTDivergThresh.addActionListener(this);
m_cLReal = new JLabel("Re(c):");
m_cLImag = new JLabel("Im(c):");
m_cLDivergThresh = new JLabel("Divergence Threshold:");
cConstraints.insets.top = 3;
cConstraints.insets.bottom = 3;
cConstraints.insets.right = 3;
cConstraints.insets.left = 3;
// cCanvas
cConstraints.gridx = 0;
cConstraints.gridy = 0;
cLayout.setConstraints(m_cCanvas, cConstraints);
this.add(m_cCanvas);
// m_cLReal
cConstraints.gridx = 0;
cConstraints.gridy = 1;
cLayout.setConstraints(m_cLReal, cConstraints);
this.add(m_cLReal);
// m_cTReal
cConstraints.gridx = 1;
cConstraints.gridy = 1;
cLayout.setConstraints(m_cTReal, cConstraints);
this.add(m_cTReal);
// m_cLImag
cConstraints.gridx = 0;
cConstraints.gridy = 2;
cLayout.setConstraints(m_cLImag, cConstraints);
this.add(m_cLImag);
// m_cTImag
cConstraints.gridx = 1;
cConstraints.gridy = 2;
cLayout.setConstraints(m_cTImag, cConstraints);
this.add(m_cTImag);
// m_cLDivergThresh
cConstraints.gridx = 0;
cConstraints.gridy = 3;
cLayout.setConstraints(m_cLDivergThresh, cConstraints);
this.add(m_cLDivergThresh);
// m_cTDivergThresh
cConstraints.gridx = 1;
cConstraints.gridy = 3;
cLayout.setConstraints(m_cTDivergThresh, cConstraints);
this.add(m_cTDivergThresh);
// m_cBStart
cConstraints.gridx = 0;
cConstraints.gridy = 4;
cLayout.setConstraints(m_cBStart, cConstraints);
this.add(m_cBStart);
if (m_bWriteLog)
{
try
{
m_cFileWriter = new FileWriter(m_sFileName, false);
m_cBufferedWriter = new BufferedWriter(m_cFileWriter);
} catch (IOException ex) {
System.out.println("Error opening file '" + m_sFileName + "'");
}
}
this.repaint();
this.transformCoordinates();
}
public synchronized void stop()
{
if (m_bRunning)
{
m_bRunning = false;
boolean bRetry = true;
}
if (m_bWriteLog)
{
try {
m_cBufferedWriter.close();
m_cFileWriter.close();
} catch (IOException ex) {
System.out.println("Error closing file '" + m_sFileName + "'");
}
}
}
public void collectGarbage()
{
Object cObj = new Object();
WeakReference ref = new WeakReference<Object>(cObj);
cObj = null;
while(ref.get() != null) {
System.gc();
}
}
public void setSummand(Complex aSummand)
{
m_cSummand.setIm(aSummand.getIm());
m_dImag = aSummand.getIm();
m_cSummand.setRe(aSummand.getRe());
m_dReal = aSummand.getRe();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
}
public void paint(Graphics aGraphics)
{
Graphics cScreenGraphics = aGraphics;
// render on background image
aGraphics = m_cBackGroundImage.getGraphics();
this.paintComponents(aGraphics);
// drawString() calls are debug code only....
aGraphics.setColor(Color.BLACK);
aGraphics.drawString(m_cSMsg, 10, 450);
aGraphics.drawString(m_cMsgIter, 10, 465);
aGraphics.drawString(m_cMsgDivThresh, 10, 480);
// rendering is done, draw background image to on screen graphics
cScreenGraphics.drawImage(m_cBackGroundImage, 0, 0, null);
}
public void actionPerformed(ActionEvent aActionEvent)
{
String strCmd = aActionEvent.getActionCommand();
if (strCmd.equals("Start"))
{
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
}
else if (aActionEvent.getSource() == m_cTReal)
{
m_dReal = Double.parseDouble(m_cTReal.getText());
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_cSummand.setRe(m_dReal);
}
else if (aActionEvent.getSource() == m_cTImag)
{
m_dImag = Double.parseDouble(m_cTImag.getText());
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_cSummand.setIm(m_dImag);
}
else if (aActionEvent.getSource() == m_cTDivergThresh)
{
m_iDivergThresh = Integer.parseInt(m_cTDivergThresh.getText());
m_cMsgDivThresh = "Divergence threshold = " + m_iDivergThresh;
}
this.update(this.getGraphics());
}
public void transformCoordinates()
{
double dCanvasHeight = (double) m_cCanvas.getHeight();
double dCanvasWidth = (double) m_cCanvas.getWidth();
// init matrix with same amount of elements as pixels in canvas
m_cCoordPlane = new Complex[(int) dCanvasHeight][(int) dCanvasWidth];
double iPlotRange = 2 * PLOTMAX;
for (int i = 0; i < dCanvasHeight; i++)
{
for (int j = 0; j < dCanvasWidth; j++)
{
m_cCoordPlane[i][j] = new Complex((i - (dCanvasWidth / 2)) * iPlotRange / dCanvasWidth,
(j - (dCanvasHeight / 2)) * iPlotRange / dCanvasHeight);
}
}
}
public void calcAbsSqValues()
{
int iCanvasHeight = m_cCanvas.getHeight();
int iCanvasWidth = m_cCanvas.getWidth();
// init matrix with same amount of elements as pixels in canvas
m_dAbsSqValues = new double[iCanvasHeight][iCanvasWidth];
m_iIterations = new int[iCanvasHeight][iCanvasWidth];
Complex cSum = new Complex();
if (m_bWriteLog) {
try
{
m_cBufferedWriter.write("m_iIterations[][] =");
m_cBufferedWriter.newLine();
}
catch (IOException ex)
{
System.out.println("Error opening file '" + m_sFileName + "'");
}
}
for (int i = 0; i < iCanvasHeight; i++)
{
for (int j = 0; j < iCanvasWidth; j++)
{
cSum.setRe(m_cCoordPlane[i][j].getRe());
cSum.setIm(m_cCoordPlane[i][j].getIm());
m_iIterations[i][j] = 0;
do
{
m_iIterations[i][j]++;
cSum.square();
cSum.add(m_cSummand);
m_dAbsSqValues[i][j] = cSum.getAbsSq();
} while ((m_iIterations[i][j] < MAXITER) && (m_dAbsSqValues[i][j] < m_iDivergThresh));
this.calcColour(i, j, m_iIterations[i][j]);
m_cMsgIter = "x = " + i + " , y = " + j;
if(m_bWriteLog)
{
System.out.println(m_cMsgIter);
System.out.flush();
}
if (m_bWriteLog) {
try
{
m_cBufferedWriter.write(Integer.toString(m_iIterations[i][j]));
m_cBufferedWriter.write(" ");
}
catch (IOException ex) {
System.out.println("Error writing to file '" + m_sFileName + "'");
}
}
}
if (m_bWriteLog) {
try
{
m_cBufferedWriter.newLine();
}
catch (IOException ex) {
System.out.println("Error writing to file '" + m_sFileName + "'");
}
}
}
m_dAbsSqValues = null;
m_iIterations = null;
cSum = null;
}
private void calcColour(int i, int j, int aIterations)
{
Color cColour = Color.getHSBColor((int) Math.pow(aIterations, 4), 0xff,
0xff * ((aIterations < MAXITER) ? 1 : 0));
m_cCanvas.setPixelColour(i, j, cColour);
cColour = null;
}
private void handleCalculation()
{
Complex cSummand = new Complex();
for(int i = -800; i <= 800; i++)
{
for(int j = -800; j <= 800; j++)
{
cSummand.setRe(((double)i)/1000.0);
cSummand.setIm(((double)j)/1000.0);
this.setSummand(cSummand);
this.calcAbsSqValues();
this.getCanvas().paint(m_cCanvas.getGraphics());
this.paint(this.getGraphics());
}
}
cSummand = null;
this.collectGarbage();
System.gc();
System.runFinalization();
}
public boolean isRunning()
{
return m_bRunning;
}
public void setRunning(boolean aRunning)
{
m_bRunning = aRunning;
}
public Canvas getCanvas()
{
return m_cCanvas;
}
public void run()
{
if(m_bRunning)
{
this.handleCalculation();
}
}
}
class JuliaCanvas extends Canvas
{
private int m_iWidth;
private int m_iHeight;
private Random m_cRnd;
private BufferedImage m_cBackGroundImage = null;
private int m_iRed[][];
private int m_iGreen[][];
private int m_iBlue[][];
JuliaCanvas(int aWidth, int aHeight)
{
m_iWidth = aWidth;
m_iHeight = aHeight;
m_cRnd = new Random();
m_cRnd.setSeed(m_cRnd.nextLong());
m_cBackGroundImage = new BufferedImage(m_iWidth, m_iHeight, BufferedImage.TYPE_INT_RGB);
m_iRed = new int[m_iHeight][m_iWidth];
m_iGreen = new int[m_iHeight][m_iWidth];
m_iBlue = new int[m_iHeight][m_iWidth];
}
public void init() {
}
public void setPixelColour(int i, int j, Color aColour)
{
m_iRed[i][j] = aColour.getRed();
m_iGreen[i][j] = aColour.getGreen();
m_iBlue[i][j] = aColour.getBlue();
}
private int getRandomInt(double aProbability)
{
return (m_cRnd.nextDouble() < aProbability) ? 1 : 0;
}
#Override
public void paint(Graphics aGraphics)
{
// store on screen graphics
Graphics cScreenGraphics = aGraphics;
// render on background image
aGraphics = m_cBackGroundImage.getGraphics();
for (int i = 0; i < m_iWidth; i++)
{
for (int j = 0; j < m_iHeight; j++)
{
Color cColor = new Color(m_iRed[i][j], m_iGreen[i][j], m_iBlue[i][j]);
aGraphics.setColor(cColor);
aGraphics.drawRect(i, j, 0, 0);
cColor = null;
}
}
// rendering is done, draw background image to on screen graphics
cScreenGraphics.drawImage(m_cBackGroundImage, 1, 1, null);
}
#Override
public void update(Graphics aGraphics)
{
paint(aGraphics);
}
}
class Complex {
private double m_dRe;
private double m_dIm;
public Complex()
{
m_dRe = 0;
m_dIm = 0;
}
public Complex(double aRe, double aIm)
{
m_dRe = aRe;
m_dIm = aIm;
}
public Complex(Complex aComplex)
{
m_dRe = aComplex.m_dRe;
m_dIm = aComplex.m_dIm;
}
public double getRe() {
return m_dRe;
}
public void setRe(double adRe)
{
m_dRe = adRe;
}
public double getIm() {
return m_dIm;
}
public void setIm(double adIm)
{
m_dIm = adIm;
}
public void add(Complex acComplex)
{
m_dRe += acComplex.getRe();
m_dIm += acComplex.getIm();
}
public void square()
{
double m_dReSave = m_dRe;
m_dRe = (m_dRe * m_dRe) - (m_dIm * m_dIm);
m_dIm = 2 * m_dReSave * m_dIm;
}
public double getAbsSq()
{
return ((m_dRe * m_dRe) + (m_dIm * m_dIm));
}
}
I'm quoting a recent comment from #MadProgrammer (including links)
"Swing is single threaded, nothing you can do to change that, all events are posted to the event queue and processed by the Event Dispatching Thread, see Concurrency in Swing for more details and have a look at Worker Threads and SwingWorker for at least one possible solution"
There is only one thread in your code. That thread is busy doing the calculation and can not respond to events located in the GUI. You have to separate the calculation in another thread that periodically updates the quantities that appears in the window. More info about that in the links, courtesy of #MadProgrammer, I insist.
UPDATED: As pointed by #Yusuf, the proper way of launching the JFrame is
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
}
});
Set the frame visible on construction and start calculation when the start button is pressed.
First;
Endless loop is not a proper way to do this. This part is loops and taking CPU and never give canvas to refresh screen. if you add below code your code will run as expected. but this is not the proper solution.
cMain.java:
while (true) {
cJuliaSet.run();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Second: you could call run method when start button clicked. But you should create a thread in run method to not freeze screen.
public static void main(String[] args) {
int windowWidth = 1000;// (int)screenSize.getWidth() - 200;
int windowHeight = 800;// (int)screenSize.getHeight() - 50;
int plotWidth = 400;// (int)screenSize.getWidth() - 600;
int plotHeight = 400;// (int)screenSize.getHeight() - 150;
JuliaSet cJuliaSet = new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
cJuliaSet.setVisible(true);
//While loop removed
}
actionPerformed:
if (strCmd.equals("Start")) {
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
this.run(); // added call run method.
} else if (aActionEvent.getSource() == m_cTReal) {
run method:
public void run()
{
if(m_bRunning)
{
new Thread(){ //Thread to release screen
#Override
public void run() {
JuliaSet.this.handleCalculation();
}
}.start(); //also thread must be started
}
}
As said by #RubioRic, SwingUtilities.invokeLater method is also a part of solution. But you need to check whole of your code and you should learn about Threads.
I want to track the device with GPS signal. At the same time show latitude, longitude, altitude and speed.
I use this code and there is no result on the screen:
#Override
protected void onGpsTracker_ButtonAction(Component c, ActionEvent event) {
try {
int i = 0;
Location loc = mylocLocationManager.getCurrentLocation();
if(loc.getStatus()==LocationManager.AVAILABLE) {
} else {
Dialog.show("Error!", "Falla de señal", "OK", null);
}
mylocLocationManager.setLocationListener(new LocationListener() {
public void locationUpdated(Location location) {
gpsLocation= location;
Component c = null;
Label labelalt = (Label)findByName("altitudT", c);
Label labellat = (Label)findByName("latitudT", c);
Label labellng = (Label)findByName("longitudT", c);
Label labeldist = (Label)findByName("distanciaT", c);
Label labelspeed = (Label)findByName("speedT", c);
altmax= location.getAltitude();
double lat = location.getLatitude();
double lng = location.getLongitude();
float speed = location.getVelocity();
double alt = location.getAltitude();
velprompos = velprompos + 1;
totspeed = (int) (totspeed + speed);
velopro = totspeed / velprompos;
totalt = altmax - alt;
velmax=speed;
Coord lastLocation = new Coord(lat, lng);
mapComponent.zoomTo(lastLocation, 15);
prevdistance = totdistance;
prevLon = currentLon;
prevLat = currentLat;
String Salt = String.valueOf(alt);
labelalt.setText(Salt);
String Slat = String.valueOf(lat);
labellat.setText(Slat);
String Slng = String.valueOf(lng);
labellng.setText(Slng);
String Sspeed = String.valueOf(speed);
labelspeed.setText(Sspeed);
//aca hay q pner dibujo lineas
}
public void providerStateChanged(int newState) {
//positionMethod();
}
});
} catch (Exception ex) {
ex.printStackTrace();
gpsLocation = null;
}
}
The idea is to get the location when the device is moving, and show this results on some labels. Any help?
You are implementing couple of things wrongly.
Add #override to overriden methods like locationUpdated and providerStateChanged .
Don't re-declare Component c as and set it to null
Get your labels by calling them directly with find method and pass c as a parameter
revalidate or repaint your form after each update.
As a general advice, keep your variable declaration consistence, declare Number of Mangoes as numOfMangoes and not numberofmangoes nor numofmangoes nor Numberofmangoes.
#Override
protected void onGpsTracker_ButtonAction(Component c, ActionEvent event) {
try {
int i = 0;
Location loc = mylocLocationManager.getCurrentLocation();
if (loc.getStatus() == LocationManager.AVAILABLE) {
System.out.println("Location available");
} else {
Dialog.show("Error!", "Falla de señal", "OK", null);
}
final LocationManager mylocLocationManager = LocationManager.getLocationManager();
mylocLocationManager.setLocationListener(new LocationListener() {
#Override
public void locationUpdated(Location location) {
gpsLocation = location;
Label labelspeed = ;
altmax = location.getAltitude();
double lat = location.getLatitude();
double lng = location.getLongitude();
float speed = location.getVelocity();
double alt = location.getAltitude();
velprompos = velprompos + 1;
totspeed = (int) (totspeed + speed);
velopro = totspeed / velprompos;
totalt = altmax - alt;
velmax = speed;
Coord lastLocation = new Coord(lat, lng);
mapComponent.zoomTo(lastLocation, 15);
prevdistance = totdistance;
prevLon = currentLon;
prevLat = currentLat;
String Salt = String.valueOf(alt);
findAltitudT(c).setText(Salt);
String Slat = String.valueOf(lat);
findLatitudT(c).setText(Slat);
String Slng = String.valueOf(lng);
findLongitudT(c).setText(Slng);
String Sspeed = String.valueOf(speed);
findSpeedT(c).setText(Sspeed);
c.getComponentForm().revalidate();
//aca hay q pner dibujo lineas
}
#Override
public void providerStateChanged(int newState) {
//positionMethod();
}
});
} catch (Exception ex) {
ex.printStackTrace();
gpsLocation = null;
}
}
public class signal_identifier {
private static final Log LOG = LogFactory.getLog(signal_identifier.class);
public static void main(String[] args) throws Exception {
long t = cvGetTickCount();
Configuration conf = new Configuration();
long milliSeconds = 1800000;
conf.setLong("mapred.task.timeout", milliSeconds);
Job job = new Job(conf, "TrafficSignalProcessing");
job.setJarByClass(signal_identifier.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(signal_mapper.class);
job.setReducerClass(signal_reducer.class);
job.setInputFormatClass(VideoInputFormat.class);
job.setOutputValueClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/tmp/traffic_signal.mp4"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/tmp/ouputv"));
job.waitForCompletion(true);
}
}
Mapper Class:
public class signal_mapper extends Mapper<Text, VideoObject, Text, IntWritable> {
private static final Log LOG = LogFactory.getLog(signal_mapper.class);
private static OpenCVFrameGrabber grabber;
private static IplImage currentFrame;
private static IplImage frame;
private static IplImage imgHSV;
private static IplImage imgThresholdr;
private static IplImage imgThresholdg;
private static IplImage imgC;
static int LowerRed = 160;
static int UpperRed = 180;
static int LowerGreen = 40;
static int UpperGreen = 80;
CvArr mask;
//private static final int FOURCC = CV_FOURCC('X', 'V', 'I', 'D');
public void map(Text key, VideoObject value, Context context, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException, InterruptedException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(value.getVideoByteArray());
LOG.info("Log__VideoConverter__byteArray: "+ byteArrayInputStream.available());
String fileName = key.toString();
int id = value.getId();
long differencer = 0;
long differenceg = 0;
long lStartTime = 0;
String flag = "start";
//LocalFileSystem fs = FileSystem.getLocal(context.getConfiguration());
Path filePath = new Path("/tmp", fileName);
//Path resFile = new Path("/tmp", "res_"+fileName);
System.out.println("File to Process :"+filePath.toString());
//FSDataOutputStream out = fs.create(filePath, true);
//out.write(value.getVideoByteArray());
//out.close();
try{
grabber = new OpenCVFrameGrabber(filePath.toString());
grabber.start();
CvMemStorage storage = CvMemStorage.create();
CvSize frameSize = new CvSize(grabber.getImageWidth(), grabber.getImageHeight());
currentFrame = cvCreateImage(frameSize, 8, 3);
IplImage cropped;// = cvCreateImage(frameSize, 8, 3);
CvRect r = new CvRect(250, 40, 350, 350);
System.out.println("Video processing .........started");
while(queryFrame()) {
cvClearMemStorage(storage);
if(flag.equals("start")){
lStartTime = new Date().getTime();
}
cvSetImageROI(currentFrame, r);
cropped = cvCreateImage(cvGetSize(currentFrame), currentFrame.depth(),currentFrame.nChannels());
// Copy original image (only ROI) to the cropped image
cvCopy(currentFrame, cropped);
imgHSV = cvCreateImage(cvGetSize(cropped), 8, 3);
cvCvtColor(cropped, imgHSV, CV_BGR2HSV);
imgThresholdr = cvCreateImage(cvGetSize(cropped), 8, 1);
imgThresholdg = cvCreateImage(cvGetSize(cropped), 8, 1);
imgC = cvCreateImage(cvGetSize(cropped),8,1);
cvInRangeS(imgHSV, cvScalar(LowerRed,150,75,0), cvScalar(UpperRed, 255, 255, 0), imgThresholdr);
cvInRangeS(imgHSV, cvScalar(LowerGreen,150,75,0), cvScalar(UpperGreen, 255, 255, 0), imgThresholdg);
Dimension positionr = getCoordinates(imgThresholdr);
int posr = positionr.width+positionr.height;
Dimension positiong = getCoordinates(imgThresholdg);
int posg = positiong.width+positiong.height;
//&& !flag.equalsIgnoreCase("red") && !flag.equalsIgnoreCase("green")
if(posr > 255 && posr < 265 ){
flag = "red";
}else {
long lEndTime = new Date().getTime();
differenceg = (lEndTime - lStartTime) - differencer;
output.collect(new Text("Green Color found at second- => "),new IntWritable((int)differenceg/1000));
//System.out.println("Green Color found at second: " + differenceg/1000);
}
if(posg > 430 && posg < 440){
flag = "green";
}else{
long lEndTime = new Date().getTime();
differencer = (lEndTime - lStartTime) - differenceg;
output.collect(new Text("Red Color found at second- => "),new IntWritable((int)differencer/1000));
//System.out.println("Red Color found at second: " + differencer/1000);
}
}
grabber.stop();
System.out.println("Video processing .........Completed");
}catch(Exception e) {
e.printStackTrace();
}
}
private static boolean queryFrame() throws Exception {
try {
IplImage frame = grabber.grab();
if (frame != null) {
cvConvertImage(frame, currentFrame, 0);
return true;
} else {
return false;
}
}catch(com.googlecode.javacv.FrameGrabber.Exception fge) {
return false;
}
catch(Exception e) {
return false;
}
}
static Dimension getCoordinates(IplImage thresholdImage) {
int posX = 0;
int posY = 0;
CvMoments moments = new CvMoments();
cvMoments(thresholdImage, moments, 1);
double momX10 = cvGetSpatialMoment(moments, 1, 0);
double momY01 = cvGetSpatialMoment(moments, 0, 1);
double area = cvGetCentralMoment(moments, 0, 0);
posX = (int) (momX10 / area);
posY = (int) (momY01 / area);
return new Dimension(posX, posY);
}
}
Reducer Class:
public class signal_reducer extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
Wait I'll post my stack trace for Exception
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nextstatschart);
myDataset = new XYMultipleSeriesDataset ();
statsRenderer = new DefaultRenderer();
myRenderer.setLabelsColor(Color.BLUE);
myRenderer.setLabelsTextSize(12);
myRenderer.setAxesColor(Color.BLACK);
myRenderer.setBackgroundColor(Color.WHITE);
myRenderer.setAntialiasing(true);
myRenderer.setShowLabels(true);
myRenderer.setShowGrid(true);
myRenderer.setChartTitle("Next Three Months Statistical Analysis");
myRenderer.setChartTitleTextSize(16);
myRenderer.setZoomEnabled(true);
myRenderer.setChartTitle(heading);
myRenderer.setLabelsColor(Color.BLACK);
myRenderer.setXAxisMin(0);
myRenderer.setXAxisMax(100);
myRenderer.setYAxisMin(0);
myRenderer.setYAxisMax(100);
myRenderer.setYLabelsAlign(Align.RIGHT);
myRenderer.setXTitle ("Case Type");
myRenderer.setYTitle ("Count");
myRenderer.setMargins(new int[] { 30, 40, 10, 40 });
Bundle b = getIntent().getExtras();
typ = b.getStringArray("typ");
nums = b.getStringArray("counts");
percentages = b.getDoubleArray("percs");
stat_count = b.getInt("stat_count");
X_Axis = b.getDoubleArray("typp");
Y_Axis = b.getDoubleArray("contt");
}
#Override
protected void onResume() {
super.onResume();
try {
refreshChart();
if (statsChartView == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.statschart);
// statsChartView = ChartFactory.getPieChartView(getApplicationContext(), statsSeries, statsRenderer);
// statsChartView = ChartFactory.getLineChartView(getApplicationContext(), xyDataset, xyRender);
// statsChartView = ChartFactory.getBarChartView(context, dataset, renderer, type)
statsChartView = ChartFactory.getBarChartView(getApplicationContext(), myDataset, myRenderer,Type.DEFAULT);
layout.addView(statsChartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.addView(statsChartView);
statsChartView.refreshDrawableState();
statsChartView.setBackgroundColor(Color.WHITE);
} else {
statsChartView.repaint();
}
// refreshChart();
}catch(Exception e){
e.printStackTrace();
}
}
void refreshChart() {
try {
mySeries = new XYSeries (series);
int[] colorpool = new int[] { 0xff000099, 0xff009900, 0xffff0000,
0xffffff00, 0xff990099, 0xffff9900, 0xff996600, 0xffcccccc,
0xff990000, 0xff66cccc, 0xff99ccff, 0xffccffcc };
int[] colors = new int[stat_count];
DecimalFormat Currency = new DecimalFormat("#0.00");
for (int i = 0; i < stat_count; i++) {
mySeries.add(Double.parseDouble(typ[i]),Double.parseDouble(nums[i]));
if (i > 11) {
colors[i] = 0xff333333;
} else {
colors[i] = colorpool[i];
}
}
myDataset.addSeries (mySeries);
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
// statsRenderer.addSeriesRenderer(r);
xyRender.addSeriesRenderer(r);
}
mySeriesRenderer = new XYSeriesRenderer ();
if (series.equals("Weight")) {
mySeriesRenderer.setColor (0xff009900);
mySeriesRenderer.setPointStyle(PointStyle.POINT);
} else {
mySeriesRenderer.setColor (0xff990099);
}
myRenderer.addSeriesRenderer (mySeriesRenderer);
int length = myRenderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
((XYSeriesRenderer) myRenderer.getSeriesRendererAt(i)).setFillPoints(true);
}
// Draw the chart
statsChartView.repaint ();
} catch (Exception e) {
e.printStackTrace();
//Exception Code
}
}
}
E/AndroidRuntime(910): FATAL EXCEPTION: main
E/AndroidRuntime(910): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.courts.action/com.courts.action.NextStatsChart}: java.lang.NullPointerException
It can be because of this,
<activity android:name="com.courts.action.NextStatsChart" android:label="#string/app_name" />