如何在JFrame

时间:2018-02-07 03:45:10

标签: java swing curve hilbert-curve space-filling-curve

我正在尝试基于Hilbert Curve制作一个项目。我能够在Applet中使用代码,但是我需要它在JFrame中工作,因为我需要一次打开多个帧来呈现我的项目。我在下面的applet中有代码,但我不知道如何更改为JFrame。

在applet表单中,它类似于下面的代码:

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);
    }
}

}

1 个答案:

答案 0 :(得分:1)

首先,我建议您先阅读Performing Custom PaintingPainting in AWT and Swing,以便更好地了解绘画在Swing中的作用。

示例Applet实际上是一个如何执行绘制的错误示例,因为您永远不应该保留对Graphics的引用,并且应该在绘制组件时将其传递给需要它的方法。

您还应该使用NullPointerException,因为sg从未初始化。

作为一般规则,首先建立UI,然后最后调用setVisible,它将解决许多简单的问题。

HilbertCurve没有理由需要JFrame的引用,而是可以在main方法中实例化该帧。

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class HilbertCurve extends JPanel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                HilbertCurve exemplo1 = new HilbertCurve();
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(exemplo1);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private SimpleGraphics sg = null;
    private int dist0 = 256;
    private int dist = dist0;

    public HilbertCurve() {
        sg = new SimpleGraphics();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(512, 512);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        int level = 4;
        dist = dist0;
        for (int i = level; i > 0; i--) {
            dist /= 2;
        }
        sg.goToXY(dist / 2, dist / 2);
        Graphics2D g2d = (Graphics2D) g.create();
        hilbertU(g2d, level);
        g2d.dispose();

    }

    private void hilbertU(Graphics2D g, int level) {
        if (level > 0) {
            hilbertD(g, level - 1);
            sg.lineRel(g, 0, dist);
            hilbertU(g, level - 1);
            sg.lineRel(g, dist, 0);
            hilbertU(g, level - 1);
            sg.lineRel(g, 0, -dist);
            hilbertC(g, level - 1);
        }
    }

    private void hilbertD(Graphics2D g, int level) {
        if (level > 0) {
            hilbertU(g, level - 1);
            sg.lineRel(g, dist, 0);
            hilbertD(g, level - 1);
            sg.lineRel(g, 0, dist);
            hilbertD(g, level - 1);
            sg.lineRel(g, -dist, 0);
            hilbertA(g, level - 1);
        }
    }

    private void hilbertC(Graphics2D g, int level) {
        if (level > 0) {
            hilbertA(g, level - 1);
            sg.lineRel(g, -dist, 0);
            hilbertC(g, level - 1);
            sg.lineRel(g, 0, -dist);
            hilbertC(g, level - 1);
            sg.lineRel(g, dist, 0);
            hilbertU(g, level - 1);
        }
    }

    private void hilbertA(Graphics2D g, int level) {
        if (level > 0) {
            hilbertC(g, level - 1);
            sg.lineRel(g, 0, -dist);
            hilbertA(g, level - 1);
            sg.lineRel(g, -dist, 0);
            hilbertA(g, level - 1);
            sg.lineRel(g, 0, dist);
            hilbertD(g, level - 1);
        }
    }

    class SimpleGraphics {

//        private Graphics g = null;
        private int x = 0, y = 0;

        public SimpleGraphics() {
        }

        public void goToXY(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public void lineRel(Graphics2D g, int deltaX, int deltaY) {
            g.drawLine(x, y, x + deltaX, y + deltaY);
            x += deltaX;
            y += deltaY;
        }
    }
}