0

I am trying to build a simple JFreeChart XYLineChart object, and embed it into a ChartPanel object.

For some unknown reason, the plot area doesn't look properly: You can see how gridlines are inconsistent in thickness, and the edges of the plot have these thick black markings in random places. What could be the cause of this?

public class Main extends JFrame() {

public static void main (String [] args) {
ECGPanel myECGPanel = new ECGPanel();
this.add(myECGPanel);
      }

}


public class ECGPanel extends Jpanel {
lineChart= ChartFactory.createXYLineChart("ECG", "Time(ms)", "Voltage(mV)", dataset,
                      PlotOrientation.VERTICAL, true, false, false);

 chartPanel=new ChartPanel(lineChart);
 chartPanel.setPreferredSize(new Dimension(1000,400));

 this.add(chartPanel);
}

Output of the code

Ibrahim7
  • 113
  • 2
  • 7
  • 1
    Windows with a display scale factor of 150% (or something else but 100%)? – user16320675 Feb 07 '23 at 15:16
  • @user16320675 sorry, what do you mean by that? – Ibrahim7 Feb 07 '23 at 15:46
  • 1
    Are you using a WIndows System? If yes, is the display scale of your system set to some value other than 100%? (e.g. the *default* 150% - right click on the desktop - `Display settings`) – user16320675 Feb 07 '23 at 15:51
  • @user16320675 It seems to be solved when I switch the scale from 125% to 100%. But I can't keep the computer on those settings. Do you know why this doesn't happen when I embed my JFreeChart in a SwingNode inside a JavaFX project? It happens only with pure Swing – Ibrahim7 Feb 07 '23 at 20:47
  • 1
    This seems more like a layout problem. Please [edit] your question to include a [mre] that reproduces the result shown. – trashgod Feb 09 '23 at 00:20
  • 1
    Swing does not interpolate images properly on Windows systems with screen scaling. You can eliminate the scaling by running your Java application with [one of these commands](https://stackoverflow.com/questions/30555401/java-disable-dpi-aware-not-working/57926454#57926454). – Gilbert Le Blanc Feb 10 '23 at 13:56

1 Answers1

0

As it was pointed out in comments, the problem relates to Windows and its Display settings. I have found the discussion about how Swing behaves on higher DPI systems here

Interestingly, I don't have this problem when I create a JFreeChart in JavaFX (both using a SwingNode and when using a ChartViewer). You can also see read about that here

Ibrahim7
  • 113
  • 2
  • 7
  • 3
    Such artifacts are usually the result of a double-buffering using the logical component size as the buffer’s pixel size. So, I’d try `false` for the `useBuffer` parameter of [this constructor](https://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/ChartPanel.html#ChartPanel-org.jfree.chart.JFreeChart-boolean-). – Holger Feb 08 '23 at 13:23
  • 1
    This may be a layout problem. The default layout of `JPanel`, `FlowLayout`, ignores preferred size, mentioned [here](https://stackoverflow.com/a/10277372/230513); what is `Jpanel`? – trashgod Feb 09 '23 at 00:19
  • @Holger that fixed it! You can post an answer so I accept it – Ibrahim7 Feb 19 '23 at 17:54