otherclientspanel.java

来自「软件工程实践课程的答案哦」· Java 代码 · 共 228 行

JAVA
228
字号
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import javax.swing.border.*;

class OtherClientsPanel extends JPanel
{
	
    public LOVList lovList;
	private int size=14;
	int order = 0;
	
	
	public void paintComponent(Graphics g)
	{  
		super.paintComponent(g);
		
		LOV lov=lovList.head;
		
		while(lov!=null)
		{
			paintRect(g,lov.x,lov.y,lov.color);
			lov=lov.next;
		}
		/*for(int i=0;i<18;i++)
		paintRect(g,i,0,1);*/
		
		if(order!=0)
		{
			while(lov!=null)
			{
				paintRect(g,lov.x,lov.y,-1);
				lov=lov.next;
			}
			
			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);
			
		    switch(order)
			{
				case 1:
					g.drawImage(i1.getImage(),30,90,color,this);
					break;
				case 2:
					g.drawImage(i2.getImage(),30,90,color,this);
					break;
				case 3:
				    g.drawImage(i3.getImage(),30,90,color,this);
					break;
				case 4:
				    g.drawImage(i4.getImage(),30,90,color,this);
					break;	
			}
			
		}
		
		
	
	}
	
	public void set(LOVList lovList)
	{
		this.lovList=lovList;
		this.setBackground(Color.black);
	}
	
	private void paintRect(Graphics g,int i,int j,int color)
	{  //System.out.println("paintRect");
	   //System.out.println("i="+i);
	   //System.out.println("j="+j);
	   //System.out.println("color="+color);
	   
	   URL ImageURLg = getClass().getResource("pics\\little pics\\gray.gif");
	   ImageIcon gray = new ImageIcon(ImageURLg);
	   
	   URL ImageURLr = getClass().getResource("pics\\little pics\\red.gif");
	   ImageIcon red = new ImageIcon(ImageURLr);
	   
	   URL ImageURLbl = getClass().getResource("pics\\little pics\\blueGreen.gif");
	   ImageIcon blueGreen = new ImageIcon(ImageURLbl);
	   
	   URL ImageURLy = getClass().getResource("pics\\little pics\\yellow.gif");
	   ImageIcon yellow = new ImageIcon(ImageURLy);
	   
	   URL ImageURLb = getClass().getResource("pics\\little pics\\blue.gif");
	   ImageIcon blue = new ImageIcon(ImageURLb);
	   
	   URL ImageURLp = getClass().getResource("pics\\little pics\\purple.gif");
	   ImageIcon purple = new ImageIcon(ImageURLp);
	   
	   URL ImageURLgr = getClass().getResource("pics\\little pics\\green.gif");
	   ImageIcon green = new ImageIcon(ImageURLgr);
		
		switch(color)
		{
			
			case -1:
			    g.drawImage(gray.getImage(),j*size,i*size,Color.black,this);
			    break;
			case 0:
				g.setColor(Color.black);
				g.fill3DRect(j*size,i*size,size,size,true);
			    break;
			case 1: 
				g.drawImage(red.getImage(),j*size,i*size,Color.black,this);
			    break;
			case 2:
				g.drawImage(blueGreen.getImage(),j*size,i*size,Color.black,this);
			    break;
			case 3:
				g.drawImage(yellow.getImage(),j*size,i*size,Color.black,this);
			    break;
			case 4:
				g.drawImage(blue.getImage(),j*size,i*size,Color.black,this);
			    break;
			case 5:
				g.drawImage(purple.getImage(),j*size,i*size,Color.black,this);
			    break;
			case 6:
			    g.drawImage(green.getImage(),j*size,i*size,Color.black,this);
			    break;
		}
		
		
	}
	
	public void setOrder(int order)
	{
		this.order = order;
		repaint();
		
	}
	
}

class OtherClientsInfo extends JPanel
{
	private String name;
	private int score;
	private ImageIcon icon=new ImageIcon("pics\\verticalLine.gif");
	URL infoImageURL = getClass().getResource("pics\\information.gif");
	ImageIcon infoImage = new ImageIcon(infoImageURL);
	public OtherClientsInfo()
	{
		name ="";
		score=0;
		this.setLayout(new BorderLayout());
		this.setBackground(Color.WHITE);
		this.add(new JLabel(icon),BorderLayout.WEST);
		
		
	}
	
	public void setScore(int score)
	{
		this.score = score;
		repaint();
	}
	
	public void setName(String name)
	{
		this.name = name;
		repaint();
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawImage(infoImage.getImage(),0,0,GameFace.bgColor,this);
		g.setFont(new Font("ScanSerif",Font.BOLD,15));
		g.setColor(Color.blue);
		FontMetrics fm=g.getFontMetrics();
	    int w=fm.stringWidth(name);
	    int h=fm.getAscent();
	    int xMiddle=(getSize().width-w)/2-5;
	    int yMiddle=(getSize().height-h)/2;	
		g.drawString(name,xMiddle,yMiddle+5);
		g.drawString("Score: "+String.valueOf(score),35,30);
	}
}

class OtherClients extends JPanel
{
	public OtherClientsPanel ocp;
	public OtherClientsInfo oci;
	
	
	
	public OtherClients()
	{
		ocp=new OtherClientsPanel();
		oci=new OtherClientsInfo();
		
		this.setLayout(new BorderLayout());
		
		this.add(oci,BorderLayout.NORTH);
		this.add(ocp,BorderLayout.CENTER);
		
	}
	
	public void setScore(int score)
	{
		oci.setScore(score);
	}
	
	public void setName(String name)
	{
		oci.setName(name);
	}
	
	public void setOrder(int order)
	{
		//System.out.println("OtherClient"+order);
		this.ocp.setOrder(order);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?