drawLine()表现

时间:2015-09-11 18:29:37

标签: java performance line

我在使用g.drawLine()时遇到了一些问题。 以下代码使用DDA算法(https://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm)

绘制一条线

在主类Starter()中,我选择行开头(10,0)和结束点(330,322)。

问题是:我需要使用类似的东西

for(int i = 1; i<=3000;i++)
        System.out.println("hi");

为了drawLine()工作。我不知道为什么,但是当我没有这个时运行我的代码时,它不起作用或者不能画出整行。完整代码如下:

MainView类:

package alexandre.VIEW;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainView {
private JFrame janela;
public JPanel panel;

public MainView()
{
    janela = new JFrame("28920");
    janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel = new JPanel();
    this.showView();
}

public void showView()
{
    janela.pack();
    janela.setLayout(null);
    janela.setSize(500, 500);
    janela.add(panel);
    panel.setBounds(0,0,500,500);
    panel.setBackground(Color.white);

    panel.setVisible(true);
    janela.setVisible(true);
}

public void plota_pixel(int x, int y)
{
    Graphics g = panel.getGraphics();


        g.drawLine(x, y, x, y);

}

}

入门级:

    package alexandre.CONTROL;

import alexandre.VIEW.MainView;

public class Starter {

public static void main(String[] args) {
    MainView view = new MainView();
    Retas retas = new Retas(view);
    retas.tracarReta(10,0,330,322);

}

}

Retas Class(绘制线条)

 package alexandre.CONTROL;

 import java.awt.Graphics;

 import alexandre.VIEW.MainView;

public class Retas {

MainView telaPrincipal;

public Retas(MainView view)
{
    this.telaPrincipal = view;
}


   public void tracarReta(int x1, int y1, int x2, int y2){



    for(int i = 1; i<=3000;i++)
        System.out.println("hi");

    int erro = 0;           
    int dx = x2-x1;
    int dy = y2-y1;
    if(dy < 0){
        x2=x2+x1;
        x1=x2-x1;
        x2=x2-x1;
        y2=y2+y1;
        y1=y2-y1;
        y2=y2-y1;
    }


    int xt=x1;
    int yt=y1;          
    dx = x2 - x1;
    dy = y2 - y1;


    if(dx >= 0 && Math.abs(dx)<Math.abs(dy))
    {
        for(int i = 1; i<=Math.abs(dy)-1; i++)
        {
            if(erro<0)
            {
                xt = xt + 1;
                yt = yt + 1;
                telaPrincipal.plota_pixel(xt, yt);
                erro = erro + dy - dx;
            }else{
                yt = yt + 1;

                telaPrincipal.plota_pixel(xt, yt);
                erro = erro - dx;
            }
        }
    }
    if(dx < 0 && Math.abs(dx)>=Math.abs(dy))
    {
        for(int i = 1; i<=Math.abs(dx)-1; i++)
        {
            if(erro < 0)
            {
                xt = xt - 1;
                telaPrincipal.plota_pixel(xt, yt);
                erro = erro + dy;
            }else{
                xt = xt - 1;
                yt = yt + 1;

                telaPrincipal.plota_pixel(xt, yt);
                erro = erro + dx + dy;
            }
        }
    }

    if(dx >= 0 && Math.abs(dx)>=Math.abs(dy))
    {

        for(int i=1; i<=Math.abs(dx)-1; i++)
        {
            if(erro <0)
            {
                xt = xt+1;

                telaPrincipal.plota_pixel(xt, yt);
                erro = erro+dy;
            }else
            {
                xt = xt+1;
                yt = yt+1;
                telaPrincipal.plota_pixel(xt, yt);
                erro = erro + dy - dx;
            }
        }
    }

    if(dx < 0 && Math.abs(dx)<Math.abs(dy))
    {
        for(int i = 1; i<=Math.abs(dy)-1; i++)
        {
            if(erro < 0)
            {
                xt = xt - 1;
                yt = yt + 1;
                telaPrincipal.plota_pixel(xt, yt);
                erro = erro + dx + dy;
            }else{
                yt = yt + 1;
                telaPrincipal.plota_pixel(xt, yt);
                erro = erro + dx;
            }
        }           
    }


}}

MainView的方法plota_pixel(x,y)在(x,y)中绘制一个点。 我知道drawLine()方法与DDA算法的做法相同,但它是一个大学&#34;家庭作业&#34;我必须逐点绘制一条线。

0 个答案:

没有答案