I have a Jfreechart which is plotting some mock real-time data. When I have my domain axis set to auto, the data can be seen updating every second. However, I wish to plot the data over a wider range (say the whole day). When I change the range, I am then unable to see the plot unless I zoom in.
Once zoomed in, the line does not cover the whole graph, but only a portion. This line then moves across the graph instead of growing/drawing along it
/** #see http://stackoverflow.com/questions/5048852 */
public class DTSCTest extends ApplicationFrame {
private static final String TITLE = "Dynamic Series";
private static final String START = "Start";
private static final String STOP = "Stop";
private static final float MINMAX = 100;
private static final int COUNT = 10;
private static final int FAST = 1000;
private static final int SLOW = FAST * 5;
private static final Random random = new Random();
private double gateStart = ThreadLocalRandom.current().nextInt(0, 101);
private boolean returning = false;
private Timer timer;
public DTSCTest(final String title) {
super(title);
final DynamicTimeSeriesCollection dataset =
new DynamicTimeSeriesCollection(1, COUNT, new Second());
Date date = new Date();
dataset.setTimeBase(new Second(date));
float[] gateStartLoad = new float[1];
gateStartLoad[0] = (float)gateStart;
dataset.addSeries(gateStartLoad, 0, "Longwall Data");
JFreeChart chart = createChart(dataset);
final JComboBox combo = new JComboBox();
combo.addItem("Fast");
combo.addItem("Slow");
combo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if ("Fast".equals(combo.getSelectedItem())) {
timer.setDelay(FAST);
} else {
timer.setDelay(SLOW);
}
}
});
this.add(new ChartPanel(chart), BorderLayout.CENTER);
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(combo);
this.add(btnPanel, BorderLayout.SOUTH);
timer = new Timer(FAST, new ActionListener() {
float[] newData = new float[1];
#Override
public void actionPerformed(ActionEvent e) {
if(gateStart == 100){
returning = true;
}else if(gateStart == 0){
returning = false;
}
if(returning){
gateStart--;
}else{
gateStart++;
}
newData[0] = (float)gateStart;
dataset.advanceTime();
System.out.println(dataset.getNewestTime());
dataset.appendData(newData);
}
});
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
TITLE, "hh:mm:ss", "Shearer Position", dataset, true, true, false);
final XYPlot plot = result.getXYPlot();
DateAxis domain = (DateAxis)plot.getDomainAxis();
Calendar calendar = Calendar.getInstance();
calendar.set(2021, 0, 6);
System.out.println(new Date());
System.out.println(calendar.getTime());
domain.setRange(new Date(), calendar.getTime());
domain.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));
ValueAxis range = plot.getRangeAxis();
range.setRange(0, 100);
return result;
}
public void start() {
timer.start();
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
DTSCTest demo = new DTSCTest(TITLE);
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
demo.start();
}
});
}
}
How do I make it so that the line is continuous (shows every observed point of data in the series), and how do I make it visible when I manually set the range
I removed domain.setRange(new Date(), calendar.getTime()); and changed nMoments in my DynamicTimeSeriesCollection(1, COUNT, new Second());, making COUNT a multiple of 60 and increasing its value. My x-axis now properly shows time however the plot is not continuous, it disappears after a time
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++;
}
It is the current state of my project
I want to change this
this is my code
Blockquote
public class MainActivity extends AppCompatActivity {
LineChart mChart;
private static final float MAX_BP_VALUE = 220f;
private static final float MAX_BT_VALUE = 42f;
private static final float UPPER_LIMIT = 130.0f;
private static final float LOWER_LIMIT = 50.0f;
private static SimpleDateFormat mFormat = new SimpleDateFormat("yyyy:mm:dd");
IAxisValueFormatter xAxisFormatter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFormat.setTimeZone(TimeZone.getDefault());
mChart = (LineChart) findViewById(R.id.chart);
xAxisFormatter = new IAxisValueFormatter() {
public int getDecimalDigits() {
return 0;
}
#Override
public String getFormattedValue(float value, AxisBase axis) {
return mFormat.format(new Date((long) value));
}
};
setupChart();
setupAxes();
setupData();
setLegend();
Button drawButton = (Button) findViewById(R.id.drawButton);
drawButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LineData data = mChart.getData();
LineDataSet dataset = BPcreateSet();
try {
dataset.addEntry(new Entry(new Long(mFormat.parse("2018:05:11").getTime()).floatValue(), 110));
dataset.addEntry(new Entry(new Long(mFormat.parse("2018:06:13").getTime()).floatValue(), 150));
dataset.addEntry(new Entry(new Long(mFormat.parse("2018:07:15").getTime()).floatValue(), 120));
dataset.addEntry(new Entry(new Long(mFormat.parse("2018:08:17").getTime()).floatValue(), 80));
dataset.addEntry(new Entry(new Long(mFormat.parse("2018:09:18").getTime()).floatValue(), 40));
} catch(Exception e) {
e.printStackTrace();
}
ArrayList<Integer> colorList = new ArrayList<Integer>();
colorList.add(Color.WHITE);
colorList.add(Color.RED);
colorList.add(Color.WHITE);
colorList.add(Color.WHITE);
colorList.add(Color.BLUE);
dataset.setCircleColors(colorList);
data.addDataSet(dataset);
mChart.setData(data);
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.animateX(1000);
}
});
}
private void setupChart() {
mChart.getDescription().setEnabled(false);
mChart.setTouchEnabled(true);
mChart.setPinchZoom(true);
mChart.setScaleEnabled(true);
mChart.setDrawGridBackground(false);
mChart.setBackgroundColor(Color.BLACK);
mChart.setMarker(new XYMarkerView(this, xAxisFormatter));
mChart.setDrawMarkers(true);
}
private void setupAxes() {
XAxis xl = mChart.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
xl.setTextColor(Color.WHITE);
xl.setDrawGridLines(false);
xl.setAvoidFirstLastClipping(true);
xl.setEnabled(true);
xl.setLabelRotationAngle(-45.0f);
xl.setGranularityEnabled(true);
xl.setGranularity(1.0f/6.0f); // per 10 minutes
xl.setDrawGridLines(true);
xl.setAxisLineWidth(2f);
xl.setAxisLineColor(Color.WHITE);
xl.setValueFormatter(xAxisFormatter);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTextColor(Color.WHITE);
leftAxis.setAxisMaximum(MAX_BP_VALUE);
leftAxis.setAxisMinimum(20f);
leftAxis.setGranularity(20f);
leftAxis.setAxisLineWidth(2f);
leftAxis.setAxisLineColor(Color.WHITE);
leftAxis.setLabelCount(10);
leftAxis.setDrawGridLines(true);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setTextColor(Color.WHITE);
rightAxis.setAxisMaximum(MAX_BT_VALUE);
rightAxis.setAxisMinimum(32f);
rightAxis.setGranularity(2f);
rightAxis.setAxisLineWidth(2f);
rightAxis.setAxisLineColor(Color.WHITE);
rightAxis.setDrawGridLines(false);
xl.setSpaceMin(50);
LimitLine ul = new LimitLine(UPPER_LIMIT, "Upper Limit");
ul.setLineWidth(2f);
ul.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
ul.setTextSize(10f);
ul.setTextColor(Color.WHITE);
LimitLine ll = new LimitLine(LOWER_LIMIT, "Lower Limit");
ll.setLineColor(Color.BLUE);
ll.setLineWidth(2f);
ll.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_TOP);
ll.setTextSize(10f);
ll.setTextColor(Color.WHITE);
LimitLine llI = new LimitLine(400f, "체온");
llI.setLineColor(Color.BLUE);
llI.setLineWidth(2f);
llI.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);
leftAxis.removeAllLimitLines();
leftAxis.addLimitLine(ul);
leftAxis.addLimitLine(ll);
xl.addLimitLine(llI);
leftAxis.setDrawLimitLinesBehindData(true);
}
private void setupData() {
LineData data = new LineData();
data.setValueTextColor(Color.WHITE);
// add empty data
mChart.setData(data);
}
private void setLegend() {
Legend l = mChart.getLegend();
l.setForm(Legend.LegendForm.CIRCLE);
l.setTextColor(Color.WHITE);
}
private LineDataSet BPcreateSet() {
LineDataSet set = new LineDataSet(null, "pulse");
set.setAxisDependency(YAxis.AxisDependency.LEFT);
set.setColors(Color.argb(255, 0, 255, 0));
set.setLineWidth(3f);
set.setCircleRadius(6f);
set.setValueTextColor(Color.WHITE);
set.setValueTextSize(10f);
set.setDrawValues(false);
return set;
}
}
I want to mark the X-axis like the picture above.
Please excuse me that First and last values is empty because I am poor at Photoshop.
Is there a solution?
You can also recommend another library that represents the time value and indicates the value required for the X-axis
first of: I am really having a problem with JFreechart and mainly I really believe that this is my fault because I start using the library without fully understanding how it fully function or use
second of: these are some helpful resource that helped me :
check it out 1
check it out 2
check it out 3
my current state : my problem is in making use of the drawPrimaryLine()
in my already build project so I am still having a problem in connecting the dots
in my way, not in a sequence way
example: if I enter (10,10) and (15,15) and (20,20) and (25,25) in this sequence, this is what I will end up with (the left side without connecting, the right side with connecting)
My problem is:
1 - when drawing a line is showing on the right side, I don't want the line to be generated or created until all of the points are add and the button done has been clicked *show in the most bottom right side
2 - I don't want the showing line to be in a sequence way I want the line to be shown base on some algorithm and not all dots will have or a line will pass through it, only a line will pass in some case.
so, in summary: not all dots will be connected only some based on an algorithm.
this is my code :
public class x_y_2 extends JFrame {
private static final String title = "Connecting The Dots";
private XYSeries added = new XYSeries("Added");
public List ls = new LinkedList<XYSeries>();
private XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
final XYSeriesCollection dataset = new XYSeriesCollection();
private XYPlot plot ;
public x_y_2(String s) {
super(s);
final ChartPanel chartPanel = createDemoPanel();
this.add(chartPanel, BorderLayout.CENTER);
JPanel control = new JPanel();
JLabel label = new JLabel("Enter 'x' value");
JTextField Field_x = new JTextField();
Field_x.setPreferredSize( new Dimension( 100, 24 ));
JLabel label2 = new JLabel("Enter 'y' value");
JTextField Field_y = new JTextField();
JLabel error = new JLabel("Importent*** in case no value is entered,value is set to '1' ");
error.setForeground(Color.RED);
Field_y.setPreferredSize( new Dimension( 100, 24 ));
control.add(label);
control.add(Field_x);
control.add(label2);
control.add(Field_y);
control.add(new JButton(new AbstractAction("Add") {
#Override
public void actionPerformed(ActionEvent e) {
if (Field_x.getText().isEmpty()) {
Field_x.setText("1"); ;
}
if (Field_y.getText().isEmpty()) {
Field_y.setText("1");
}
Double x = Double.parseDouble(Field_x.getText());
Double y = Double.parseDouble(Field_y.getText());
added.add(x,y);
ls.add(added);
Field_x.setText("");
Field_y.setText("");
}
}));
control.add(error);
control.add(new JButton(new AbstractAction("Done..") {
#Override
public void actionPerformed(ActionEvent e) {
label.setVisible(false);
label2.setVisible(false);
Field_x.setVisible(false);
Field_y.setVisible(false);
error.setVisible(false);
PrimaryLine pr = new PrimaryLine(3);
GraphingData graphingdata = new GraphingData(2,4,2,10);
// pr.drawPrimaryLine(state, g2, plot, dataset, pass, series, item, domainAxis, rangeAxis, dataArea);
System.out.println(ls.size());
for (int i = 0 ; i < ls.size() ; i++) {
XYSeries xy = (XYSeries)ls.get(i);
System.out.println(xy.getX(i) +" "+xy.getY(i));
}
}
}));
this.add(control, BorderLayout.SOUTH);
}
private XYDataset createSampleData() {
dataset.addSeries(added);
return dataset;
}
private ChartPanel createDemoPanel() {
JFreeChart jfreechart = ChartFactory.createXYLineChart(
title, "X", "Y", createSampleData(),PlotOrientation.VERTICAL, true, true, false);
plot =jfreechart.getXYPlot();
renderer.setSeriesLinesVisible(0, true);
renderer.setSeriesShapesVisible(0, true);
plot.setRenderer(renderer);
return new ChartPanel(jfreechart);
}}
second class :
public class GraphingData extends JPanel {
double x_st , y_st , x_ed, y_ed = 0;
public Graphics2D g2 ;
public GraphingData(double x_st,double y_st,double x_ed,double y_ed) {
this.x_st = x_st ;
this.y_st = y_st;
this.x_ed = x_ed;
this.y_ed = y_ed;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(new Line2D.Double(x_st,y_st,x_ed, y_ed));
}
}
Third Class :
public class PrimaryLine extends XYLineAndShapeRenderer {
private final int anchor;
public PrimaryLine(int acnchor) {
this.anchor = acnchor;
}
#Override
protected void drawPrimaryLine(XYItemRendererState state, Graphics2D g2,
XYPlot plot, XYDataset dataset, int pass, int series, int item,
ValueAxis domainAxis, ValueAxis rangeAxis, Rectangle2D dataArea) {
if (item == anchor) {
return;
}
}
public void chart() {
PrimaryLine r = new PrimaryLine(8);
XYPlot plot = new XYPlot(createSampleData(),new NumberAxis("X"), new
NumberAxis("Y"), r);
JFreeChart chart = new JFreeChart(plot);
}
private XYDataset createSampleData() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
XYSeries added = new XYSeries("added");
added.add(4,2);
added.add(2,1);
xySeriesCollection.addSeries(added);
return xySeriesCollection;
}
}
Any kinda of help I would be greatfull for .
I'm writing a Shooter (FPS - First person shooter) and I'm using OpenGl (jogl 1.0)
with JFrame .
I want to add an ActionListener to the JFrame :
public class Main extends JDialog {
private static ActionListener action;
private static JFrame framePhaseOne;
private static JFrame framePhaseTwo;
...
...
action = new ActionListener() // this is for PHASE 2
{
public void actionPerformed(ActionEvent ae)
{
if (userPoints.getGamePhase()) // if F2 was clicked
{
framePhaseTwo = new JFrame(WorldName2);
framePhaseTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
framePhaseTwo.setLocationByPlatform(true);
framePhaseTwo.setLocation(FRAME_LOCATION_X, FRAME_LOCATION_Y);
Renderer_PhaseTwo myCanvas2 = new Renderer_PhaseTwo(userPoints);
final Animator animator2 = new Animator(myCanvas2);
framePhaseTwo.add(myCanvas2);
framePhaseTwo.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);
framePhaseTwo.addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing(WindowEvent e)
{
new Thread()
{
#Override
public void run()
{
animator2.stop();
System.exit(0);
}
}.start();
}
});
framePhaseTwo.setVisible(true);
animator2.start();
myCanvas2.requestFocus();
myCanvas2.setFocusable(true);
}
}
};
I want to add action to framePhaseOne , how can I do that , without using JPanel and buttons ?
Here is the full code if Main class , if needed :
/**
* This is the main class that runs the First Person Java app
* using the OpenGL mechanism , with JOGL 1.0
* #author X2
*
*/
public class Main extends JDialog
{
// when true permission granted for starting the game
private static boolean start = false;
private static final long serialVersionUID = 1L;
protected static TimerThread timerThread;
static JStatusBar statusBar = new JStatusBar();
private static JFrame framePhaseOne;
private static JFrame framePhaseTwo;
private static ActionListener action;
/**
* framePhaseOne properties
*/
private static final int FRAME_LOCATION_X = 300;
private static final int FRAME_LOCATION_Y = 50;
private static final int FRAME_SIZE_X = 850; // animator's target frames per second
private static final int FRAME_SIZE_Y = 700; // animator's target frames per second
/**
* start button properties
*/
private static final int BUTTON_LOCATION_X = (FRAME_SIZE_X / 2) - 100;
private static final int BUTTON_LOCATION_Y = (FRAME_SIZE_Y / 2) - 50;
private static final int BUTTON_SIZE_X = 140; // animator's target frames per second
private static final int BUTTON_SIZE_Y = 50; // animator's target frames per second
/**
* timer & game title & arrow picture
*/
private static final String WorldName1 = "FPS 2013 CG Project - Phase 1";
private static final String WorldName2 = "FPS 2013 CG Project - Phase 2";
private static final String HARD_TARGET = "src/res/target.jpg";
private static final String runningOut = "Time is running out - you have : ";
static int interval;
static Timer timer1;
static JLabel changingLabel1 = null;
static Points userPoints = new Points();
/**
* Timer properties
*/
private static Timer timer;
private static int count = 60;
/**
* ActionListener for timer
*/
private static ActionListener timerAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (start)
{
count--;
if (count == 0)
timer.stop();
changingLabel1.setText(runningOut + count + " seconds" + " , and your points are: "
+ userPoints.getPoints());
}
}
};
public static void exitProcedure() {
System.out.println();
timerThread.setRunning(false);
System.exit(0);
}
/**
* Clock timer1
* #author X2
*
*/
public static class TimerThread extends Thread
{
protected boolean isRunning;
protected JLabel dateLabel;
protected JLabel timeLabel;
protected SimpleDateFormat dateFormat =
new SimpleDateFormat("EEE, d MMM yyyy");
protected SimpleDateFormat timeFormat =
new SimpleDateFormat("h:mm a");
public TimerThread(JLabel dateLabel, JLabel timeLabel) {
this.dateLabel = dateLabel;
this.timeLabel = timeLabel;
this.isRunning = true;
}
#Override
public void run() {
while (isRunning) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Calendar currentCalendar = Calendar.getInstance();
Date currentTime = currentCalendar.getTime();
dateLabel.setText(dateFormat.format(currentTime));
timeLabel.setText(timeFormat.format(currentTime));
}
});
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
}
}
}
public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}
}
/**
*
* #param args
*/
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
framePhaseOne = new JFrame(WorldName1);
action = new ActionListener() // this is for PHASE 2
{
public void actionPerformed(ActionEvent ae)
{
if (userPoints.getGamePhase()) // if F2 was clicked
{
framePhaseTwo = new JFrame(WorldName2);
framePhaseTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
framePhaseTwo.setLocationByPlatform(true);
framePhaseTwo.setLocation(FRAME_LOCATION_X, FRAME_LOCATION_Y);
Renderer_PhaseTwo myCanvas2 = new Renderer_PhaseTwo(userPoints);
final Animator animator2 = new Animator(myCanvas2);
framePhaseTwo.add(myCanvas2);
framePhaseTwo.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);
framePhaseTwo.addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing(WindowEvent e)
{
new Thread()
{
#Override
public void run()
{
animator2.stop();
System.exit(0);
}
}.start();
}
});
framePhaseTwo.setVisible(true);
animator2.start();
myCanvas2.requestFocus();
myCanvas2.setFocusable(true);
}
}
};
final Container contentPane = framePhaseOne.getContentPane();
contentPane.setLayout(new BorderLayout());
/**
* the timer of the count-down
*/
timer = new Timer(1000, timerAction);
timer.start();
changingLabel1 = new JLabel("Game is offline , hit Start to continue !");
statusBar.setLeftComponent(changingLabel1);
final JLabel dateLabel = new JLabel();
dateLabel.setHorizontalAlignment(JLabel.CENTER);
statusBar.addRightComponent(dateLabel);
final JLabel timeLabel = new JLabel();
timeLabel.setHorizontalAlignment(JLabel.CENTER);
statusBar.addRightComponent(timeLabel);
contentPane.add(statusBar, BorderLayout.SOUTH);
/**
* start button
*/
final JButton startButton = new JButton("Start the game !");
// startButton.setBounds(300, 50,140, 50 );
startButton.setBounds(BUTTON_LOCATION_X
, BUTTON_LOCATION_Y,
BUTTON_SIZE_X,
BUTTON_SIZE_Y );
startButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
start = true; // start the game
userPoints.startGame();
contentPane.remove(startButton);
contentPane.revalidate();
contentPane.repaint();
}
});
contentPane.add(startButton);
framePhaseOne.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
framePhaseOne.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent event) {
exitProcedure();
}
});
timerThread = new TimerThread(dateLabel, timeLabel);
timerThread.start();
Renderer_PhaseOne myCanvas = new Renderer_PhaseOne(userPoints);
final Animator animator = new Animator(myCanvas);
Toolkit t = Toolkit.getDefaultToolkit();
BufferedImage originalImage = null;
try
{
originalImage = ImageIO.read(new File(HARD_TARGET));
}
catch (Exception e1) {e1.printStackTrace();}
Cursor newCursor = t.createCustomCursor(originalImage, new Point(0, 0), "none");
framePhaseOne.setCursor(newCursor);
framePhaseOne.setLocation(FRAME_LOCATION_X, FRAME_LOCATION_Y);
framePhaseOne.add(myCanvas);
framePhaseOne.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);
framePhaseOne.addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing(WindowEvent e)
{
new Thread()
{
#Override
public void run()
{
animator.stop();
System.exit(0);
}
}.start();
}
});
framePhaseOne.setVisible(true);
animator.start();
myCanvas.requestFocus();
myCanvas.setFocusable(true);
}
});
}
}
Regards
You can't add an ActionListener to a JFrame, it does not function like a button and so has no action listeners.
What you are looking for is a MouseListener. It detects mouse clicks. You may also be interested in a MouseMotionListener which give you information on mouse movement.
Here's an example:
framePhaseOne.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
System.out.println("Mouse was clicked on my frame!");
}
};
MouseAdapter is an abstract class which implements MouseListener. It keeps you from having to implement all the methods required by the MouseListener interface.
Edit:
After speaking with you in comments below what you're looking for is a KeyListener. Again, I recommend the KeyAdapter for the same reasons as MouseAdapter. Here's an example:
framePhaseOne.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_F2){
//close frame one.
}
}
});
Also do this with framePhaseTwo if you want it to close your first frame as well.
framePhaseTwo.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_F2){
//close frame one
}
}
});
Please note, the frame requires focus to receive key events.
I want to compress and store data of real time line graph I tried but not succeeded
public class DTest extends ApplicationFrame {
javax.swing.Timer _timer;
int nPoints = 200;
float[] history;
/** The most recent value added. */
private float lastValue = (float) 100.0;
DynamicTimeSeriesCollection dataset;
JPanel content;
private final ChartPanel chartPanel;
public DTest(final String title) {
super(title);
history = new float[nPoints];
dataset = new DynamicTimeSeriesCollection(
1, nPoints, new Second()//here speed will set
);
dataset.setTimeBase(new Second(0,0,0,1,1,2000));
dataset.addSeries(new float[]{0.0f}, 0, "S1");
System.out.println("Series count = " + dataset.getSeriesCount());
final JFreeChart chart = createChart(dataset);
chartPanel = new ChartPanel(chart);
content = new JPanel(new FlowLayout());
final JButton btn = new JButton("Stop");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_timer.stop();
}
});
final JButton btn1 = new JButton("Run");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// create new dataset and chart, set the new chart in the chartpanel
//createChart(dataset);
_timer.start();
}
});
JComboBox comb = new JComboBox();
comb.addItem("Select");
comb.addItem("Joy Stick");
content.add(chartPanel);//panel for chart
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(btn);
btnPanel.add(btn1);
btnPanel.add(comb);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(content, BorderLayout.NORTH);
pane.add(btnPanel, BorderLayout.CENTER);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
//setContentPane(content);
comb.addActionListener(new ActionListener() {
private float[] float_array;
private int itemCount;
public void actionPerformed(ActionEvent e) {
JComboBox jComb = (JComboBox) e.getSource();
if (jComb.getSelectedItem().equals("Joy Stick")) {
System.out.println("Joy Stick is Pressed");
try {
float_array = new float[1];
float_array[0] = 0;
itemCount = 0;
dataset.appendData(float_array);
dataset.addSeries(new float[]{0.0f}, 0, "S1");
_timer = new javax.swing.Timer(1, new ActionListener() { // 500ms
private int resizes;
private int inserted;
public void actionPerformed(ActionEvent e) {
double factor = 0.90 + 0.2 * Math.random();
lastValue = lastValue * (float) factor;
float_array[0] = lastValue;
System.out.println("lastValue is " + lastValue);
inserted++;
if ( inserted % (resizes+1)==0 )
dataset.appendData(float_array, itemCount++, 1);
history[itemCount] = lastValue;
if (itemCount >= nPoints - 1) {
resizes++;
DynamicTimeSeriesCollection newSet = new DynamicTimeSeriesCollection(1, nPoints, new Second());
newSet.setTimeBase(new Second(0,0,0,2,2,2000));
newSet.addSeries(new float[]{0.0f}, 0, "S1");
itemCount /= 2;
for (int i = 1; i < nPoints; i++) {
history[i / 2] = history[i];
float_array[0]=history[i / 2];
newSet.appendData(float_array, i/2, 1);
history[i] = 0;
}
chartPanel.setChart(createChart(newSet));
dataset = newSet;
chartPanel.repaint();
}
}
});
_timer.setRepeats(true);
_timer.start();
} catch (NullPointerException ne) {
System.out.println("NullPointer Exception" + ne.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
} else { ;
}
}
});
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
"Dynamic Graph", "Time", "Value", dataset, true, true,
false);
final XYPlot plot = result.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
//plot.setRangeAxis(WIDTH, axi)
axis.setAutoRange(true);
//axis.setFixedAutoRange(60.0); // 60 seconds
axis = plot.getRangeAxis();
axis.setRange(-100.0, 200.0);
return result;
}
public static void main(final String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
final DTest demo = new DTest("Dynamic Graph");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
demo.setVisible(true);
} catch (Exception e) {
}
}
});
}
}
…as the line moves forward, the previous line value should not disappear, but it should begin to compress itself.
The Memory Usage tab of the demo does exactly what you describe.