在我的程序中,我正在使用一个基本的图形组件,在代码中显示"错误",在屏幕上打印女士错误。每次我运行,或者在绘制错误和图像之间有所延迟时,无论如何都会打印出错误。这是我的代码:
import static java.lang.System.*;
import static java.lang.Math.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.Graphics.*;
import java.awt.event.*;
import java.io.*;
public class ladyBugs
{
public static void main(String[]args)
{
new Windows();
}
}
class Windows extends Frame implements ActionListener, KeyListener, MouseListener
{
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private double width = screenSize.getWidth();
private double height = screenSize.getHeight();
private int SCWIDTH = (int)width, SCHEIGHT = (int)760;
private int whichScreen;
private int turn;
private Image myPic;
private Image myScreen;
private Graphics pen;
public Windows()
{
out.println(SCWIDTH +" "+ SCHEIGHT);
setSize(SCWIDTH, SCHEIGHT);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
myPic = Toolkit.getDefaultToolkit().getImage("C:\\Users\\s613228\\Pictures\\moby.png");
this.setVisible(true);
this.addKeyListener(this);
this.addMouseListener(this);
gameLoop();
}
public void gameLoop()
{
}
public void paint(Graphics pen)
{
if(whichScreen ==0)
{
setBounds(0, 0, SCWIDTH, SCHEIGHT);
pen.setFont(new Font("verdana", Font.PLAIN, 50));
pen.drawString("Cody's Test Program", SCWIDTH/5+50, SCHEIGHT/6); // Centered
pen.drawImage(myPic, SCWIDTH/8, 150, 351, 144, this);
pause(1000);
whichScreen++;
}
else
doubleBuffer(pen);
}
public void doubleBuffer(Graphics pen)
{
Image code = Toolkit.getDefaultToolkit().getImage("c:\\users\\s613228\\pictures\\screenCapJCreator.png");
pen.setColor(Color.WHITE);
pen.setFont(new Font("verdana", Font.PLAIN, 50));
pen.drawString("Cody's Test Program", SCWIDTH/5+50, SCHEIGHT/6); // Centered
pen.drawImage(code, 0, 0, SCWIDTH, SCHEIGHT, this);
pen.setColor(Color.BLACK);
pen.setFont(new Font("verdana", Font.PLAIN, 60));
pen.drawLine(0, 760, SCWIDTH, 760);
pen.drawString("There are bugs in the code!", SCWIDTH/5, SCHEIGHT/6); // Centered
for(int x = 0; x < 50; x++)
{
int random1 = (int)(random()*SCWIDTH*2-1)+1;
int random2 = (int)(random()*1100-1)+200;
int size = (int)(random()*100)+20;
ladyBugs(random1, random2, size, pen);
pause(15);
}
}
public void ladyBugs(int x, int y, int size, Graphics pen)
{
pen.setColor(Color.BLACK); // Head
pen.fillOval(x/2-size/4, y/2-(size/2+size/4), size/2, size/2);
pen.setColor(Color.WHITE); // Whites of eyes
pen.fillOval(x/2-size/6, y/2-(size/2+size/6), size/8, size/8);
pen.fillOval(x/2+(size/6-size/8), y/2-(size/2+size/6), size/8, size/8);
// Body
pen.setColor(Color.BLACK);
pen.fillOval(x/2-size/2-4, y/2-size/2-4, size+8, size+8);
pen.setColor(Color.RED);
pen.fillOval(x/2-size/2, y/2-size/2, size, size);
pen.setColor(Color.BLACK); // Middle-Line Wing Separation
pen.drawLine(x/2, y/2-size/2, x/2, y/2+size/2);
pen.drawLine(x/2-1, y/2-size/2, x/2-1, y/2+size/2-1);
pen.drawLine(x/2+1, y/2-size/2, x/2+1, y/2+size/2-1);
// Upper-Half Black Dots
pen.fillOval(x/2-size/4, y/2-size/4, size/5, size/5);
pen.fillOval(x/2+(size/4-size/5), y/2-size/4, size/5, size/5);
// Lower-Half Black Dots
pen.fillOval(x/2-size/3, y/2+size/6, size/5, size/5);
pen.fillOval(x/2+(size/3-size/5), y/2+size/6, size/5, size/5);
// Pupils
pen.fillOval(x/2-(size/6-size/32), y/2-(size/2+size/7), size/12, size/16);
pen.fillOval(x/2+(size/12-size/48), y/2-(size/2+size/7), size/12, size/16);
// Antenae
int s = size/2;
pen.drawArc(x/2-(s+(s/16)), y/2-size+(s/4), size/2, size/2, 90, -90);
pen.drawArc(x/2+s/16, y/2-size+(s/4), size/2, size/2, 90, 90);
pen.fillOval(x/2-(s+s/8)/2, y/2-(size-s/4)-s/15, s/10, s/10);
pen.fillOval(x/2+s/2, y/2-(size-s/4)-s/15, s/10, s/10);
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void keyEntered(KeyEvent e)
{
}
public void mouseClicked(MouseEvent m)
{
}
public void mousePressed(MouseEvent m)
{
}
public void mouseReleased(MouseEvent m)
{
}
public void mouseEntered(MouseEvent m)
{
}
public void mouseExited(MouseEvent m)
{
}
public void actionPerformed(ActionEvent b)
{
}
public void update(Graphics G)
{
paint(G);
}
public static void pause(long r)
{
try
{
Thread.sleep(r);
}
catch (Exception e)
{
out.println(" Sleep Error "+e);
}
}
}
我特别想要的是如何在错误之前打印背景图像(此代码的屏幕截图)。这是具体的部分:
public void paint(Graphics pen)
{
if(whichScreen ==0)
{
setBounds(0, 0, SCWIDTH, SCHEIGHT);
pen.setFont(new Font("verdana", Font.PLAIN, 50));
pen.drawString("Cody's Test Program", SCWIDTH/5+50, SCHEIGHT/6); // Centered
pen.drawImage(myPic, SCWIDTH/8, 150, 351, 144, this);
pause(1000);
whichScreen++;
}
else
doubleBuffer(pen);
}
public void doubleBuffer(Graphics pen)
{
Image code = Toolkit.getDefaultToolkit().getImage("c:\\users\\s613228\\pictures\\screenCapJCreator.png");
pen.setColor(Color.WHITE);
pen.setFont(new Font("verdana", Font.PLAIN, 50));
pen.drawString("Cody's Test Program", SCWIDTH/5+50, SCHEIGHT/6); // Centered
pen.drawImage(code, 0, 0, SCWIDTH, SCHEIGHT, this);
pen.setColor(Color.BLACK);
pen.setFont(new Font("verdana", Font.PLAIN, 60));
pen.drawLine(0, 760, SCWIDTH, 760);
pen.drawString("There are bugs in the code!", SCWIDTH/5, SCHEIGHT/6); // Centered
for(int x = 0; x < 50; x++)
{
int random1 = (int)(random()*SCWIDTH*2-1)+1;
int random2 = (int)(random()*1100-1)+200;
int size = (int)(random()*100)+20;
ladyBugs(random1, random2, size, pen);
pause(15);
}
}
public void ladyBugs(int x, int y, int size, Graphics pen)
{
pen.setColor(Color.BLACK); // Head
pen.fillOval(x/2-size/4, y/2-(size/2+size/4), size/2, size/2);
pen.setColor(Color.WHITE); // Whites of eyes
pen.fillOval(x/2-size/6, y/2-(size/2+size/6), size/8, size/8);
pen.fillOval(x/2+(size/6-size/8), y/2-(size/2+size/6), size/8, size/8);
// Body
pen.setColor(Color.BLACK);
pen.fillOval(x/2-size/2-4, y/2-size/2-4, size+8, size+8);
pen.setColor(Color.RED);
pen.fillOval(x/2-size/2, y/2-size/2, size, size);
pen.setColor(Color.BLACK); // Middle-Line Wing Separation
pen.drawLine(x/2, y/2-size/2, x/2, y/2+size/2);
pen.drawLine(x/2-1, y/2-size/2, x/2-1, y/2+size/2-1);
pen.drawLine(x/2+1, y/2-size/2, x/2+1, y/2+size/2-1);
// Upper-Half Black Dots
pen.fillOval(x/2-size/4, y/2-size/4, size/5, size/5);
pen.fillOval(x/2+(size/4-size/5), y/2-size/4, size/5, size/5);
// Lower-Half Black Dots
pen.fillOval(x/2-size/3, y/2+size/6, size/5, size/5);
pen.fillOval(x/2+(size/3-size/5), y/2+size/6, size/5, size/5);
// Pupils
pen.fillOval(x/2-(size/6-size/32), y/2-(size/2+size/7), size/12, size/16);
pen.fillOval(x/2+(size/12-size/48), y/2-(size/2+size/7), size/12, size/16);
// Antenae
int s = size/2;
pen.drawArc(x/2-(s+(s/16)), y/2-size+(s/4), size/2, size/2, 90, -90);
pen.drawArc(x/2+s/16, y/2-size+(s/4), size/2, size/2, 90, 90);
pen.fillOval(x/2-(s+s/8)/2, y/2-(size-s/4)-s/15, s/10, s/10);
pen.fillOval(x/2+s/2, y/2-(size-s/4)-s/15, s/10, s/10);
}
注意:当然,所述图像是从我的C:\驱动器引导的,所以除非你使用类似的图片,否则它显然不会出现在y&#39;所有内容中。 谢谢