使用Netbeans时在JAVA中重绘/刷新 [英] Redraw/Refresh in JAVA when using Netbeans

查看:98
本文介绍了使用Netbeans时在JAVA中重绘/刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我对Java和Netbeans比较陌生,所以请光临我。我正在尝试在jPanel上绘制2D图形,我很难让它正常工作。我不确定我是否遵循了正确的程序,或者我只是错过了一些愚蠢的东西。



似乎我的图形被删除了它被调整大小的时间或者像菜单栏一样出现在它上面的东西。每次发生事情时我都尽力重新绘制,但现在我已经走到了尽头。因为现在如果出现菜单会删除图像,如果我在菜单处于活动状态时重新绘制图像,则会删除菜单。



当我使用控件调整表单大小时停靠的东西非常有趣。我把一个事件监听,如果jScrollPane调整大小,它会将2D图形重新应用到jPanel。它有时会这样做,有时不会。



Hi,

I'm rather new to Java and Netbeans so please bare with me. I'm trying to draw 2D graphics on a jPanel and I am having a hard time getting it to work right. I'm not sure if I'm following the correct procedure for this or if I'm just missing something silly.

It seems like my graphic is being erased each time it is resized or if something appears on top of it like a menu bar. I have tried my best to have this redraw each time something happens but now I am at a dead end. Because now if a menu appears it erases the image, if I redraw the image while the menu is active it erases the menu.

When I resize the form with the controls docked something very interesting happens. I put an event that listens if the jScrollPane resizes, which reapplies the 2D graphics to the jPanel. It sometimes does it, sometimes not.

package my.SnapToGrid;

import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.InputEvent;

public class SnapGrid extends javax.swing.JFrame {

     int ViewPortWidth = 1000;//viewport width
     int ViewPortHeight = 2000;//viewport Height
     Color ViewPortBackgroundColor = Color.white;
     Color ViewPortGridColor = Color.darkGray;
     Color ViewPortSnapColor = Color.BLUE;
     int GridWidth = 30;//grid width
     int GridHeight = 30;//grid heiGridHeightt
     int npx, npy;//current snap coords
     BufferedImage buf;//double buffer
     
    public SnapGrid() {
        buf = new BufferedImage(ViewPortWidth, ViewPortHeight, BufferedImage.TYPE_INT_RGB);
        initComponents();
        jplGraphic.setBackground(ViewPortBackgroundColor);
        jplGraphic.setPreferredSize(new Dimension(ViewPortWidth,ViewPortHeight));
    }                  

    // Calculate the snap position and Redraw and Reapply the 2D Graphics
    private void jplGraphicMouseMoved(java.awt.event.MouseEvent evt) {                                      
                    int x = evt.getX(), y = evt.getY();
                    int mx = x % GridWidth, my = y % GridHeight;

                    if (mx<GridWidth/2) npx = x - mx;
                    else npx = x + (GridWidth-mx);

                    if (my<GridHeight/2) npy = y - my;
                    else npy = y + (GridHeight-my);

                    status.setText(npx+", "+npy);
                    GenerateDrawing();
                    UpdateDrawing();
    }                                     

    // Reapply the 2D Graphic when it is moved
    private void jplGraphicComponentMoved(java.awt.event.ComponentEvent evt) {                                          
        UpdateDrawing();
    }                                         
    
    // Draw the 2D Graphic and apply it
    private void formComponentShown(java.awt.event.ComponentEvent evt) {                                    
        GenerateDrawing();
        UpdateDrawing();
    }                                   

     // Reapply the 2D Graphics when resized
    private void jplGraphicComponentResized(java.awt.event.ComponentEvent evt) {                                            
        UpdateDrawing();
    }                                           

    // Scroll and Move the ViewPort and Reapply the 2D Graphics
    private void jplGraphicMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {                                           
        // Are just the CTRL switches left on
        if(evt.getModifiers() == InputEvent.CTRL_MASK) {    // Zoom Functionality
            java.awt.Toolkit.getDefaultToolkit().beep();
        }
        else{   // Scroll Functionality      
            Point ViewPortPosition = jscpViewPort.getViewport().getViewPosition();
            int UnitsToScroll = evt.getUnitsToScroll();
            jscpViewPort.getVerticalScrollBar().getHeight();
            if(evt.getModifiers() == InputEvent.SHIFT_MASK) {   // Scroll Horizontally
                ViewPortPosition.x += UnitsToScroll;
                if(ViewPortPosition.x < 0)ViewPortPosition.x = 0;
                int WidthLimit = ViewPortWidth-jscpViewPort.getHorizontalScrollBar().getWidth();
                if(ViewPortPosition.x > WidthLimit)ViewPortPosition.x = WidthLimit;
            }
            else{   // Scroll Vertically
                ViewPortPosition.y += UnitsToScroll;
                if(ViewPortPosition.y < 0)ViewPortPosition.y = 0;
                int HeightLimit = ViewPortHeight-jscpViewPort.getVerticalScrollBar().getHeight();
                if(ViewPortPosition.y > HeightLimit)ViewPortPosition.y = HeightLimit;
            }
            jscpViewPort.getViewport().setViewPosition(ViewPortPosition);
            System.out.println("("+ViewPortPosition.x+", " +ViewPortPosition.y+")");
            System.out.println(jscpViewPort.getHorizontalScrollBar().getWidth());
        }
    }                                          

    // Reapply the 2D Graphics when resized
    private void jscpViewPortComponentResized(java.awt.event.ComponentEvent evt) {                                              
        UpdateDrawing();
    }                                             

    // Draws the 2D Graphics and stores it in memory
    private void GenerateDrawing(){
               Graphics2D g = (Graphics2D)buf.createGraphics();
        g.setBackground(ViewPortBackgroundColor);   //Set Img Background
        
        g.clearRect(0, 0, ViewPortWidth, ViewPortHeight);   //Erase Everything
        
        //Draw Grid Markers
        g.setColor(ViewPortGridColor);  //Set Grid Marker Color
        for (int j=GridHeight;j<ViewPortHeight;j+=GridHeight) {
            for (int i=GridWidth;i<ViewPortWidth;i+=GridWidth) {
                g.drawLine(i, j, i, j);
            }
        }
        
        //Draw Snapped Point
        g.setColor(ViewPortSnapColor);
        if (npx>=0 && npy>=0 && npx<=ViewPortWidth && npy<=ViewPortHeight) {
             g.drawOval(npx-4, npy-4, 8, 8);
        }
    }
    
    // Applies the 2D graphics to the jPanel
    private void UpdateDrawing(){
        jplGraphic.getGraphics().drawImage(buf, 0, 0, Color.BLACK, null);
    }

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SnapGrid().setVisible(true);
            }
        });
    }
                  
}





我没有包含所有NetBeans生成的代码,因为它太长了。



I didn't include all of the NetBeans generated code because it was simply too long.

推荐答案

经过几个小时的搜索和尝试各种示例后,我终于发现我的问题是我试图重绘整个用户界面来重绘我的图形,或独立重绘我的图形导致其他奇怪的问题。问题,我认为我正在产生不必要的解决方法。



最后修复它的是创建一个JPanel的扩展类,它在JPanel上绘制/显示图形,以及然后仅在组件应该重绘时重绘图形。这解决了滚动,调整大小,移动,菜单等所有问题。它显然也使用了更少的资源。



After many hours of searching and trying various examples I finally figured out my problem was that I was attempting to redraw the entire UI to redraw my graphic, or redrawing my graphic independently causing other weird problems. Problems that I thought I was generating unnecessary workarounds.

What fixed it at the end was creating an extending class of JPanel that draws/displays the graphic on a JPanel, and then redraws the graphic only when the component is supposed to redraw. This fixed all problems with scrolling, resizing, moving, menus etc. It also obviously uses less resources.

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    doDrawing(g);
}


这篇关于使用Netbeans时在JAVA中重绘/刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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