当前位置:首页 > Java

java 如何画点

2026-02-05 05:00:33Java

绘制单个点的方法

在 Java 中绘制单个点可以通过 GraphicsGraphics2D 类实现。以下是一个简单的示例代码,展示如何在 JFrame 上绘制一个点:

import javax.swing.*;
import java.awt.*;

public class DrawPoint extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillRect(50, 50, 1, 1); // 在坐标(50,50)绘制一个1x1像素的点
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Draw Point Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DrawPoint());
        frame.setSize(200, 200);
        frame.setVisible(true);
    }
}

使用 Graphics2D 绘制更精确的点

Graphics2D 提供了更多绘图控制选项,可以绘制更精确的点:

java 如何画点

import javax.swing.*;
import java.awt.*;

public class DrawPointWithGraphics2D extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.BLUE);
        g2d.setStroke(new BasicStroke(1));
        g2d.drawLine(100, 100, 100, 100); // 在坐标(100,100)绘制一个点
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Graphics2D Point Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DrawPointWithGraphics2D());
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

绘制多个点的方法

如果需要绘制多个点,可以将坐标存储在数组或列表中,然后循环绘制:

java 如何画点

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class DrawMultiplePoints extends JPanel {
    private List<Point> points = new ArrayList<>();

    public DrawMultiplePoints() {
        points.add(new Point(50, 50));
        points.add(new Point(100, 100));
        points.add(new Point(150, 150));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.GREEN);
        for (Point p : points) {
            g.fillRect(p.x, p.y, 1, 1);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Multiple Points Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DrawMultiplePoints());
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

自定义点的大小和形状

可以通过调整绘制形状的大小和类型来自定义点的外观:

import javax.swing.*;
import java.awt.*;

public class CustomPoint extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // 绘制圆形点
        g2d.setColor(Color.RED);
        g2d.fillOval(50, 50, 5, 5); // 5x5像素的圆形点

        // 绘制方形点
        g2d.setColor(Color.BLUE);
        g2d.fillRect(100, 100, 5, 5); // 5x5像素的方形点
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Custom Point Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CustomPoint());
        frame.setSize(200, 200);
        frame.setVisible(true);
    }
}

使用 BufferedImage 绘制点

对于需要高性能绘图的场景,可以使用 BufferedImage

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class BufferedImagePoint extends JPanel {
    private BufferedImage image;

    public BufferedImagePoint() {
        image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setColor(Color.BLACK);
        g2d.fillRect(0, 0, 200, 200); // 黑色背景
        g2d.setColor(Color.YELLOW);
        g2d.fillRect(50, 50, 1, 1); // 黄色点
        g2d.dispose();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("BufferedImage Point Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new BufferedImagePoint());
        frame.setSize(200, 200);
        frame.setVisible(true);
    }
}

这些方法涵盖了 Java 中绘制点的基本技术,可以根据具体需求选择适合的方法。

标签: java
分享给朋友:

相关文章

java如何输入

java如何输入

使用Scanner类进行输入 Scanner类是Java中最常用的输入工具,适用于从控制台或文件读取数据。需要导入java.util.Scanner包。 基本语法: Scanner sca…

如何配置java

如何配置java

安装Java开发工具包(JDK) 访问Oracle官方网站或OpenJDK项目页面下载适合操作系统的JDK安装包。选择与系统架构匹配的版本(如Windows x64、macOS ARM等)。运行安装程…

java如何调用方法

java如何调用方法

调用方法的基本语法 在Java中,调用方法需要明确方法所属的对象(实例方法)或类(静态方法),并传递必要的参数。语法格式如下: 实例方法:对象名.方法名(参数列表); 静态方法:类名.方法名…

如何卸载java

如何卸载java

卸载Java的步骤 Windows系统卸载方法 打开控制面板,选择“程序和功能”或“卸载程序”。在列表中找到Java相关条目(如“Java 8 Update 251”),右键选择卸载并跟随向导完成操…

java如何产生随机数

java如何产生随机数

使用 Math.random() 方法 Math.random() 生成一个范围在 [0.0, 1.0) 的伪随机 double 值。通过缩放和偏移可以生成指定范围的随机数。 double ra…

java如何遍历map

java如何遍历map

遍历Map的几种方法 在Java中,遍历Map有多种方式,可以根据需求选择合适的方法。以下是常见的几种遍历方式: 使用entrySet遍历 通过entrySet()方法获取键值对的集合,可以同时访问…