为什么多边形我告诉我的程序显示。这是我调用的类的代码。
package rubiksCube;
/*class that contains my paint component*/
import java.awt.*;
import javax.swing.JPanel;
public class Display extends JPanel{
Polygon[][] p = {{new Polygon(),new Polygon(),new Polygon()},
{new Polygon(),new Polygon(),new Polygon()},
{new Polygon(),new Polygon(),new Polygon()}};
int[] rx = {0,0,0,0};
int[] ry = {0,0,0,0};
int rsN = 0;
public void renderSide(int sN,int[] x,int[] y){
rsN = sN;
ry = y;
rx = x;
}
public void paintComponent(Graphics g){
g.clearRect(0, 0, 100, 100);
for(int y = 0;y<3;y++){
for(int x = 0;x<3;x++){
System.out.print(x);
System.out.println(y);
g.setColor(getColor(Main.j[rsN][(y*3)+x]));
p[y][x].addPoint((((rx[0]-rx[3])*(y/3)+rx[0])-((rx[1]-rx[2]))*(y/3)+rx[1])*(x/3)+((rx[0]-rx[3])*(y/3)+rx[0]),((((ry[0]-ry[3])*(x/3)+ry[0])-((ry[1]-ry[2]))*(x/3)+ry[1]))*(y/3)+((ry[0]-ry[3])*(x/3)+ry[0]));
p[y][x].addPoint((((rx[0]-rx[3])*(y/3)+rx[0])-((rx[1]-rx[2]))*(y/3)+rx[1])*(x+1/3)+((rx[0]-rx[3])*(y/3)+rx[0]),((((ry[0]-ry[3])*(x/3)+ry[0])-((ry[1]-ry[2]))*(x/3)+ry[1])*(y/3))+((ry[0]-ry[3])*(x/3)+ry[0]));
p[y][x].addPoint((((rx[0]-rx[3])*(y/3)+rx[0])-((rx[1]-rx[2]))*(y/3)+rx[1])*(x+1/3)+((rx[0]-rx[3])*(y/3)+rx[0]),((((ry[0]-ry[3])*(x/3)+ry[0])-((ry[1]-ry[2]))*(x/3)+ry[1]))*(y+1/3)+((ry[0]-ry[3])*(x/3)+ry[0]));
p[y][x].addPoint((((rx[0]-rx[3])*(y/3)+rx[0])-((rx[1]-rx[2]))*(y/3)+rx[1])*(x/3)+((rx[0]-rx[3])*(y/3)+rx[0]),((((ry[0]-ry[3])*(x/3)+ry[0])-((ry[1]-ry[2]))*(x/3)+ry[1]))*(y+1/3)+((ry[0]-ry[3])*(x/3)+ry[0]));
g.fillPolygon(p[y][x]);
}
}
}
public Color getColor(int i){
switch(i){
case 0:
return Color.WHITE;
case 1:
return Color.GREEN;
case 2:
return Color.RED;
case 3:
return Color.YELLOW;
case 4:
return Color.BLUE;
}
return Color.ORANGE;
}
}
//my main class
package rubiksCube;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Main {
public static int[][] j = {
{0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1},
{2,2,2,2,2,2,2,2,2},
{3,3,3,3,3,3,3,3,3},
{4,4,4,4,4,4,4,4,4},
{5,5,5,5,5,5,5,5,5}
};
public static void main(String args[]){
SimpleGui s = new SimpleGui();
Display d = new Display();
int[] x = {0,100,100,0};
int[] y = {0,0,100,100};
d.renderSide(0,x,y);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.pack();
s.setSize(1000,700);
s.setVisible(true);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.pack();
s.setSize(1000,1000);
s.setVisible(true);
}
}
//and my gui class
package rubiksCube;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleGui extends JFrame{
int[] px2;
int[] py2;
int[] pz2;
Graphics g2 = null;
int sN;
public SimpleGui(){
JButton solve = new JButton();
JPanel p = new JPanel();
solve.setText("Solve");
solve.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Control c = new Control();
c.solve();
}});
solve.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent k) {
switch (k.getKeyCode()){
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_LEFT:
break;
case KeyEvent.VK_RIGHT:
break;
case KeyEvent.VK_0:
break;
case KeyEvent.VK_1:
break;
case KeyEvent.VK_2:
break;
case KeyEvent.VK_3:
break;
case KeyEvent.VK_4:
break;
case KeyEvent.VK_5:
break;
case KeyEvent.VK_6:
break;
case KeyEvent.VK_7:
break;
case KeyEvent.VK_8:
break;
case KeyEvent.VK_9:
break;
case KeyEvent.VK_MINUS:
break;
case KeyEvent.VK_EQUALS:
break;
}
}
public void keyReleased(KeyEvent k) {
}
public void keyTyped(KeyEvent k) {
}
});
JButton random = new JButton();
random.setText("Randomize");
random.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Control c = new Control();
c.random();
}});
p.setVisible(true);
p.setLayout(new GridBagLayout());
solve.setVisible(true);
random.setVisible(true);
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.FIRST_LINE_START;
p.add(solve,c);
c.anchor = GridBagConstraints.PAGE_START;
c.gridx = 0;
c.gridy = 0;
p.add(random,c);
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.gridx = 2;
c.gridy = 2;
JPanel d = new Display();
d.setPreferredSize(new Dimension(100,100));
p.add(d,c);
add(p);
setContentPane(p);
setVisible(true);
}}
当我说g.fillPolyon(p [y] [x])时,有人可以解释一下;它不起作用? 请帮助,谢谢。 这是我的显示类的第23行。
答案 0 :(得分:2)
super.paintComponent
会导致潜在问题Display
的尺寸提示,可将其呈现为0x0
0x0
的x / y位置是组件的左上角,指定可能超出大小的X / Y位置该组件不会被涂上以下......
public static void main(String args[]) {
SimpleGui s = new SimpleGui();
Display d = new Display();
int[] x = {0, 100, 100, 0};
int[] y = {0, 0, 100, 100};
d.renderSide(0, x, y);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setSize(1000, 700);
s.setVisible(true);
}
有点没用,因为d
实际上没有被添加到可以呈现它的任何东西
<强>更新强>
您需要进一步调试代码......
我添加了一些System.out.println
语句来查看您正在创建的内容,这是输出...
---nxn
是主for-loops
。其后的每一系列数字都是Polygon
---0x0
0x0
0x0
0x0
0x0
---1x0
0x0
100x0
100x0
0x0
---2x0
0x0
200x0
200x0
0x0
---0x1
0x0
0x0
0x0
0x0
---1x1
0x0
100x0
100x0
0x0
---2x1
0x0
200x0
200x0
0x0
---0x2
0x0
0x0
0x0
0x0
---1x2
0x0
100x0
100x0
0x0
---2x2
0x0
200x0
200x0
0x0
---0x0
0x0
0x0
0x0
0x0
---1x0
0x0
100x0
100x0
0x0
---2x0
0x0
200x0
200x0
0x0
---0x1
0x0
0x0
0x0
0x0
---1x1
0x0
100x0
100x0
0x0
---2x1
0x0
200x0
200x0
0x0
---0x2
0x0
0x0
0x0
0x0
---1x2
0x0
100x0
100x0
0x0
---2x2
0x0
200x0
200x0
0x0
如果你仔细观察,你会看到你画的是直线(或空的多边形)。
您在p
中声明和使用Display
的方式也值得关注,因为您从未重置Polygon
s,这意味着,在每次重新粉饰时,您都是只需在多边形中添加更多点。最好预先计算这个并简单地绘制结果 - 恕我直言