I am writing an application in Java (do not offer third-party libraries, please), you need to draw a graph of the function entered from the keyboard. I used g.drawPolyLine(xArray[], yArray[], numOfPoints) , g - an object of class Graphics , also tried g2.draw(new Line2D.Double(double x1, double y1, double x2, double y2)) , g2 - object Graphics2D class, I thought that in the second version the graph will be clearer, but all the same, it turns out some pixels that are sharply striking, here's an example: Schedule

If you make a "feather" thicker, the result is the same. I tried to see how normal graphics are drawn on sites on the Internet - the thickness of the “pen” is 2 pixels, and one of them is lighter, and it creates the illusion that pixelation is not visible. Is it possible to achieve the same result in another way, or how to implement the same (as on websites)?

I tried to set KEY_ANTIALIASING to VALUE_ANTIALIAS_ON (enabled anti-aliasing), improved the quality of the render, didn’t really change (the line became thicker and that’s if the scale was increased, it became a little better, and that’s all).
Here is: After

There is nothing changed except the render settings. It looks a little different, but still not the way we would like. If someone says that it is impossible to do better, here is an example: site yotx.ru Screenshot from a graphing site . I would like to get just such a result.

  • If it comes out to find, try to look at the algorithm for creating lines in Paint . When you draw, the line is also clear, as in the picture, and when you let go - it is transformed. - Rostislav Dugin
  • one
    it's called anti-aliasing, try following the instructions from the official manual - etki

1 answer 1

There is a render setting where you can enable anti-aliasing:

 graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

Read more here .

I can also offer an alternative, JavaFX:

 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.stage.Stage; public class LineChartSample extends Application { @Override public void start(Stage stage) { stage.setTitle("Line Chart Sample"); //Задаем оси final NumberAxis xAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis(); //Имя оси xAxis.setLabel("Something"); //Добавляем их на график final LineChart<Number,Number> lineChart = new LineChart<Number,Number>(xAxis,yAxis); //Его имя lineChart.setTitle("Stock Monitoring, 2010"); //Создаем линию графика XYChart.Series series = new XYChart.Series(); series.setName("My portfolio"); //Втыкаем данные series.getData().add(new XYChart.Data(1, 23)); series.getData().add(new XYChart.Data(2, 14)); series.getData().add(new XYChart.Data(3, 15)); series.getData().add(new XYChart.Data(4, 24)); series.getData().add(new XYChart.Data(5, 34)); series.getData().add(new XYChart.Data(6, 36)); series.getData().add(new XYChart.Data(7, 22)); series.getData().add(new XYChart.Data(8, 45)); series.getData().add(new XYChart.Data(9, 43)); series.getData().add(new XYChart.Data(10, 17)); series.getData().add(new XYChart.Data(11, 29)); series.getData().add(new XYChart.Data(12, 25)); //Создаем окно с результатом. Scene scene = new Scene(lineChart,800,600); lineChart.getData().add(series); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } 

Read more here . The library is included in the jdk library package.

  • A little not what you need: the result is not much better. - Peter Samokhin
  • If you use Graphic2D, the result is poor. If you just use Graphics, it turns out quite tolerable option. - Peter Samokhin