Draw a point: use a drawLine() method : Line « 2D Graphics GUI « Java






Draw a point: use a drawLine() method

    

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Points extends JPanel {

  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(Color.red);

    for (int i = 0; i <= 100000; i++) {
      Dimension size = getSize();
      int w = size.width ;
      int h = size.height;

      Random r = new Random();
      int x = Math.abs(r.nextInt()) % w;
      int y = Math.abs(r.nextInt()) % h;
      g2d.drawLine(x, y, x, y);
    }
  }

  public static void main(String[] args) {
    Points points = new Points();
    JFrame frame = new JFrame("Points");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(points);
    frame.setSize(250, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

   
    
    
    
  








Related examples in the same category

1.Drawing a Line using Java 2D Graphics API
2.Line StylesLine Styles
3.Dash style line
4.Line dashes style 2
5.Lines Dashes style 3
6.Line Dash Style 4
7.Program to draw gridsProgram to draw grids
8.Xsplinefun displays colorful moving splines in a window
9.A line is drawn using two points
10.Line-graph drawable
11.Draw Dashed
12.Draw Optimized Line
13.Draw with Line2D.Double and Ellipse2D.Double