小程序画多条线时,只显示最后一条 [英] When drawing multiple lines in applet, it shows only the last one

查看:23
本文介绍了小程序画多条线时,只显示最后一条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Java 小程序和画布绘制多条线.我已经定义了类 Canvas:

I am trying to draw multiple lines by using Java applet and canvas. I have defined class Canvas:

 public class Canvas extends JPanel {

    private static final int RIGHT=0, LEFT=1, UP=2, DOWN=3;
    public static final int WIDTH=600, HEIGHT=500;
    private int direction = 0 ;                          
    private int pixels;                             

    /**
     *  Canvas() constructor sets its size
     */
    public Canvas() {
        setSize(WIDTH, HEIGHT);
    }

    public void setPatt(int pat, int lev) {
        direction = pat;
        pixels = lev;
    }

    public void paintComponent(Graphics g) {
        g.setColor(getForeground());
        switch (direction) {
        case LEFT:  
            drawLineLeft(g, pixels); 
            break;
        case RIGHT:
            drawLineRight(g, pixels);
            break;
        case UP:
            drawLineUp(g, pixels);
            break;
        case DOWN:
            drawLineDown(g, pixels);
            break;
        } 
    } 

    private void drawLineLeft(Graphics g, int pix){

        if(pix > 0){
            Dimension d = getSize();
            int x = d.width/2;
            int y = d.height/2;
           g.drawLine(x, y, x-10*pix, y);//left
        }
    }

    private void drawLineUp(Graphics g, int pix){
        if(pix > 0){
            Dimension d = getSize();
            int x = d.width/2;
            int y = d.height/2;
            g.drawLine(x, y, x, y-10*pix);//up
        }
    }

    private void drawLineRight(Graphics g, int pix){
        //Graphics2D g2 = (Graphics2D) g;
        if(pix > 0){
            Dimension d = getSize();
            int x = d.width/2;
            int y = d.height/2;

            g.drawLine(x, y, x+10*pix, y);//right
        }
    }

    private void drawLineDown(Graphics g, int pix){
        if(pix > 0){
            Dimension d = getSize();
            int x = d.width/2;
            int y = d.height/2;
            g.drawLine(x, y, x, y+10*pix);// down
        }
    }
} 

我还有另一个类,我在其中定义了下拉列表、文本字段和按钮.我可以从下拉列表中选择应该绘制线条的方向(右、左、上、下 - 方向),我可以通过在文本字段中输入数字(像素)来定义线条的长度.当按钮被按下时,方法 setPatt 被调用并在画布中出现一行:

I also have another class where I have defined drop down list, text field and button. I can choose direction from drop down list in which line should be painted (RIGHT, LEFT, UP, DOWN - direction) and I can define how long the line will be by typing a number in text field (pixels). When the button is pushed, method setPatt is called and line appears in canvas:

public class TurtleApplet extends JApplet implements ActionListener
{
    .....
    .....
    .....

    public void actionPerformed( ActionEvent e) 
    {
       if(e.getSource() == drawButton){
            int y = Integer.parseInt(pixels.getText());
            canvas.setPatt(direction.getSelectedIndex(), Integer.parseInt(pixels.getText()));

        }
       //repaint();
    }  
    .....
}

问题是,当我按下按钮时,只有当我调整小程序窗口大小时,画布中才会出现新行.我尝试添加 repaint() 方法,这有帮助,但仍然存在另一个问题 - 如何在不丢失前一行的情况下绘制新线?

The problem is that when I push the button new line appears in canvas only then when I resize applet window. I tried to add repaint() method, this helped, but still there is another problem - how to make draw new line without losing the previous one?

只有最后一行在屏幕上可见,例如,当我从中心向左画一条线(线总是从画布中心开始)然后我想从右边画一条新线居中,第一行消失,我只能看到第二行,依此类推.

Only the last line is visible on the screen, for example, when I draw a line to the left from center (line always starts from the center of canvas) and then I want to draw a new line to the right from the center, the line first line disappears and I can see only the second one and so on.

推荐答案

您应该跟踪已绘制的线条.paintComponent 方法应该打印(或重新打印)整个组件.据我所知,你总是只打印 1 行,因此你失去了以前的状态.

You should keep track of lines drawn already. The paintComponent method supposed to print (or reprint) the entire component. As far as I see you always print just 1 line, therefore you loose the previous state.

尝试创建一个 Map,在其中存储线数据并在 paintComponent 方法中迭代以绘制所有线.

Try to create a Map<Integer, Integer> where you store the line data and iterate through in the paintComponent method to draw all lines.

方法 setPatt 可以改为调用 addPatt.

The method setPatt may be called addPatt instead.

PS:如果顺序很重要,试试LinkedHashMap

PS: If the order matters, try LinkedHashMap

这篇关于小程序画多条线时,只显示最后一条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆