Java图形和线程 [英] Java Graphics and Threads

查看:62
本文介绍了Java图形和线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于java编程类,我试图创建一个画面正在帧中移动的错觉。一切都在A.W.T.工作得很好。小程序或框架。当我移动东西来摆动时,重绘方法似乎以不同的方式工作。每次重绘时,图像都会留下一个打印,而不是一个漂亮的小跳。每个人都有任何提示我如何改进这个功能。我已经附上了这个项目的代码。谢谢。





For a java programming class I am trying to create the illusion that a face drawn is moving across a frame. Everything is working just fine in an A.W.T. Applet or Frame. When I move things over to swing the repaint method seems to work in a different manner. Instead of a nice little hop across the frame the image leaves a print each time it is redrawn.Does anyone have any tips on how I can improve this feature.I have attached the code for this project. Thanks.


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


public class PersonMain extends Frame implements Runnable{

    private int x =0;

    public PersonMain(){
        super("Moving Face");

        setBackground(Color.BLUE);
       
        setSize(400,400);

        this.addWindowListener(new WindowAdapter(){
              public void windowClosing(WindowEvent we){
                System.exit(0);
              }
            });

        Thread t = new Thread(this);
        t.start();

    }

    public void run() {

        int xDir = +1; //direction (horizontal)
        int incr = 5; //speed of movement
        int sleepFor = 600;
        while (true) {
            x += (incr * xDir);
            /*redraw image in new screen location*/
            repaint();
            setBackground(Color.BLUE);
            try {
                /*put thread to sleep for designated time*/
                Thread.sleep(sleepFor);
            } //ends try
            catch (InterruptedException e) {}
        }
    } //ends method

    public void paint(Graphics g){

        g.setColor(Color.WHITE);

        g.drawOval(x, 100, 200,200);
        g.fillOval(x, 100,200,200);

        drawHat();

        drawEye(x+30,130);
        drawEye(x+140,130);

        drawNose(x,150);

        drawMouth();

    }

    public void drawEye(int x, int y) {

         Graphics g = getGraphics();


         g.drawOval(x, y, 25, 15);
         g.setColor(Color.BLACK);
         g.fillOval(x, y, 25, 10);

    }

    public void drawHat(){

        Graphics g = getGraphics();

         g.drawRect(x+20, 20, 150, 100);
         g.fillRect(x+20,20,150,100);
         g.drawLine(x+20, 120, x+275, 120);

    }

     public void drawNose(int x, int y) {

         Graphics g = getGraphics();

         g.drawLine(x+100, y, x+120, 160);
         g.drawLine(x+100, y, x+120, 160);
     }

     public void drawMouth(){

         Graphics g = getGraphics();

         g.drawArc(x+60, 190, 90, 75, 180, 180);

         }


     public static void main(String args[]){
         PersonMain pm = new PersonMain();

         pm.setVisible(true);
         pm.setSize(600,400);
     }
}

推荐答案

我很惊讶早期的版本没有离开幽灵痕迹也是如此。



你所做的所有代码都是每次向右画5个像素。



它永远不会抹掉以前绘制的东西。



你的绘画方法需要先删掉以前绘制的东西。



最简单的方法是在每次绘制面部之前用背景颜色绘制整个矩形。
I''m kind of surprised that the earlier version didn''t leave the ghost trace as well.

All your code is doing is drawing the face 5 pixels to the right each time.

It doesn''t ever erase what was previously drawn.

Your paint method needs to start by erasing what was previously drawn.

The easiest way would be to paint the entire rectangle with the background color each time before you draw the face.


这篇关于Java图形和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆