这个想法是排列拼图在圆环表面。我设置它是为了你按Q,W,E,D移动棋子。我遇到的问题是,当你组合水平和垂直移动时,数字(代表颜色)重叠。后来我想要有1个和2个颜色的部分并使其更大但是现在我需要更改代码以使其更简单并使其正常工作。任何建议或评论都会有所帮助。谢谢。
package project;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
//KeyListener is use with keyboard
public class main extends JPanel implements ActionListener, KeyListener
{
Timer tm = new Timer(1, this); //for animation
int a1 = 100, b1 = 100; //(a,b)
int a2 = 200, b2 = 100;
int a3 = 300, b3 = 100;
int a4 = 400, b4 = 100;
int a5 = 100, b5 = 200;
int a6 = 200, b6 = 200;
int a7 = 300, b7 = 200;
int a8 = 400, b8 = 200;
int a9 = 100, b9 = 300;
int a10 = 200, b10 = 300;
int a11 = 300, b11 = 300;
int a12 = 400, b12 = 300;
int a13 = 100, b13 = 400;
int a14 = 200, b14 = 400;
int a15 = 300, b15 = 400;
int a16 = 400, b16 = 400;
public main()
{
tm.start(); //starts timer
addKeyListener(this); //this refearing to KeyListener
setFocusable(true); //enable KeyListener
setFocusTraversalKeysEnabled(false); //shift or tab is not use so F
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.black);
g.setColor(Color.blue);
g.drawString("1", a1, b1);
g.drawString("1", a2, b2);
g.drawString("2", a3, b3);
g.drawString("2", a4, b4);
g.drawString("1", a5, b5);
g.drawString("1", a6, b6);
g.drawString("2", a7, b7);
g.drawString("2", a8, b8);
g.drawString("3", a9, b9);
g.drawString("3", a10, b10);
g.drawString("4", a11, b11);
g.drawString("4", a12, b12);
g.drawString("3", a13, b13);
g.drawString("3", a14, b14);
g.drawString("4", a15, b15);
g.drawString("4", a16, b16);
}
public void actionPerformed(ActionEvent e) //stops at edges
{
if (a1 > 400) {a1 = 100;}
if (a2 > 400) {a2 = 200;}
if (a3 > 400) {a3 = 100;}
if (a4 > 400) {a4 = 200;}
if (a5 > 400) {a5 = 100;}
if (a6 > 400) {a6 = 200;}
if (a7 > 400) {a7 = 100;}
if (a8 > 400) {a8 = 200;}
if (a9 > 400) {a9 = 100;}
if (a10 > 400) {a10 = 200;}
if (a11 > 400) {a11 = 100;}
if (a12 > 400) {a12 = 200;}
if (a13 > 400) {a13 = 100;}
if (a14 > 400) {a14 = 200;}
if (a15 > 400) {a15 = 100;}
if (a16 > 400) {a16 = 200;}
if (b1 > 400) {b1 = 100;}
if (b2 > 400) {b2 = 100;}
if (b3 > 400) {b3 = 100;}
if (b4 > 400) {b4 = 100;}
if (b5 > 400) {b5 = 200;}
if (b6 > 400) {b6 = 200;}
if (b7 > 400) {b7 = 200;}
if (b8 > 400) {b8 = 200;}
if (b9 > 400) {b9 = 100;}
if (b10 > 400) {b10 = 100;}
if (b11 > 400) {b11 = 100;}
if (b12 > 400) {b12 = 100;}
if (b13 > 400) {b13 = 200;}
if (b14 > 400) {b14 = 200;}
if (b15 > 400) {b15 = 200;}
if (b16 > 400) {b16 = 200;}
repaint(); // repaint rectangle
}
public void keyPressed(KeyEvent e)
{
int c = e.getKeyCode();
if (c == KeyEvent.VK_Q)
{
b2 = b2 + 200; //2nd column
b6 = b6 + 200;
b10 = b10 + 200;
b14 = b14 + 200;
b3 = b3 + 200; //3rd column
b7 = b7 + 200;
b11 = b11 + 200;
b15 = b15 + 200;
}
if (c == KeyEvent.VK_W)
{
b1 = b1 + 200; //1st column
b5 = b5 + 200;
b9 = b9 + 200;
b13 = b13 + 200;
b4 = b4 + 200; //4th column
b8 = b8 + 200;
b12 = b12 + 200;
b16 = b16 + 200;
}
if (c == KeyEvent.VK_E)
{
a1 = a1 + 200; //1st row
a2 = a2 + 200;
a3 = a3 + 200;
a4 = a4 + 200;
a13 = a13 + 200; //4th row
a14 = a14 + 200;
a15 = a15 + 200;
a16 = a16 + 200;
}
if (c == KeyEvent.VK_D)
{
a5 = a5 + 200; //2nd row
a6 = a6 + 200;
a7 = a7 + 200;
a8 = a8 + 200;
a9 = a9 + 200; //3rd row
a10 = a10 + 200;
a11 = a11 + 200;
a12 = a12 + 200;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){} //when you stop pressing, vel
public static void main(String[] args)
{
main m = new main();
JFrame jf = new JFrame();
jf.setTitle("Torus");
jf.setSize(800,800);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(m);
jf.setVisible(true); // after adding all components to the JFrame
}
}
答案 0 :(得分:0)
这里真正显而易见的是变量的使用不当,而不是所有这些a1 a2 a3 ..... b1 b2 ....也许只考虑使用数组,也许是二维数组?从那里你可以开始使你的代码更好。