Java图形用户界面与输入输出编程指南
立即解锁
发布时间: 2025-08-18 00:35:33 阅读量: 1 订阅数: 2 


Java编程基础:科学家与工程师的必备技能
# Java图形用户界面与输入输出编程指南
## 1. 图形用户界面基础
在Java中,图形用户界面(GUI)的实现可以借助AWT或Swing。GUI程序是事件驱动的,即用户在界面上的任何操作(如点击按钮)都可视为一个事件。Swing中用于在屏幕上实现独立窗口的类是JFrame。
### 1.1 组件类型
主要有三种组件类型:
- **顶级容器**:例如JFrame,它是界面的最外层容器,相当于一个窗口。
- **中间容器**:如JPanel,可用于组织和布局其他组件。
- **原子组件**:像JButton,是最基本的交互元素。
### 1.2 事件监听与布局管理
为了让界面响应用户事件,需要为相关组件编写监听器。组件的布局可以使用布局管理器,常见的布局管理器包括BorderLayout、FlowLayout和GridLayout。
### 1.3 绘图操作
绘图通常在扩展的JPanel对象上进行,需要重写paintComponent方法来实现具体的绘图操作。Graphics类提供了各种绘制形状的方法。
## 2. 绘制数学图形
绘制数学图形时,会面临将自然的世界坐标转换为绝对像素坐标的挑战。因为屏幕上的垂直像素坐标与世界坐标系统中的y坐标方向相反,所以需要一个类来完成这种转换。
### 2.1 MyWorld类
MyWorld类可用于将自然的世界坐标转换为绝对像素坐标,其代码如下:
```java
public class MyWorld
//transforms world coordinates into pixel coordinates
{
private double xmin;
private double xmax;
private double ymin;
private double ymax;
private int xrange;
private int yrange;
private int topborder;
private int leftborder;
public MyWorld( double xleft, double xright, double ydown,
double yup, int width, int height )
{
xmin = xleft; xmax = xright; ymin = ydown; ymax = yup;
xrange = width; yrange = height;
topborder = 50; leftborder = 50;
}
public int xp( double xworld )
{
return (int) Math.round(leftborder + xrange * (xworld - xmin)
/ (xmax - xmin));
}
public int yp( double yworld )
{
return (int) Math.round(topborder + yrange * (yworld - ymax)
/ (ymin - ymax));
}
}
```
该类的构造函数创建了一个世界坐标的“视图对象”,而xp和yp方法分别将x和y的世界坐标参数转换为绝对的水平和垂直像素坐标。
### 2.2 MyGraph程序
下面的MyGraph程序使用MyWorld类绘制阻尼振荡函数 $y = e^{-0.1x} \sin x$ 的图形,同时绘制x轴和y轴:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyGraph extends JFrame
{
DrawPanel drawArea = new DrawPanel();
int winWidth;
int winHeight;
public MyGraph()
{
setTitle("Mathematical graphs");
getContentPane().add(drawArea);
drawArea.setBackground(new Color(0,255,0));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
class DrawPanel extends JPanel
{
public DrawPanel()
{
winWidth = 400;
winHeight = 400;
setPreferredSize (new Dimension(winWidth,winHeight));
}
protected void paintComponent (Graphics g)
{
super.paintComponent (g);
double xmin = -2*Math.PI;
double xmax = 8*Math.PI;
double ymin = -2;
double ymax = 2;
double x, y, xold, yold;
MyWorld w2p = new MyWorld( xmin, xmax, ymin, ymax, 300, 300 );
g.drawString( "Damped oscillations", w2p.xp(Math.PI/2),
w2p.yp(ymax/2) );
g.drawLine( w2p.xp(xmin), w2p.yp(0),
w2p.xp(xmax), w2p.yp(0) ); // x-axis
g.drawLine( w2p.xp(0), w2p.yp(ymin),
w2p.xp(0), w2p.yp(ymax) ); // y-axis
xold = 0; yold = 0;
for (x = 0; x < 8*Math.PI; x = x+Math.PI/40)
{
y = Math.exp( -0.1*x) * Math.sin(x);
g.drawLine( w2p.xp(xold), w2p.yp(yold),
w2p.xp(x), w2p.yp(y) );
xold = x;
yold = y;
}
}
}
public static void main(String[] args)
{
MyGraph d = new MyGraph();
d.setVisible(true);
}
}
```
此程序的执行流程如下:
```mermaid
graph TD
A[创建MyGraph对象] --> B[设置窗口标题]
B --> C[添加绘图面板]
C --> D[设置面板背景颜色]
D --> E[设置关闭操作]
E --> F[调整窗口大小]
F --> G[显示窗口]
G --> H[绘制图形]
```
### 2.3 绘制流程分析
- 创建MyGraph对象时,会设置窗口标题、添加绘图面板、设置背景颜色和关闭操作,并调整窗口大小。
- 在DrawPanel的paintComponent方法中,首先计算世界坐标范围,创建MyWorld对象。
- 绘制坐标轴和图形标题。
- 通过循环计算函数值,并使用drawLine方法连接相邻的点,从而绘制出函数图形。
## 3. 分形图形绘制
分形图形近年来备受关注,其中Julia集和Mandelbrot集是比较著名的分形。
### 3.1 Julia集
Julia集是复多项式 $z^2 - \mu$ 的一个集合,其中 $z$ 是复变量,$\mu$ 是复常数。以下是绘制Julia集的简单程序:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JuliaSet extends JFrame
{
DrawPanel drawArea = new DrawPanel();
int winWidth;
int winHeight;
public JuliaSet()
{
setTitle("Julia Set");
getContentPane().add(drawArea);
drawArea.setBackground(new Color(0,255,0));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
class DrawPanel extends JPanel
{
public DrawPanel()
{
winWidth = 800;
winHeight = 600;
setPreferredSize (new Dimension(winWidth,winHeight));
}
protected void paintComponent (Graphics g)
{
super.paintComponent (g);
int maxIts = 40;
// max number of iterations
double r = 10;
// infinity
double
a, b;
// real and imag parts of mu
double xmin, xmax, ymin, ymax; // range of world coords
double x, y, x0, y0, newX, newY; // world coords
int n;
// iteration counter
int xp, yp;
// pixel coords
int maxX, maxY;
// pixel range
int R, G, B;
// RGB parameters
int topBorder = 50;
// dont draw on the menubar!
xmin = -2;
xmax = 2;
ymin = -1;
ymax = 1;
maxX = 790;
maxY = 500;
a = 1.25;
b = 0;
for (xp = 0; xp <= maxX; xp++)
for (yp = 0; yp <= maxY; yp++)
{
x0 = (xmax - xmin) * xp / maxX + xmin;
y0 = (ymin - ymax) * yp / maxY + ymax;
x = x0;
y = y0;
n = 0;
while (n < maxIts && x*x + y*y <= r)
{
n++;
newX = x*x - y*y - a;
newY = 2*x*y - b;
x = newX;
y = newY;
}
if (x*x + y*y > r)
{
n = n %7 + 1;
B = n % 2;
if (n <=3)
R = 0;
else
R = 1;
if (n == 2 | n == 3 | n == 6 | n == 7)
G = 1;
else
G = 0;
g.setColor( new Color(255*R, 255*G, 255*B) );
g.drawLine( xp, yp+topBorder, xp, yp+topBorder );
}
else
{
// black inside
```
0
0
复制全文
相关推荐










