簡體   English   中英

形狀不是實時繪制的

[英]Shapes are not drawn in real time

所以,如果我嘗試移動形狀,它實際上會移動,但沒有運動 animation。為了繪制移動的形狀,必須最小化或最大化應用程序 window。

這是 PentaminoShape class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class PentominoShape extends JFrame implements MouseListener, MouseMotionListener {
    JPanel shapePane;
    Container contentPane;
    private Polygon currPolygon;
    private int x, y;
    ArrayList<Polygon> polygons = new ArrayList<Polygon>();
    JFrame frame;
    public PentominoShape(JFrame frame){
        this.frame = frame;
        initShape();
    }
    private void initShape() {
        Polygon fig1 = new Polygon(new int[]{10, 50, 50, 10}, new int[]{10, 10, 200, 200}, 4);
        Polygon fig2 = new Polygon(new int[]{130, 210, 210, 170, 170, 130, 130, 90, 90, 130}, new int[]{80, 80, 120, 120, 200, 200, 160, 160, 120, 120}, 10);
        Polygon fig3 = new Polygon(new int[]{290, 330, 330, 250, 250, 290}, new int[]{50, 50, 200, 200, 160, 160}, 6);
        Polygon fig4 = new Polygon(new int[]{10, 90, 90, 50, 50, 10}, new int[]{280, 280, 400, 400, 360, 360}, 6);
        Polygon fig5 = new Polygon(new int[]{170, 210, 210, 170, 170, 130, 130, 170}, new int[]{240, 240, 360, 360, 400, 400, 320, 320}, 8);
        Polygon fig6 = new Polygon(new int[]{250, 370, 370, 330, 330, 290, 290, 250}, new int[]{280, 280, 320, 320, 400, 400, 320, 320}, 8);
        Polygon fig7 = new Polygon(new int[]{10, 50, 50, 90, 90, 130, 130, 10}, new int[]{480, 480, 520, 520, 480, 480, 560, 560}, 8);
        Polygon fig8 = new Polygon(new int[]{170, 250, 250, 290, 290, 170}, new int[]{520, 520, 440, 440, 560, 560}, 6);
        Polygon fig9 = new Polygon(new int[]{330, 370, 370, 410, 410, 450, 450, 410, 410, 330}, new int[]{520, 520, 480, 480, 440, 440, 520, 520, 560, 560}, 10);
        Polygon fig10 = new Polygon(new int[]{10, 50, 50, 90, 90, 130, 130, 90, 90, 50, 50, 10}, new int[]{680, 680, 640, 640, 680, 680, 720, 720, 760, 760, 720, 720}, 12);
        Polygon fig11 = new Polygon(new int[]{170, 210, 210, 250, 250, 210, 210, 170}, new int[]{640, 640, 600, 600, 760, 760, 680, 680}, 8);
        Polygon fig12 = new Polygon(new int[]{330, 410, 410, 370, 370, 290, 290, 330}, new int[]{640, 640, 680, 680, 760, 760, 720, 720}, 8);

        polygons.add(fig1);
        polygons.add(fig2);
        polygons.add(fig3);
        polygons.add(fig4);
        polygons.add(fig5);
        polygons.add(fig6);
        polygons.add(fig7);
        polygons.add(fig8);
        polygons.add(fig9);
        polygons.add(fig10);
        polygons.add(fig11);
        polygons.add(fig12);

        Color[] c = new Color[12];
        c[0] = new Color(25, 165, 25);
        c[1] = new Color(255, 165, 25);
        c[2] = new Color(255, 50, 50);
        c[3] = new Color(150, 45, 90);
        c[4] = new Color(25, 165, 150);
        c[5] = new Color(25, 165, 255);
        c[6] = new Color(255, 40, 190);
        c[7] = new Color(180, 90, 60);
        c[8] = new Color(90, 80, 70);
        c[9] = new Color(70, 80, 90);
        c[10] = new Color(150, 30, 20);
        c[11] = new Color(80, 80, 80);

        shapePane = new JPanel(){
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;

                g2.setColor(c[0]); g2.fill(fig1);
                g2.setColor(c[1]); g2.fill(fig2);
                g2.setColor(c[2]); g2.fill(fig3);
                g2.setColor(c[3]); g2.fill(fig4);
                g2.setColor(c[4]); g2.fill(fig5);
                g2.setColor(c[5]); g2.fill(fig6);
                g2.setColor(c[6]); g2.fill(fig7);
                g2.setColor(c[7]); g2.fill(fig8);
                g2.setColor(c[8]); g2.fill(fig9);
                g2.setColor(c[9]); g2.fill(fig10);
                g2.setColor(c[10]); g2.fill(fig11);
                g2.setColor(c[11]); g2.fill(fig12);
            }
        };
        /*contentPane = this.getContentPane();
        contentPane.add(shapePane);
        this.pack();*/

        frame.add(shapePane);

        shapePane.addMouseListener(this);
        shapePane.addMouseMotionListener(this);

        /*shapePane.addMouseListener(this);
        shapePane.addMouseMotionListener(this);*/
    }
    public void mousePressed(MouseEvent e) {
        for(Polygon polygon: polygons) {
            if (polygon.contains(e.getPoint())) {
                System.out.println("Pressed");
                currPolygon = polygon;
                x = e.getX();
                y = e.getY();
            }
        }
    }
    public void mouseDragged(MouseEvent e) {
        try {
            if (currPolygon.contains(x, y)) {
                System.out.println("Dragged");
                int dx = e.getX() - x;
                int dy = e.getY() - y;
                currPolygon.translate(dx, dy);
                x += dx;
                y += dy;
                repaint();
            }
        }catch (NullPointerException ex){

        }
    }
    public void mouseReleased(MouseEvent e){
        currPolygon = null;
    }
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mouseMoved(MouseEvent e){}

}

和主要的 Pentamino class:

import javax.swing.*;

public class Pentomino extends JFrame {
    JFrame frame;
    PentominoShape shape;
    PentominoPanel panel;
    public Pentomino(){
        initUI();
    }

    private void initUI(){
        frame = new JFrame("Пентамино");
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(1500, 900);
        setResizable(false);

        shape = new PentominoShape(frame);
        panel = new PentominoPanel(frame);

        /*frame.add(shape);
        frame.add(panel);*/

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Pentomino game = new Pentomino();
    }
}

我是 Java 的新手,我不明白如何解決這個問題。 嘗試在 inte.net 上搜索類似問題,但沒有找到任何內容。

你的錯誤在這里:

public void mouseDragged(MouseEvent e) {
    try {
        if (currPolygon.contains(x, y)) {
            System.out.println("Dragged");
            int dx = e.getX() - x;
            int dy = e.getY() - y;
            currPolygon.translate(dx, dy);
            x += dx;
            y += dy;
            repaint(); // ***** here ****
            
        }
    }catch (NullPointerException ex){

    }
}

您在封閉的 class 上調用repaint() ,這是一個 JFrame,它從未顯示過,因此不會達到預期的效果。

事實上,問問你自己,為什么這...

public class PentominoShape extends JFrame // ...

為什么 PentominoShape 完全擴展 JFrame,當它不表現為 JFrame,而不應該表現為 JFrame 時?

相反,在保存形狀的 JPanel 上調用 repaint,shapePane JPanel,是的,擺脫捕獲 NullPointerException。 那永遠不應該出現在您的代碼中:

public void mouseDragged(MouseEvent e) {
    if (currPolygon == null) {
        return;
    }
    if (currPolygon.contains(x, y)) {
        System.out.println("Dragged");
        int dx = e.getX() - x;
        int dy = e.getY() - y;
        currPolygon.translate(dx, dy);
        x += dx;
        y += dy;
        shapePane.repaint(); // now we're repainting the correct JPanel!
    }
}

旁注:我會清理一些東西,包括

  1. 如果可能,沒有任何 class 擴展 JFrame
  2. 創建我自己的多邊形類型 class:
public class PentominoShape2 {
    private Polygon polygon;
    private Color color;

    public PentominoShape2(Polygon polygon, Color color) {
        this.polygon = polygon;
        this.color = color;
    }

    public void draw(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(color);
        g2.fill(polygon);
    }

    public Polygon getPolygon() {
        return polygon;
    }

    public Color getColor() {
        return color;
    }

    public boolean contains(Point p) {
        return polygon.contains(p);
    }

}

然后在繪圖JPanel

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (PentominoShape2 poly : polys) {
            poly.draw(g);
        }
    }

鼠標監聽器也一樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM