有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在swing中创建多维数据集?

我试图创建一个类,当被JPanel调用时,它会创建一个多维数据集。我看到的是一种叫做ColorCube的方法,它需要某种类型的“{}”和Canvas,尽管我没有发现这种方法与JPanel兼容

为了澄清,我不是问如何创建自定义JComponent(确切地说),也不是问如何添加颜色或旋转它,而是问如何创建一个类,当JPanel调用该类时,该类会将多维数据集呈现到屏幕上


共 (2) 个答案

  1. # 1 楼答案

    您真正需要的是x, y, and size传递给Cube类,然后

    • 获取这些参数,并为第一个正方形和第二个移位正方形构建一个角点数组。请参阅Cube类中的方法getCubeOnePointsgetCubeTwoPoints方法

    • 画第一个正方形。绘制第二个正方形,并连接点阵列中的点。请参阅Cube类中的drawCube方法

    • 创建传递必要参数的Cube类的实例,并绘制多维数据集。参见CubePanel构造函数和paintComponent方法

    enter image description here

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class CubePanel extends JPanel{
        private static final int D_W = 400;
        private static final int D_H = 400;
    
        Cube cube;
        public CubePanel() {
            cube = new Cube(75, 75, 200, 50);
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            cube.drawCube(g);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(D_W, D_H);
        }
    
        public class Cube {
            int x, y, size, shift;
            Point[] cubeOnePoints;
            Point[] cubeTwoPoints;
            public Cube(int x, int y, int size, int shift) {
                this.x = x;
                this.y = y;
                this.size = size;
                this.shift = shift;
                cubeOnePoints = getCubeOnePoints();
                cubeTwoPoints = getCubeTwoPoints();
            }
    
            private Point[] getCubeOnePoints() {
                Point[] points = new Point[4];
                points[0] = new Point(x, y);
                points[1] = new Point(x + size, y);
                points[2] = new Point(x + size, y + size);
                points[3] = new Point(x, y + size);
                return points;
            }
    
            private Point[] getCubeTwoPoints() {
                int newX = x + shift;
                int newY = y + shift;
                Point[] points = new Point[4];
                points[0] = new Point(newX, newY);
                points[1] = new Point(newX + size, newY);
                points[2] = new Point(newX + size, newY + size);
                points[3] = new Point(newX, newY + size);
                return points;
            }
    
            public void drawCube(Graphics g) {
                g.drawRect(x, y, size, size);
                g.drawRect(x + shift, y + shift, size, size);
                // draw connecting lines
                for (int i = 0; i < 4; i++) {
                    g.drawLine(cubeOnePoints[i].x, cubeOnePoints[i].y, 
                            cubeTwoPoints[i].x, cubeTwoPoints[i].y);
                }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new CubePanel());
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    

    更新

    "what if i wanted this in a 3d where the cube could be walked around "

    只需创建方法来移动xy并调用它,然后重新绘制。该方法可能类似于

        public void shiftLeft() {
            x -= SHIFT_INC;
            for (Point p : cubeOnePoints) {
                p.x -= SHIFT_INC;
            }
            for (Point p : cubeTwoPoints) {
                p.x -= SHIFT_INC;
            }
        }
    

    在下面的例子中,我只是用

        im.put(KeyStroke.getKeyStroke("LEFT"), "shiftLeft");
        getActionMap().put("shiftLeft", new AbstractAction(){
            public void actionPerformed(ActionEvent e) {
                cube.shiftLeft();
                repaint();
            }
        });
    

    enter image description here

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.InputMap;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.SwingUtilities;
    
    public class CubePanel extends JPanel{
        private static final int D_W = 400;
        private static final int D_H = 300;
    
        Cube cube;
        public CubePanel() {
            cube = new Cube(75, 75, 50, 15);
            InputMap im = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            im.put(KeyStroke.getKeyStroke("RIGHT"), "shiftRight");
            getActionMap().put("shiftRight", new AbstractAction(){
                public void actionPerformed(ActionEvent e) {
                    cube.shiftRight();
                    repaint();
                }
            });
            im.put(KeyStroke.getKeyStroke("LEFT"), "shiftLeft");
            getActionMap().put("shiftLeft", new AbstractAction(){
                public void actionPerformed(ActionEvent e) {
                    cube.shiftLeft();
                    repaint();
                }
            });
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            cube.drawCube(g);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(D_W, D_H);
        }
    
        public class Cube {
            private static final int SHIFT_INC = 5;
            int x, y, size, shift;
            Point[] cubeOnePoints;
            Point[] cubeTwoPoints;
            public Cube(int x, int y, int size, int shift) {
                this.x = x;
                this.y = y;
                this.size = size;
                this.shift = shift;
                cubeOnePoints = getCubeOnePoints();
                cubeTwoPoints = getCubeTwoPoints();
            }
    
            private Point[] getCubeOnePoints() {
                Point[] points = new Point[4];
                points[0] = new Point(x, y);
                points[1] = new Point(x + size, y);
                points[2] = new Point(x + size, y + size);
                points[3] = new Point(x, y + size);
                return points;
            }
    
            private Point[] getCubeTwoPoints() {
                int newX = x + shift;
                int newY = y + shift;
                Point[] points = new Point[4];
                points[0] = new Point(newX, newY);
                points[1] = new Point(newX + size, newY);
                points[2] = new Point(newX + size, newY + size);
                points[3] = new Point(newX, newY + size);
                return points;
            }
    
            public void shiftLeft() {
                x -= SHIFT_INC;
                for (Point p : cubeOnePoints) {
                    p.x -= SHIFT_INC;
                }
                for (Point p : cubeTwoPoints) {
                    p.x -= SHIFT_INC;
                }
            }
    
            public void shiftRight() {
                x += SHIFT_INC;
                for (Point p : cubeOnePoints) {
                    p.x += SHIFT_INC;
                }
                for (Point p : cubeTwoPoints) {
                    p.x += SHIFT_INC;
                }
            }
    
            public void drawCube(Graphics g) {
                g.drawRect(x, y, size, size);
                g.drawRect(x + shift, y + shift, size, size);
                // draw connecting lines
                for (int i = 0; i < 4; i++) {
                    g.drawLine(cubeOnePoints[i].x, cubeOnePoints[i].y, 
                            cubeTwoPoints[i].x, cubeTwoPoints[i].y);
                }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new CubePanel());
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    
  2. # 2 楼答案

    package Box;
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ComponentListener;
    import java.awt.event.ComponentEvent;
    public class Box2 extends JPanel
    {
    
        public Box2() 
        {
            this.addComponentListener(new ComponentListener(){
            public void componentShown(ComponentEvent arg0) {
                // TODO Auto-generated method stub
    
            }
    
            public void componentResized(ComponentEvent arg0) {
                paintComponent(getGraphics());
    
            }
    
            public void componentMoved(ComponentEvent arg0) {
                // TODO Auto-generated method stub
    
            }
    
            public void componentHidden(ComponentEvent arg0) {
                // TODO Auto-generated method stub
    
            }
            });
    
        }
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            this.setBackground(Color.white);
    
    
            Dimension d;
            d=getSize();
            int height, width;
            height =d.height;
            width=d.width;
            int w,h;
            javax.swing.border.Border linebor =BorderFactory.createLineBorder(new Color(0xAD85FF),6 );
    
            g.drawRect(0,0, w=width/2, h=height/2);
    
            g.drawRect(w/2,h/2,w/2*2,h/2*2);
    
            g.drawLine(0,0,w/2,h/2);
    
            g.drawLine(w,h,w/2+w/2*2,h/2+h/2*2);
    
            g.drawLine(w,0,w/2+w/2*2,h/2);  
    
            g.drawLine(0,h,w/2,h/2+h/2*2);  
            //g.drawLine(0, height – borderControl, width, height – borderControl);
        }
    }
    

    现在为主文件创建另一个类

    package Box;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class Box2_main extends JPanel
    {
    
    
        public static void main(String[] args)
        {
            Box2 cube = new Box2();
            JFrame frame = new JFrame("Cube2");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(cube);
            frame.setSize(500, 500);
            frame.setVisible(true);
        }
    
    }
    

    如果更改窗口的尺寸,则立方体的大小也将增大/减小