我正在尝试创建一个由JPanel
调用的类,创建一个多维数据集。我所看到的是一种称为ColorCube
的东西,它需要某种“ Universe
”和Canvas
,但我没有发现此方法与JPanel
兼容。
澄清一点,我不是问如何创建自定义JComponent
(确切地说),我也不会问如何添加颜色或旋转它,只是如何创建一个由{{1}调用的类将多维数据集渲染到屏幕上。
答案 0 :(得分:8)
你真正需要的是x, y, and size
传递给Cube
类,然后
获取这些参数并为第一个方块构建一个角点数组,并为第二个移位的方块构建角点。请参阅getCubeOnePoints
类中的方法getCubeTwoPoints
和Cube
方法。
画出第一个方块。绘制第二个方块,并连接点阵列中的点。请参阅drawCube
课程中的Cube
方法。
创建传递必要参数的Cube
类的实例,并绘制多维数据集。请参阅CubePanel
构造函数和paintComponent
方法
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);
}
});
}
}
&#34;如果我想要在3D中可以随身携带多维数据集,那该怎么办?&#34;
只需创建方法来移动所有 x
或y
并调用它,然后重新绘制。该方法可能类似于
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();
}
});
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);
}
});
}
}
答案 1 :(得分:1)
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);
}
}
如果更改窗口的尺寸,则立方体的大小也会增加/减少。