java如何画图
Java绘图基础
Java中绘图主要通过java.awt和javax.swing包实现。核心类是Graphics,它提供了绘制图形、文本和图像的方法。绘图通常在JPanel或JComponent的paintComponent方法中进行。
import javax.swing.*;
import java.awt.*;
class MyPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(0, 0, 100, 100);
}
}
public class DrawingExample {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new MyPanel());
frame.setSize(300, 300);
frame.setVisible(true);
}
}
基本图形绘制
Graphics类提供多种绘图方法:
drawLine(int x1, int y1, int x2, int y2):绘制直线drawRect(int x, int y, int width, int height):绘制矩形fillRect(int x, int y, int width, int height):填充矩形drawOval(int x, int y, int width, int height):绘制椭圆fillOval(int x, int y, int width, int height):填充椭圆
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(50, 50, 100, 80);
g.setColor(Color.BLUE);
g.drawOval(150, 150, 80, 80);
}
高级绘图技术
使用Graphics2D可以获得更高级的绘图功能。Graphics2D是Graphics的子类,支持更复杂的图形操作。
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// 设置抗锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// 绘制渐变矩形
GradientPaint gradient = new GradientPaint(0, 0, Color.RED,
getWidth(), getHeight(), Color.YELLOW);
g2d.setPaint(gradient);
g2d.fillRect(0, 0, getWidth(), getHeight());
// 旋转并绘制文本
g2d.rotate(Math.PI/4, 100, 100);
g2d.setColor(Color.BLACK);
g2d.drawString("Rotated Text", 50, 50);
}
图像处理
Java可以加载和显示图像文件,常用的类是Image和BufferedImage。
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
Image image = ImageIO.read(new File("image.jpg"));
g.drawImage(image, 0, 0, this);
} catch (IOException e) {
e.printStackTrace();
}
}
动画实现
通过重绘机制可以实现动画效果。通常使用Timer来控制动画帧率。
class AnimationPanel extends JPanel {
private int x = 0;
public AnimationPanel() {
Timer timer = new Timer(50, e -> {
x += 5;
if(x > getWidth()) x = 0;
repaint();
});
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, 50, 40, 40);
}
}
自定义图形
实现Shape接口可以创建自定义图形,或使用GeneralPath类构建复杂路径。
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
GeneralPath path = new GeneralPath();
path.moveTo(100, 100);
path.lineTo(150, 150);
path.lineTo(100, 200);
path.closePath();
g2d.fill(path);
}
坐标系变换
Graphics2D支持多种坐标系变换操作,包括平移、旋转和缩放。
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// 保存当前变换
AffineTransform oldTransform = g2d.getTransform();
// 应用新变换
g2d.translate(100, 100);
g2d.rotate(Math.PI/4);
g2d.scale(1.5, 1.5);
// 绘制变换后的图形
g2d.fillRect(0, 0, 50, 50);
// 恢复原始变换
g2d.setTransform(oldTransform);
}
双缓冲技术
双缓冲可以消除画面闪烁,提高绘图性能。

class DoubleBufferedPanel extends JPanel {
private BufferedImage buffer;
@Override
protected void paintComponent(Graphics g) {
if(buffer == null ||
buffer.getWidth() != getWidth() ||
buffer.getHeight() != getHeight()) {
buffer = new BufferedImage(getWidth(), getHeight(),
BufferedImage.TYPE_INT_ARGB);
}
Graphics2D g2d = buffer.createGraphics();
// 在缓冲图像上绘制
render(g2d);
g2d.dispose();
// 将缓冲图像绘制到屏幕上
g.drawImage(buffer, 0, 0, this);
}
private void render(Graphics2D g) {
// 实际绘图代码
}
}






