编写一个小应用程序(Applet)

编写一个小应用程序(Applet),第1张

import java.awt.*

/**

* @author Hardneedl

*/

interface Brush {

void doPaint(Graphics g)

}

import javax.swing.*

import javax.swing.border.*

import java.awt.*

import java.awt.event.*

/**

* @author Hardneedl

*/

public class StringAppletDemo extends JApplet {

private StringBrush stringBrush

private class _Canvas extends JComponent {

protected void paintComponent(Graphics g) {

super.paintComponent(g)

stringBrush.doPaint(g)

}

public Border getBorder() {return BorderFactory.createLineBorder(Color.BLACK,2)}

}

private _Canvas canvas

private final static int STEP = 10

private final static Point PIONT = new Point(100, 170)

protected void processKeyEvent(KeyEvent e) {

switch (e.getKeyCode()) {

case KeyEvent.VK_LEFT:

stringBrush.setLocation(PIONT.x -= STEP, PIONT.y)

canvas.paintImmediately(canvas.getBounds())

break

case KeyEvent.VK_RIGHT:

stringBrush.setLocation(PIONT.x += STEP, PIONT.y)

canvas.paintImmediately(canvas.getBounds())

break

case KeyEvent.VK_DOWN:

stringBrush.setLocation(PIONT.x, PIONT.y += STEP)

canvas.paintImmediately(canvas.getBounds())

break

case KeyEvent.VK_UP:

stringBrush.setLocation(PIONT.x, PIONT.y -= STEP)

canvas.paintImmediately(canvas.getBounds())

break

}

}

public void init() {

super.init()

stringBrush = new StringBrush(StringAppletDemo.class.getName())

canvas = new _Canvas()

getContentPane().add(canvas, BorderLayout.CENTER)

}

private class StringBrush implements Brush {

private String s

private StringBrush(String s) {this.s = s}

private Point p = new Point(100,100)

private void setLocation(int x,int y) {p.setLocation(x,y)}

public void doPaint(Graphics g) {

Graphics g2d = g.create()

g2d.drawString(s, p.x, p.y)

g2d.dispose()

}

}

}

import java.awt.Graphicsimport java.applet.Appletimport javax.swing.JFramepublic class AppletN extends Applet{public void paint(Graphics g){int sum=0 int row=1, col=1 for(int i=1i<=10i++){int sun=1for(int j=1j<=ij++){sun*=j }sum+=sun g.drawString(i+"!="+sun, 80*col, 40*row) if(++col==4){row++ col=1 }}row++ col=80 g.drawString("1!+2!+..+10!="+sum, col, 40*row) }public static void main(String a[]){JFrame j = new JFrame("AppletN") j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) j.setSize(400, 300) j.add(new AppletN()) j.setVisible(true) }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8127145.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存