paintpanel.java
来自「软件工程实践课程的答案哦」· Java 代码 · 共 261 行
JAVA
261 行
import java.awt.*;
import javax.swing.*;
import java.net.URL;
import java.awt.event.*;
class PaintPanel extends JPanel implements KeyListener
{
URL blueURL = getClass().getResource("pics\\blue.jpg");
ImageIcon blue = new ImageIcon(blueURL);
URL yellowURL = getClass().getResource("pics\\yellow.jpg");
ImageIcon yellow = new ImageIcon(yellowURL);
URL blueGreenURL =getClass().getResource("pics\\blueGreen.jpg");
ImageIcon blueGreen = new ImageIcon(blueGreenURL);
URL greenURL =getClass().getResource("pics\\green.jpg");
ImageIcon green = new ImageIcon(greenURL);
URL redURL = getClass().getResource("pics\\red.jpg");
ImageIcon red = new ImageIcon(redURL);
URL purpleURL = getClass().getResource("pics\\purple.jpg");
ImageIcon purple = new ImageIcon(purpleURL);
URL grayURL = getClass().getResource("pics\\gray.jpg");
ImageIcon gray = new ImageIcon(grayURL);
URL teamURL = getClass().getResource("pics\\team.jpg");
ImageIcon team = new ImageIcon(teamURL);
URL teamTagURL = getClass().getResource("pics\\teamTag.jpg");
ImageIcon teamTag = new ImageIcon(teamTagURL);
int horizonal = 0;
int vertical = 0;
//static int fallTag = 0;
//static int fallTag2 = 0;
int size = GameFace.unitSize;
GameFace gameFace;
public int order=0;//名次
public void setOrder(int order)
{
this.order=order;
}
public synchronized void paintComponent(Graphics g)
{
super.paintComponent(g);
if(gameFace.gameStart)
{ //get non-zero location and paint it
LOV lov = gameFace.lovList.head;
g.drawImage(team.getImage(),5,120,Color.black,this);
while(lov!=null)
{
this.paintRec(g,lov.x,lov.y,lov.color);
lov = lov.next;
}
}
else if(gameFace.gameOver)
{
LOV lov = gameFace.lovList.head;
while(lov!=null)
{
this.paintRec(g,lov.x,lov.y,-1);
lov = lov.next;
}
URL ImageURL = getClass().getResource("pics\\xiaoxin.gif");
ImageIcon xiaoXin = new ImageIcon(ImageURL);
URL ImageURLt = getClass().getResource("pics\\xiaoxin2.gif");
ImageIcon xiaoXin2 = new ImageIcon(ImageURLt);
URL ImageURL1 = getClass().getResource("pics\\1.gif");
ImageIcon i1 = new ImageIcon(ImageURL1);
URL ImageURL2 = getClass().getResource("pics\\2.gif");
ImageIcon i2 = new ImageIcon(ImageURL2);
URL ImageURL3 = getClass().getResource("pics\\3.gif");
ImageIcon i3 = new ImageIcon(ImageURL3);
URL ImageURL4= getClass().getResource("pics\\4.gif");
ImageIcon i4 = new ImageIcon(ImageURL4);
Color color=new Color(Color.TRANSLUCENT);
if(order==1)
g.drawImage(xiaoXin.getImage(),95,250,color,this);
else
g.drawImage(xiaoXin2.getImage(),95,250,color,this);
switch(order)
{
case 1:
g.drawImage(i1.getImage(),100,170,color,this);
break;
case 2:
g.drawImage(i2.getImage(),100,170,color,this);
break;
case 3:
g.drawImage(i3.getImage(),100,170,color,this);
break;
case 4:
g.drawImage(i4.getImage(),100,170,color,this);
break;
}
}
else
{
g.drawImage(teamTag .getImage(),5,120,Color.black,this);
g.setColor(Color.WHITE);
g.drawLine(0,size*3,350,size*3);
// this.setBackground(Color.BLACK);
}
}
/**paint the square in location(y,x)
* */
public void paintRec(Graphics g,int x ,int y,int color)
{
g.setColor(Color.WHITE);
g.drawLine(0,size*3,350,size*3);
switch(color)
{
case -1:
g.drawImage(gray.getImage(),y*size,x*size,Color.black,this);
break;
case 1:
g.drawImage(red.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.red);
break;
case 2:
g.drawImage(blueGreen.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.green);
break;
case 3:
g.drawImage(yellow.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.yellow);
break;
case 4:
g.drawImage(blue.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.blue);
break;
case 5:
g.drawImage(purple.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.pink);
break;
case 6:
g.drawImage(green.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.pink);
}
//g.fill3DRect(y*size,x*size,size,size,true);
}
public void setGameFace(GameFace gameFace)
{
this.gameFace = gameFace;
}
public void leftMove()
{
if(!gameFace.bar.checkBottom())
gameFace.bar.left();
this.repaint();
}
public void rightMove()
{
if(!gameFace.bar.checkBottom())
gameFace.bar.right();
this.repaint();
}
public void shift()
{
if(!gameFace.bar.checkBottom())
gameFace.bar.shift();
this.repaint();
}
public void downMove()
{
gameFace.bar.dropOneStep();
this.repaint();
}
public synchronized void down()
{
gameFace.bar.drop();
this.repaint();
}
/////////keyListener///////////////
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{ //System.out.println("keyPressed");
if(gameFace.gameStart)
switch(e.getKeyCode())
{
case KeyEvent.VK_LEFT :
this.leftMove();
break;
case KeyEvent.VK_RIGHT :
this.rightMove();
break;
case KeyEvent.VK_UP:
this.shift();
this.repaint();
break;
case KeyEvent.VK_SPACE:
this.down();
this.repaint();
gameFace.timer.resume();
break;
case KeyEvent.VK_DOWN:
//if(gameFace.speed==0)
//gameFace.timer.resume();
//fallTag = 1;
break;
}
}
public void keyReleased(KeyEvent e)
{
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?