简体   繁体   English

硬币翻转程序

[英]Coin flip program

I tried making a program that flips a coin(shows image of heads first and later shows image of tails) and I encountered problems trying to have the image of the coin viewed when I ran the problem;我尝试制作一个翻转硬币的程序(先显示正面图像,然后显示反面图像),但在运行问题时尝试查看硬币图像时遇到了问题; only a blank screen would show.只会显示一个空白屏幕。 I don't know whether this is from an improper saving method of the jpg images or from an error in the code.我不知道这是由于 jpg 图像的不正确保存方法还是代码错误造成的。 I also came across an error before again coding the program where I had the heads image show and tails image not show.在再次对程序进行编码之前,我也遇到了一个错误,其中显示了头部图像而未显示尾部图像。

CoinTest.java runs coin runner and Coin.java is the class for the program. CoinTest.java 运行 coin runner,Coin.java 是程序的类。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CoinTest extends JPanel
implements ActionListener
{
  private Coin coin;

  public CoinTest ()
{
Image heads = (new ImageIcon("quarter-coin-head.jpg")).getImage();
Image tails = (new ImageIcon("Indiana-quarter.jpg")).getImage();
coin = new Coin(heads, tails);

Timer clock = new Timer(2000, this);
clock.start();
}

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

int x = getWidth() / 2;
int y = getHeight() / 2;
coin.draw(g, x, y);
}

 public void actionPerformed(ActionEvent e)
   {
    coin.flip();
    repaint();
   }

public static void main(String[] args)
{
JFrame w = new JFrame("Flipping coin");
w.setSize(300, 300);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    CoinTest panel = new CoinTest();
    panel.setBackground(Color.WHITE);
    Container c = w.getContentPane();
    c.add(panel);

    w.setVisible(true);
  }
}

Now the actual Coin class.现在是实际的 Coin 类。

import java.awt.Image;
import java.awt.Graphics;

public class Coin
{
private Image heads;
private Image tails;
private int side = 1;

public Coin(Image h, Image t)
{
    heads = h;
    tails = t;
}

//flips the coin
public void flip()
{
    if (side == 1)
        side = 0;
    else
        side = 1;
}

//draws the appropriate side of the coin - centered  in the JFrame
public void draw(Graphics g, int x, int y)
{
    if (side == 1)
    g.drawImage(heads, heads.getWidth(null)/3, heads.getHeight(null)/3, null);
    else 
    g.drawImage(heads, tails.getWidth(null)/3, tails.getHeight(null)/3, null);
}
}

Firstly, ensure that both images are in the correct location to load.首先,确保两个图像都在正确的位置加载。

Secondly, you have a typo here:其次,你这里有一个错字:

if (side == 1)
  g.drawImage(heads, heads.getWidth(null)/3, heads.getHeight(null)/3, null);
else 
  g.drawImage(heads, tails.getWidth(null)/3, tails.getHeight(null)/3, null);
              ^^^^

should be tails...应该是尾巴...

The width and height of the applet are coded in the tag.小程序的宽度和高度在标签中编码。 The code that draws the applet uses the two methods to get these values at run time.绘制小程序的代码使用这两种方法在运行时获取这些值。 So now, different tags can ask for the same applet to paint different sized rectangles.所以现在,不同的标签可以要求同一个小程序绘制不同大小的矩形。 The source code does not need to be recompiled for different sizes.源代码不需要针对不同的大小重新编译。

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

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