我正在尝试基于希尔伯特曲线制作一个项目。我能够在 Applet 中使用代码,但我需要它在 JFrame 中工作,因为我需要一次打开多个框架来展示我的项目。我在下面的小程序中有代码,但我不知道如何更改为 JFrame。
在小程序形式中,它就像下面的代码:
import java.awt.*;
import java.applet.*;
public class HilbertCurve extends Applet{
private SimpleGraphics sg = null;
private int dist0=256;
private int dist=dist0;
public void init(){
resize(dist0, dist0);
sg = new SimpleGraphics(getGraphics());}
public void paint(Graphics g){
int level = 4;
dist = dist0;
for(int i=level; i>0; i--) dist/=2;
sg.goToXY (dist/2, dist/2);
HilbertU(level);
}
private void HilbertU(int level){
if(level>0){
HilbertD(level-1); sg.lineRel(0,dist);
HilbertU(level-1); sg.lineRel(dist,0);
HilbertU(level-1); sg.lineRel(0,-dist);
HilbertC(level-1);
}
}
private void HilbertD(int level){
if(level>0){
HilbertU(level-1); sg.lineRel(dist,0);
HilbertD(level-1); sg.lineRel(0,dist);
HilbertD(level-1); sg.lineRel(-dist,0);
HilbertA(level-1);
}
}
private void HilbertC(int level){
if(level>0){
HilbertA(level-1); sg.lineRel(-dist,0);
HilbertC(level-1); sg.lineRel(0,-dist);
HilbertC(level-1); sg.lineRel(dist,0);
HilbertU(level-1);
}
}
private void HilbertA(int level){
if(level>0){
HilbertC(level-1); sg.lineRel(0,-dist);
HilbertA(level-1); sg.lineRel(-dist,0);
HilbertA(level-1); sg.lineRel(0,dist);
HilbertD(level-1);
}
}
import java.awt.*;
import javax.swing.*;
class SimpleGraphics{
private Graphics g = null;
private int x =0, y = 0;
public SimpleGraphics(Graphics g) {
this.g = g;}
public void goToXY(int x, int y){
this.x =x;
this.y= y;
}
public void lineRel(int deltaX, int deltaY){
g.drawLine(x, y, x+deltaX, y+deltaY);
x+=deltaX;
y+=deltaY;}
}
我试着自己把它放到 JFrame 中,但我做不到。JFrame 打开,但 HilbertCurve 没有启动。下面是我的代码,我没有更改 SimpleGraphics 类
import java.awt.*;
import javax.swing.*;
public class HilbertCurve extends JPanel {
JFrame frame;
private SimpleGraphics sg = null;
private int dist0=256;
private int dist=dist0;
public static void main(String[] args){
HilbertCurve exemplo1 = new HilbertCurve();
exemplo1.fireUpScreen();
}
public void fireUpScreen(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(512,512);
frame.setVisible(true);
frame.add(this);
}
public void paint(Graphics g){
int level = 4;
dist = dist0;
for(int i=level; i>0; i--) dist/=2;
sg.goToXY (dist/2, dist/2);
HilbertU(level);
}
private void HilbertU(int level){
if(level>0){
HilbertD(level-1); sg.lineRel(0,dist);
HilbertU(level-1); sg.lineRel(dist,0);
HilbertU(level-1); sg.lineRel(0,-dist);
HilbertC(level-1);
}
}
private void HilbertD(int level){
if(level>0){
HilbertU(level-1); sg.lineRel(dist,0);
HilbertD(level-1); sg.lineRel(0,dist);
HilbertD(level-1); sg.lineRel(-dist,0);
HilbertA(level-1);
}
}
private void HilbertC(int level){
if(level>0){
HilbertA(level-1); sg.lineRel(-dist,0);
HilbertC(level-1); sg.lineRel(0,-dist);
HilbertC(level-1); sg.lineRel(dist,0);
HilbertU(level-1);
}
}
private void HilbertA(int level){
if(level>0){
HilbertC(level-1); sg.lineRel(0,-dist);
HilbertA(level-1); sg.lineRel(-dist,0);
HilbertA(level-1); sg.lineRel(0,dist);
HilbertD(level-1);
}
}
}