java中color类怎么调用,和设置数值啊,好多次都出现错误,求解答,一下面的这个程序为例
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassMouseDemo3exte...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseDemo3 extends JFrame implements MouseListener,MouseMotionListener{
int flag;
int x=0;
int y=0;
int startx,starty,endx,endy;
public MouseDemo3(){
Container contentPane=getContentPane();
contentPane.addMouseListener(this);
contentPane.addMouseMotionListener(this);
setSize(300,300);
show();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void mousePressed(MouseEvent e){
startx=e.getX();
starty=e.getY();
}
public void mouseReleased(MouseEvent e){
endx=e.getX();
endy=e.getY();
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
public void mouseMoved(MouseEvent e){
flag=1;
x=e.getX();
y=e.getY();
repaint();
}
public void mouseDragged(MouseEvent e){
flag=2;
x=e.getX();
y=e.getY();
repaint();
}
public void update(Graphics g){
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(),getHeight());
paint(g);
}
public void paint(Graphics g){
g.setColor(Color.red);//主要是这!!
if(flag==1){
g.drawString("鼠标坐标:("+x+" ,"+y+" )",10,50);
g.drawLine(startx,starty,endx,endy);
}
if(flag==2){
g.drawString("拖曳鼠标坐标:("+x+","+y+")",10,50);
g.drawLine(startx,starty,x,y);
}
}
public static void main(String[] args){
new MouseDemo3();
}
} 展开
import java.awt.event.*;
import javax.swing.*;
public class MouseDemo3 extends JFrame implements MouseListener,MouseMotionListener{
int flag;
int x=0;
int y=0;
int startx,starty,endx,endy;
public MouseDemo3(){
Container contentPane=getContentPane();
contentPane.addMouseListener(this);
contentPane.addMouseMotionListener(this);
setSize(300,300);
show();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void mousePressed(MouseEvent e){
startx=e.getX();
starty=e.getY();
}
public void mouseReleased(MouseEvent e){
endx=e.getX();
endy=e.getY();
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
public void mouseMoved(MouseEvent e){
flag=1;
x=e.getX();
y=e.getY();
repaint();
}
public void mouseDragged(MouseEvent e){
flag=2;
x=e.getX();
y=e.getY();
repaint();
}
public void update(Graphics g){
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(),getHeight());
paint(g);
}
public void paint(Graphics g){
g.setColor(Color.red);//主要是这!!
if(flag==1){
g.drawString("鼠标坐标:("+x+" ,"+y+" )",10,50);
g.drawLine(startx,starty,endx,endy);
}
if(flag==2){
g.drawString("拖曳鼠标坐标:("+x+","+y+")",10,50);
g.drawLine(startx,starty,x,y);
}
}
public static void main(String[] args){
new MouseDemo3();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询