简体   繁体   English

在Java外使用Paint()的图形g

[英]using Graphic g of Paint() outside in java

Hey I want to create a applet "whiteboard" for doing rough work while browsing. 嘿,我想创建一个applet“白板”,以便在浏览时进行粗略的工作。 It includes features like drawing,doing rough,writing. 它包括绘图,粗加工,书写等功能。 As the user click and drag the mouse the path is drawn using drawLine command. 当用户单击并拖动鼠标时,将使用drawLine命令绘制路径。 I have create a ObjectDrawer Class which listen to MouseDrag Event and then draw the object using Graphics g. 我创建了一个ObjectDrawer类,它侦听MouseDrag事件,然后使用Graphics g绘制对象。 I use this command to get graphic g and I know its not correct but I couldnot find the solution Graphic g=obj.getGraphics(); 我使用此命令获取图形g,但我知道它不正确,但是找不到解决方案Graphic g = obj.getGraphics();。

Morever While creating an Applet we donot create any object which initialise the process.Its automatically calls init().So how to use the variable of applet class.I mean if I create an object for the WhiteBoard Class then will the variables have the same values all the time?no matter how many object I create? 此外,在创建Applet时,我们不会创建任何初始化该进程的对象,而是会自动调用init(),因此如何使用applet类的变量。我的意思是如果我为Whiteboard类创建了一个对象,那么这些变量将具有相同的含义始终保持价值?无论我创建多少个对象? Eg.Suppose the Applet is working with the drawstatus variable as circle.Now I create an object obj.Does obj.drawStatus is line or circle? 例如,假设小程序正在使用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);
        }

    }
}

Here will the g(Graphics) paint in the applet or somewhere else?And is it correct to create an object and use getGraphics?Because the object which initialise the applet and this obj arent same.I mean only the obj which initialise the applet can change the graphics?? g(Graphics)会在applet或其他地方绘制吗?创建对象并使用getGraphics是否正确?因为初始化applet的对象和此objent相同。我的意思是只有初始化applet的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());
    }
}

Sorry the question become a bit long and confusing as I couldnot understand what really the problem is. 抱歉,这个问题有点冗长和令人困惑,因为我无法理解真正的问题是什么。 Thanks for reading EDIT: My new Code which worked.Pleasse Explaing why is it working now?And my panel(buttons) in applet is visible only when I move my mouse in south region otherwise itsnot visible at all?Do i have to setVisible(true) in applet for panel? 感谢您阅读EDIT:我的新代码有效.Pleasse解释了为什么现在可以正常工作;并且小程序中的面板(按钮)仅在将鼠标移至南部区域时才可见,否则根本看不见吗?我必须设置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){}

  }

You need to override the paint() method of applet. 您需要重写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);
    }

}

In mouseDragged() you're going to need to save x and y as instance variables though 在mouseDragged()中,您需要将x和y保存为实例变量

After that in mouseDragged() you call repaint() 之后,在mouseDragged()中调用repaint()

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

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