当前位置: 技术问答>java相关
该程序怎么无法显示?
来源: 互联网 发布时间:2017-03-22
本文导语: 编译通过,但是执行的时候没有反应!!!没有框架! import java.awt.*; import java.applet.*; public class Drawpics extends Applet { Panel palette ,tools ; Button line ,rect,oval; Button black ,white,red,blue,green,yellow; MyCanvas drawarea;/...
编译通过,但是执行的时候没有反应!!!没有框架!
import java.awt.*;
import java.applet.*;
public class Drawpics extends Applet
{
Panel palette ,tools ;
Button line ,rect,oval;
Button black ,white,red,blue,green,yellow;
MyCanvas drawarea;//定义画布;
int mx=0;//光标位置;
int my=0;
int mx1=0;
int my1=0;
int currentTool =0 ;
Color currentColor =new Color(0,0,0);//首先设为黑色;
public void init()
{
setLayout(new BorderLayout() );//applet布局;
palette =new Panel();
palette.setLayout(new GridLayout (2,3,5,5) );
black =new Button();
black.setBackground(Color.black);
white =new Button();
white.setBackground(Color.white);
blue =new Button();
blue.setBackground(Color.blue);
red =new Button();
red.setBackground(Color.red);
green =new Button();
green.setBackground(Color.green);
yellow =new Button();
yellow.setBackground(Color.yellow);
palette.add(black);
palette.add(white);
palette.add(red);
palette.add(blue);
palette.add(green);
palette.add(yellow);
add(palette,"South");
tools=new Panel();
tools.setLayout(new GridLayout (3,1,5,5) );
line = new Button ("Line");
rect = new Button("Rect");
oval = new Button ("Oval");
tools.add(line);
tools.add(rect);
tools.add(oval);
add(tools,"West");
drawarea =new MyCanvas();
add(drawarea,"Center");
validate();
}
public boolean action (Event evt,Object obj)
{
if (evt.target ==line)
currentTool =0;
if (evt.target ==rect)
currentTool =1;
if (evt.target ==oval)
currentTool =2;
if(evt.target==black)
currentColor =new Color(0,0,0);
if(evt.target==white)
currentColor =new Color(255,255,255);
if(evt.target==red)
currentColor =new Color(255,0,0);
if(evt.target==blue)
currentColor =new Color(0,0,255);
if(evt.target==green)
currentColor =new Color(0,255,0);
if(evt.target==yellow)
currentColor =new Color(255,255,0);
return true;
}
class MyCanvas extends Canvas
{
public MyCanvas()
{
}
public boolean mouseDown(Event e,int x,int y)
{
mx=x;
my=y;
mx1=x;
my1=y;
return(true);
}
public boolean mouseDrag(Event e,int x,int y)
{
mx1=x;
my1=y;
repaint();
return(true);
}
public void paint(Graphics g)
{
g.setColor(currentColor);
if (currentTool==2)
{
g.fillOval(mx,my,mx1,my1);
}
else if (currentTool==1)
{
g.fillRect(mx,my,mx1-mx,my1-my);
}
else if (currentTool==0)
{
g.drawLine(mx,my,mx1,my1);
}
}
}
public static void main(String[] args)
{
Drawpics Dr=new Drawpics();
Dr.init();
}
}
import java.awt.*;
import java.applet.*;
public class Drawpics extends Applet
{
Panel palette ,tools ;
Button line ,rect,oval;
Button black ,white,red,blue,green,yellow;
MyCanvas drawarea;//定义画布;
int mx=0;//光标位置;
int my=0;
int mx1=0;
int my1=0;
int currentTool =0 ;
Color currentColor =new Color(0,0,0);//首先设为黑色;
public void init()
{
setLayout(new BorderLayout() );//applet布局;
palette =new Panel();
palette.setLayout(new GridLayout (2,3,5,5) );
black =new Button();
black.setBackground(Color.black);
white =new Button();
white.setBackground(Color.white);
blue =new Button();
blue.setBackground(Color.blue);
red =new Button();
red.setBackground(Color.red);
green =new Button();
green.setBackground(Color.green);
yellow =new Button();
yellow.setBackground(Color.yellow);
palette.add(black);
palette.add(white);
palette.add(red);
palette.add(blue);
palette.add(green);
palette.add(yellow);
add(palette,"South");
tools=new Panel();
tools.setLayout(new GridLayout (3,1,5,5) );
line = new Button ("Line");
rect = new Button("Rect");
oval = new Button ("Oval");
tools.add(line);
tools.add(rect);
tools.add(oval);
add(tools,"West");
drawarea =new MyCanvas();
add(drawarea,"Center");
validate();
}
public boolean action (Event evt,Object obj)
{
if (evt.target ==line)
currentTool =0;
if (evt.target ==rect)
currentTool =1;
if (evt.target ==oval)
currentTool =2;
if(evt.target==black)
currentColor =new Color(0,0,0);
if(evt.target==white)
currentColor =new Color(255,255,255);
if(evt.target==red)
currentColor =new Color(255,0,0);
if(evt.target==blue)
currentColor =new Color(0,0,255);
if(evt.target==green)
currentColor =new Color(0,255,0);
if(evt.target==yellow)
currentColor =new Color(255,255,0);
return true;
}
class MyCanvas extends Canvas
{
public MyCanvas()
{
}
public boolean mouseDown(Event e,int x,int y)
{
mx=x;
my=y;
mx1=x;
my1=y;
return(true);
}
public boolean mouseDrag(Event e,int x,int y)
{
mx1=x;
my1=y;
repaint();
return(true);
}
public void paint(Graphics g)
{
g.setColor(currentColor);
if (currentTool==2)
{
g.fillOval(mx,my,mx1,my1);
}
else if (currentTool==1)
{
g.fillRect(mx,my,mx1-mx,my1-my);
}
else if (currentTool==0)
{
g.drawLine(mx,my,mx1,my1);
}
}
}
public static void main(String[] args)
{
Drawpics Dr=new Drawpics();
Dr.init();
}
}
|
applet单独运行的main一般这么写!希望有所帮助,呵呵
public static void main(String[] args) {
Applet1 applet = new Applet1();
applet.isStandalone = true;
Frame frame;
frame = new Frame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
public static void main(String[] args) {
Applet1 applet = new Applet1();
applet.isStandalone = true;
Frame frame;
frame = new Frame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}