【问题标题】:using Graphic g of Paint() outside in java在java中使用Paint()的Graphic g
【发布时间】:2013-03-31 13:26:17
【问题描述】:

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

此外,在创建 Applet 时,我们不会创建任何初始化进程的对象。它会自动调用 init()。那么如何使用 applet 类的变量。我的意思是,如果我为 WhiteBoard 类创建一个对象,那么变量将始终具有相同的值?无论我创建多少对象? 例如,假设 Applet 使用 drawstatus 变量作为圆。现在我创建一个对象 obj。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);
        }

    }
}

g(Graphics) 会在 applet 或其他地方绘制吗?创建一个对象并使用 getGraphics 是否正确?因为初始化 applet 的对象和这个 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());
    }
}

抱歉,这个问题有点冗长且令人困惑,因为我无法理解真正的问题是什么。 谢谢阅读 编辑: 我的新代码有效。请解释为什么它现在可以工作?只有当我在南部区域移动鼠标时,我的小程序中的面板(按钮)才可见,否则它根本不可见?我必须在小程序中设置可见(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){}

  }

【问题讨论】:

  • 而drawRect,drawOval不能取float值吗?有没有其他的画法让它们可以取float值从而使图形平滑?
  • 为什么白板类有静态字段?实例变量不是更有意义吗?
  • 使用 JApplet 时,背景颜色没有改变。这就是为什么
  • @Cruncher:这正是我的问题。在不同的类中创建 WhiteBoard 的对象然后访问 object.lastx 时,我得到一个空值。这就是我将其设为静态的原因。
  • @cruncher:忽略上面的评论。因为PositionRecorder正在改变lastx值,我想在ObjectDrawer中使用那个值。所以我想把它变成静态的。我可以把它传递给objectDrawer

标签: java swing applet japplet


【解决方案1】:

你需要重写小程序的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()

【讨论】:

  • 请注意,这实际上只会绘制最后一件事。如果您希望在绘制后保存它,则需要保存信息以绘制所有内容。策略设计模式可能有用
  • 使用策略/命令绘制东西的好处是它使撤消变得非常容易
  • :老师说的很简单。我想一定有其他方法可以做到这一点。但是,如果我按照您的建议进行操作,则需要将保存在整数 x 和 y 中的实例变量传递给绘制方法/或重绘。我将如何通过它?我用谷歌搜索并了解了一些缓冲区图像/绘制,我不记得确切。会有帮助吗?
  • 你可以创建一个bufferedImage,使用它的图形对象来绘制它,然后在applet的paint方法中你可以只绘制bufferedImage。
  • 我没有使用所有这些。我只是删除了其他类并将所有这些放在同一个类中并且它有效。仍在寻找原因。
猜你喜欢
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 2012-06-01
  • 2016-12-29
  • 1970-01-01
  • 2022-01-02
相关资源
最近更新 更多