简体   繁体   English

调整大小或移动滚动条时,线条和椭圆形消失

[英]My lines and ovals disappear when I resize or move scrollbars

i have aj Panel in a jSrollbarPane in netbeans design mode. 我在netbeans设计模式下的jSrollbarPane中有一个j面板。 I want to paint permanently on it untill user presses the button "clear". 我想在其上永久涂漆,直到用户按下“清除”按钮。 My lines and ovals created in path() disappear when I resize or move scrollbars on UI. 在UI上调整大小或移动滚动条时,在path()中创建的线条和椭圆形消失。

My code segment is below, thank you in advance 我的代码段如下,在此先感谢您

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) { 
//i create some public double arrays here like x[] and y[]
}

 private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      

        path(x, y, 0);
    }    
 public void path(double[] X, double[] Y, int type) {
        Graphics2D gfx = (Graphics2D) jPanel1.getGraphics();
        int xT, yT, xL, yL;
        getContentPane();
        scale = jSlider1.getValue();
        switch (type) {
            case 0:
                gfx.setStroke(new BasicStroke(3));
                break;
            case 1:
                gfx.setStroke(new BasicStroke(1));
                gfx.setPaint(Color.blue);
                break;
            case 2:
                gfx.setStroke(new BasicStroke(1));
                gfx.setPaint(Color.green);
                break;
            case 3:
                gfx.setStroke(new BasicStroke(1));
                gfx.setPaint(Color.red);
                break;
            default:
                gfx.setStroke(new BasicStroke(1));
                gfx.setPaint(Color.yellow);
                break;
        }

        for (int l = 1; l < size; l++) {
            xT = (int) (scale * X[l - 1]);
            yT = (int) (scale * Y[l - 1]);
            xL = (int) (scale * X[l]);
            yL = (int) (scale * Y[l]);

            gfx.drawOval(xT, yT, 5, 5);
            gfx.drawLine(xT, yT, xL, yL);

        }
    } 

As soon as I saw your title, I knew that you were drawing using a Graphics object obtained via Component#getGraphics() . 一看到您的标题,我便知道您正在使用通过Component#getGraphics()获得的Graphics对象进行绘图。 Don't do this. 不要这样

You should not draw with a Graphics object obtained by calling getGraphics() on a component. 您不应使用通过在组件上调用getGraphics()获得的Graphics对象进行绘制。 This will return a Graphics object that is short lived, risking disappearing graphics or worse, a NullPointerException. 这将返回一个短暂存在的Graphics对象,可能会导致图形消失甚至更糟的是NullPointerException。 Instead, draw in the JPanel's paintComponent(...) method either directly, or indirectly by drawing on a BufferedImage (yes, you can get its Graphics object via getGraphics() ) and then drawing the BufferedImage to the GUI within the paintComponent method. 取而代之的是,直接或通过在BufferedImage上进行绘制来间接或间接地绘制JPanel的paintComponent(...)方法(是的,您可以通过getGraphics()获得其Graphics对象),然后在paintComponent方法中将BufferedImage绘制到GUI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 状态栏,如何使我的线条,椭圆和rects可见? - Status bar , how to make my lines,ovals and rects visible? 当滚动条出现时,JScrollPane会调整包含JPanel的大小 - JScrollPane resize containing JPanel when scrollbars appear JButton仅在我将鼠标悬停在它们上方时才会显示。 当我调整窗口大小时也消失 - JButtons only show up when I hover over them. Also disappear when I resize window 尝试使用线条和椭圆在 Java 中绘制圆柱体 - Trying to draw a cylinder in Java using lines and ovals 我的UI闪烁,每当我尝试动态调整imageview的大小时,图像就会消失 - My UI is blinking and whenever i try to resize the imageview dynamically, the image disappear 对象在调整大小时以及窗口最小化时消失 - Objects disappear on resize and when the window is minimized 为什么当我开始一项新活动时我的背景消失了? - why does my background disappear when i start a new activity? 没有互联网时,如何使我的按钮消失? - How can I make my button in disappear when there's no internet? 按下“ S”后,如何使两个椭圆形跟随并环绕另一个椭圆形? - How can i make 2 Ovals follow and circle around another Oval when 'S' is pressed? Java重绘问题-椭圆形移动 - Java repaint issue-seeing ovals each move
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM