在JPanel中用java绘制图像

时间:2013-07-17 10:38:30

标签: java graphics jframe jpanel

好的,所以在得到第一个评论后,我做了建议现在 我没有得到任何恐怖 但我也没有得到任何图片,因为堆栈可能是空的 为什么它是空的?

嘿伙计们,我正在写一个小项目。 所以我的问题是我有一个名为slot的类。 slot是一个获得一些私有变量的JPanel。 它有一堆WhitePiece和一堆BlackPiece  WhitePiece和BlackPiece是获得图像的对象。 它有一个slotNumber,可以帮助我知道它应该在板上的位置。 它有一个int piecesAmount。

它有一些处理这些变量的方法。

现在我做了什么,以检查我是否可以画槽 我写了一个名为SlotCheck的类,它扩展了JFrame并添加了一个Slot。 现在在Slot里面,我得到了一个名为draw Slot的方法,它可以得到Graphics g 在JPanel上绘制图像。

所以我知道它有点混乱和混乱,我写得不清楚 但我希望你能理解这段代码。

这是WhitePiece的代码:

package Try1;

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class WhitePiece {
    private ImageIcon piecePic;
    private Image pic;

    public WhitePiece() throws IOException {
        piecePic = new ImageIcon("pics/whitePiece.png");
        pic = ImageIO.read(new File("pics/whitePiece.png"));
    }

    public Image getPic() {
        return pic;
    }
}

这是插槽的代码: 我认为你可以跳转到drawSlot方法,因为所有其他东西都是非常不可靠的。

package Try1;

import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.util.Stack;
import javax.swing.JPanel;

// slot is a defined place on the backgammon board that can hold 
// one type of pieces black or white.
//there are 24 slots 12 on each side and 6 in each quartor.
//if a slot holds 2 or more pieces those pieces cannot be eaten.
public class Slot extends JPanel{
 private int slotNumber;
 private int piecesAmount;
 private SlotType type;
 private Stack<WhitePiece> wPieces;
 private Stack<BlackPiece> bPieces;

 public Slot(){
  type = SlotType.empty;
  piecesAmount = 0;
  setSize(300,40);
  wPieces = new Stack<WhitePiece>();
  bPieces = new Stack<BlackPiece>();
 }
 public SlotType getType(){
  return type;
 }
 public void setType(SlotType t){
  if(piecesAmount == 0)
    type = t;
 }
 public int getPiecesAmount(){
  return piecesAmount;
 }
 public void setPiecesAmount(int pa) throws IOException{ 
  if(type != SlotType.empty){
      piecesAmount = pa;
      if(type == SlotType.black){
         if(pa>bPieces.size())
           for(int i=0;i<(pa-bPieces.size());i++)
              bPieces.push(new BlackPiece());
         else
             if(pa<bPieces.size())
                 for(int i=0;i<(bPieces.size()-pa);i++)
                     bPieces.pop();
     }
      else{
          if(pa>wPieces.size())
               for(int i=0;i<(pa-wPieces.size());i++)
                  wPieces.push(new WhitePiece());
             else
                 if(pa<wPieces.size())
                     for(int i=0;i<(wPieces.size()-pa);i++)
                         wPieces.pop();
      }
  }
 }
  public void decreasePiecesAmount(){
  if(type != SlotType.empty){
      piecesAmount --;
      if(type == SlotType.black)
          bPieces.pop();
      else
          wPieces.pop();
  }
 }
 public void increasePiecesAmount() throws IOException{
  if(type != SlotType.empty){
      piecesAmount ++;
      if(type == SlotType.black)
          bPieces.push(new BlackPiece());
      else
          wPieces.push(new WhitePiece());
  }
 }
 public void pushPiece(){

 }
 public void drawSlot(Graphics g) throws IOException{
  if(type == SlotType.empty){ 
  }else
      if(type == SlotType.white){
        Image pic = wPieces.pop().getPic();
        wPieces.push(new WhitePiece());
        if(slotNumber <= 11){
          for(int i=0;i<wPieces.size();i++){
            g.drawImage(pic, 5, i*30, null);
          }
        }
        else{
            for(int i=0;i<wPieces.size();i++){
                g.drawImage(pic, 5,300-(i*30), null);
            }
       }
      }else
      {
          Image pic = bPieces.pop().getPic();
          bPieces.push(new BlackPiece());
          if(slotNumber<=11){
             for(int i=0;i<bPieces.size();i++){
                g.drawImage(pic, 5, i*30, 30, 30, null);
            }
          }else{
              for(int i=0;i<bPieces.size();i++){
                    g.drawImage(pic, 5, 300-(i*30), 30, 30, null);
              }  
          }
    } 
 }
  protected void setSlotNumber(int sN){
   slotNumber = sN;
  }
  public int getSlotNumber(){
  return slotNumber;
  }
}

最后这里是slotCheck的代码:

package Try1;
import java.io.IOException;

import javax.swing.JFrame;
public class SlotCheck extends JFrame{
  private Slot sl;
  public SlotCheck() throws IOException{
  sl = new Slot();
  sl.setType(SlotType.white);
  sl.setPiecesAmount(5);
  sl.setSlotNumber(5);
  add(sl);
  sl.setBounds(100,10,40,300);
  }
  public void drawSlot() throws IOException{
  sl.drawSlot(getGraphics());
  }
  public static void main(String[]args) throws IOException{
  SlotCheck sc = new SlotCheck();
  sc.setDefaultCloseOperation(EXIT_ON_CLOSE);
  sc.setTitle("check of slot");
  sc.setVisible(true);
  sc.setLocation(300, 200);
  sc.setSize(400,400);
  sc.drawSlot();
  }  
}

这是我在尝试运行运行的SlotCheck时得到的错误 drawSlot方法。

    Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Unknown Source)
at java.util.Stack.pop(Unknown Source)
at Try1.Slot.drawSlot(Slot.java:84)
at Try1.SlotCheck.drawSlot(SlotCheck.java:16)
at Try1.SlotCheck.main(SlotCheck.java:25)

所以我认为问题在于获取图像或在堆栈中 我会赞助你的帮助,因为我是一个初学者,并且显然没有任何线索。 :)

0 个答案:

没有答案