简体   繁体   English

画iconimage java摆动?

[英]Paint iconimage java swing?

I'm trying to create a program that draws up playing cards. 我正在尝试创建一个绘制纸牌的程序。 The chart is drawn each time an object of class is created. 每次创建类对象时都会绘制图表。 As it stands now, I have five objects of the class, but only one card is being painted. 就目前情况而言,我有五个班级的对象,但是只绘制了一张卡片。 I wonder of course why? 我当然想知道为什么吗?

I also wonder how I could improve my movement of the cards. 我也想知道如何改善卡片的运动。

Cards.java Cards.java

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class Cards extends JPanel implements MouseMotionListener, MouseListener{

    Random rnd = new Random();
    int X, posX;
    int Y,  posY;
    int line;
    int col;
    String card;
    boolean faceUp;
    boolean pressOut;
    static int numOfCards;


    String [][]  icons = {
            {"img/c1.gif", "img/d1.gif", "img/h1.gif" , "img/s1.gif"},
            {"img/c2.gif", "img/d2.gif", "img/h2.gif" , "img/s2.gif"},
            {"img/c3.gif", "img/d3.gif", "img/h3.gif" , "img/s3.gif"},
            {"img/c4.gif", "img/d4.gif", "img/h4.gif" , "img/s4.gif"},
            {"img/c5.gif", "img/d5.gif", "img/h5.gif" , "img/s5.gif"},
            {"img/c6.gif", "img/d6.gif", "img/h6.gif" , "img/s6.gif"},
            {"img/c7.gif", "img/d7.gif", "img/h7.gif" , "img/s7.gif"},
            {"img/c8.gif", "img/d8.gif", "img/h8.gif" , "img/s8.gif"},
            {"img/c9.gif", "img/d9.gif", "img/h9.gif" , "img/s9.gif"},
            {"img/c10.gif", "img/d10.gif", "img/h10.gif" , "img/s10.gif"},
            {"img/cj.gif", "img/dj.gif", "img/hj.gif" , "img/sj.gif"},
            {"img/cq.gif", "img/dq.gif", "img/hq.gif" , "img/sq.gif"},
            {"img/ck.gif", "img/dk.gif", "img/hk.gif" , "img/sk.gif"}
    };

    public Cards() {
        numOfCards++;
        addMouseListener(this);
        addMouseMotionListener(this);
        this.X = rnd.nextInt(300);
        this.Y = rnd.nextInt(300);
        this.line = rnd.nextInt(13);
        this.col = rnd.nextInt(4);
        this.card = icons[line][col];
        this.faceUp = rnd.nextBoolean();
        this.pressOut = false;
    }


    public void paintComponent(Graphics g){
        super.paintComponent(g);

        if(faceUp == true){
            ImageIcon icon = new ImageIcon(this.getClass().getResource(card));
            g.drawImage(icon.getImage(), this.X, this.Y, this);
        } else {
            ImageIcon icon = new ImageIcon(this.getClass().getResource("/img/b1fv.gif"));
            g.drawImage(icon.getImage(), this.X, this.Y, this);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if(this.faceUp == false){
            this.faceUp = true;
            this.repaint();
        } else {
            this.faceUp = false;
            this.repaint();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        this.posX = this.getX() - e.getX();
        this.posY = this.getY() - e.getY();

        if (this.card != null) { 
            updateLocation(e);
        }else{
            pressOut = true;
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        updateLocation(e);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (!pressOut) {
            updateLocation(e);
        }
    }

    public void updateLocation(MouseEvent e){
        this.setLocation(this.posX + e.getX(), this.posY + e.getY());
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}

Table.java Table.java

    import java.awt.Color;
import java.awt.Frame;

import javax.swing.JFrame;

public class Table{
    public static void main(String[] args) {
        JFrame frame = new JFrame("Cards");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addMouseListener(null);
        frame.addMouseMotionListener(null);
        frame.setBackground(Color.GREEN);
        frame.setSize(400,600);


        Cards c1 = new Cards();
        Cards c2 = new Cards();
        Cards c3 = new Cards();
        Cards c4 = new Cards();
        Cards c5 = new Cards();

        System.out.println(c1.card);
        System.out.println(c2.card);
        System.out.println(c3.card);
        System.out.println(c4.card);
        System.out.println(c5.card);
        System.out.println(c5.numOfCards);

        frame.add(c1);
        frame.add(c2);
        frame.add(c3);
        frame.add(c4);
        frame.add(c5);
        frame.setVisible(true);
    }
}

The default layout for a JFrame is BorderLayout . JFrame的默认布局是BorderLayout

Try using GridLayout , for example 例如,尝试使用GridLayout

frame.setLayout(new GridLayout(0, 3));

Don't load the image in the paintComponent method, this method should exit as fast as possible, instead, pre-load the images... 不要在paintComponent方法中加载图像,该方法应尽快退出,而是预加载图像...

public class Cards extends JPanel implements MouseMotionListener, MouseListener{
    //...
    private Image face;
    private Image back;
    //...
    public Cards() {
        //...
        face = new ImageIcon(this.getClass().getResource(card)).getImage();
        back = ImageIcon icon = new ImageIcon(this.getClass().getResource("/img/b1fv.gif")).getImage();
        //...
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);

        if(faceUp){
            g.drawImage(face, this.X, this.Y, this);
        } else {
            g.drawImage(back , this.X, this.Y, this);
        }
    }

You should also be overriding the getPreferredSize method of the JPanel , this works with the layout manager API to allow it to make decisions about how best to layout component... 您还应该重写JPanelgetPreferredSize方法,该方法与布局管理器API一起使用,以使其能够决定如何最好地布局组件。

public Dimension getPreferredSize() {
    return face == null ? super.getPreferredSize() : new Dimension(face.getWidth(this), face.getHeight(this));
}

This will now allow you to use JFrame#pack instead of JFrame#setSize 现在,这将允许您使用JFrame#pack而不是JFrame#setSize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM