在java中使用外部的Paint()图形g

时间:2013-03-29 18:23:26

标签: java swing applet japplet

嘿我想创建一个applet“whiteboard”,用于在浏览时进行粗略的工作。 它包括绘画,粗糙,写作等功能。 当用户单击并拖动鼠标时,将使用drawLine命令绘制路径。 我创建了一个ObjectDrawer类,它监听MouseDrag Event,然后使用Graphics g绘制对象。 我用这个命令得到图形g,我知道它不正确,但我找不到解决方案 图g = obj.getGraphics();

Morever在创建Applet时,我们不创建任何初始化进程的对象。它会自动调用init()。所以如何使用applet类的变量。我的意思是如果我为WhiteBoard类创建一个对象那么将变量我一直有相同的价值?无论我创造多少个对象? 例如。假设Applet正在使用drawstatus变量作为circle.Now我创建一个对象obj.Does obj.drawStatus是行还是圆?

public class WhiteBoard extends Applet {
    public static int lastx=0;public static int lasty=0;public static String drawStatus="line";

    public void init(){
        setLayout(new BorderLayout());
        MainPanel p=new MainPanel();
        add(p,BorderLayout.SOUTH);
        setBackground(Color.WHITE);
        setForeground(Color.BLUE);
        addMouseListener(new PositionRecorder());
        addMouseMotionListener(new ObjectDrawer());
    }

    public void record(int x,int y){
        lastx=x;
        lasty=y;
    }

}


public class ObjectDrawer extends MouseAdapter {
    WhiteBoard obj=new WhiteBoard();
    int lastx=WhiteBoard.lastx;
    int lasty=WhiteBoard.lasty;int x,y;
    String status=WhiteBoard.drawStatus;
    public void MouseDragged(MouseEvent event){
        x=event.getX();
        y=event.getY();
        Graphics g=obj.getGraphics();
        g.setColor(Color.cyan);

        if(status.equals("line")){
            g.drawLine(lastx,lasty,x,y);
        }
        if(status.equals("rectangle")){

            g.drawRect(lastx,lasty,x-lastx,y-lasty);
        }
        if(status.equals("circle")){
            int r=(int)Math.sqrt(Math.pow(x-lastx,2)+Math.pow(y-lasty, 2));
            g.drawOval(lastx,lasty,2*r,2*r);
        }

    }
}

这里是applet或其他地方的g(Graphics)绘图吗?创建一个对象并使用getGraphics是否正确?因为初始化applet和this obj的对象是不一样的。我的意思是只有初始化的obj小程序可以改变图形??

public class PositionRecorder extends MouseAdapter {
    WhiteBoard obj=new WhiteBoard();
    public void mouseEntered(MouseEvent event) {
        //requestFocus(); // Plan ahead for typing
        obj.record(event.getX(), event.getY());
    }

    public void mousePressed(MouseEvent event) {
        obj.record(event.getX(), event.getY());
    }
}

对不起,这个问题变得有点长,令人困惑,因为我无法理解究竟是什么问题。 谢谢阅读 编辑: 我的新代码工作.Pleasse解释为什么它现在正在工作?我的小工具面板(按钮)只有在我移动我的鼠标在南部区域时才可见,否则它根本不可见?我必须在applet中设置setVisible(true)面板?

    import javax.swing.*;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.*;
    import java.awt.event.MouseEvent;

    public class WhiteBoard extends JApplet implements           MouseListener,MouseMotionListener,KeyListener{
public  int lastx=0;public  int lasty=0; 
Graphics g;Font f;
public void init(){

    MainPanel p=new MainPanel();
    getContentPane().add(BorderLayout.SOUTH,p);
    setBackground(Color.WHITE);

    addMouseListener(this);
    addMouseMotionListener(this);
    addKeyListener(this);

    g=getGraphics();g.setColor(Color.BLUE);
    f=new Font("SERIF",Font.BOLD,16);
    g.setFont(f);
    }

public void record(int x,int y){
    lastx=x;
    lasty=y;
}

public void paint(Graphics g){



}
public void mouseMoved(MouseEvent event){
    record(event.getX(),event.getY());
}
 public void mouseEntered(MouseEvent event) {
      requestFocus(); // Plan ahead for typing
      record(event.getX(), event.getY());

    }

    public void mousePressed(MouseEvent event) {
      record(event.getX(), event.getY());

    }

    public void mouseDragged(MouseEvent event){
        int x,y;
         x=event.getX();
         y=event.getY();


            g.drawLine(lastx,lasty,x,y);
            record(x,y);
        }


    public void keyTyped(KeyEvent ke){

        String msg=String.valueOf(ke.getKeyChar());
        g.drawString(msg,lastx,lasty);
        record(lastx+9,lasty);
    }

    public void keyReleased(KeyEvent ke){}
    public void keyPressed(KeyEvent ke){}
    public void mouseClicked(MouseEvent event){}
    public void mouseExited(MouseEvent event){}
    public void mouseReleased(MouseEvent event){}

  }

1 个答案:

答案 0 :(得分:0)

您需要覆盖applet的paint()方法。

@override
public void paint(Graphics g)
{
    super.paint(g);
    g.setColor(Color.cyan);

    if(status.equals("line")){
            g.drawLine(lastx,lasty,x,y);
    }
    if(status.equals("rectangle")){
            g.drawRect(lastx,lasty,x-lastx,y-lasty);
    }
    if(status.equals("circle")){
            int r=(int)Math.sqrt(Math.pow(x-lastx,2)+Math.pow(y-lasty, 2));
            g.drawOval(lastx,lasty,2*r,2*r);
    }

}

在mouseDragged()中,你需要将x和y保存为实例变量

之后在mouseDragged()中调用repaint()