Note that there are some explanatory texts on larger screens.

plurals
  1. POusing Graphic g of Paint() outside in java
    text
    copied!<p>Hey I want to create a applet "whiteboard" for doing rough work while browsing. It includes features like drawing,doing rough,writing. As the user click and drag the mouse the path is drawn using drawLine command. I have create a ObjectDrawer Class which listen to MouseDrag Event and then draw the object using 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();</p> <p>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? 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?</p> <pre><code>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); } } } </code></pre> <p>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??</p> <pre><code>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()); } } </code></pre> <p>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?</p> <pre><code> 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){} } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload