嘿,我想创建一个小程序“白板”,以便在浏览时进行粗略的工作。它包括绘画,粗略,写作等功能。当用户单击并拖动鼠标时,使用 drawLine 命令绘制路径。我创建了一个 ObjectDrawer 类,它监听 MouseDrag 事件,然后使用 Graphics g 绘制对象。我使用这个命令来获取图形 g,我知道它不正确,但我找不到解决方案 Graphic 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不一样。我的意思是只有初始化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());
}
}
抱歉,这个问题变得有点冗长和令人困惑,因为我无法理解真正的问题是什么。感谢阅读编辑:我的新代码有效。请解释为什么它现在有效?只有当我在南部区域移动鼠标时,我的小程序中的面板(按钮)才可见,否则它根本不可见?我必须设置可见( 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){}
}