Panel , Panel -, , . .
Window ( ), , . Frame. Frame , , .
Swing JPanel JFrame. Canvas, , , Component.
, . , Java, Internet, , HTML. , , " ", , , . HTML- appletviewer. . , . , , ( ). Applet . Applet java.applet AWT JApplet Swing.
Applet, Web-. main(), init(). init() . start() , , . stop() , Web-, . destroy() , . , paint() Component. paint() , , repaint(), .
, init(), paint(), setColor() drawString() .
/* # 1: : DateApplet.java */
|
|
package chapt11;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Calendar;
import java.util.Formatter;
import javax.swing.JApplet;
public class DateApplet extends JApplet {
private Formatter dateFmt = new Formatter();
private Formatter timeFmt = new Formatter();
public void init() {
setSize(180, 100);
Calendar c = Calendar.getInstance();
String era = "";
if (c.get(Calendar.ERA) == 1)
era = "..";
dateFmt.format("%tA %td.%tm.%tY "
+ era, c, c, c, c);
timeFmt.format("%tT", c);
}
public void paint(Graphics g) {
g.setColor(Color.RED);
g.drawString(" " + timeFmt,
10, getHeight()/2);
g.setColor(new Color(0,87,127));
g.drawString(dateFmt.toString(), 13,
getHeight() - 10);
}
}
. 11.2.
java.util.Calendar. Color.
, - , <applet > </applet> HTML. :
<html>
<applet code = chapt11.DateApplet.class
width = 250 height = 250> </applet></html>
HTML- , .
, setColor(), drawString(), java.awt.Graphics. Graphics , . Graphics ( ) , , . update() paint().
Graphics:
drawLine(int x1, int y1, int x2, int y2) ;
drawRect(int x, int y, int width, int height) fillRect(int x, int y, int width, int height) ;
draw3DRect(int x, int y, int width, int height, boolean raised) ;
drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) ;
drawOval(int x, int y, int width, int height) ;
drawPolygon(int[] xPoints, int[] yPoints, int nPoints) (), x y;
drawPolygon(Polygon p) , Polygon;
drawPolyline(int[] xPoints, int[] yPoints, int nPoints) , x y;
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) ;
drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) ;
drawString(String str, int x, int y) ;
setColor(Color c), getColor() ;
getFont() ;
setFont(Font font) .
Graphics Applet, JApplet.
24, , Graphics .
|
|
/* # 2: : ThrRect.java */
package chapt11;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
public class ThrRect extends JApplet {
public void draw3D(Graphics g, int x, int y, int width, int height, boolean isRaised, boolean isFilled) {
g.draw3DRect(x, y, width - 1, height - 1,
isRaised);
g.draw3DRect(x + 1, y + 1, width - 3,
height - 3, isRaised);
g.draw3DRect(x + 2, y + 2, width - 5,
height - 5, isRaised);
if (isFilled)
g.fillRect(x + 3, y + 3, width - 6,
height - 6);
}
public void paint(Graphics g) {
g.setColor(Color.GRAY);
draw3D(g, 10, 5, 80, 40, true, false);
draw3D(g, 130, 5, 80, 40, false, false);
draw3D(g, 10, 55, 80, 40, true, true);
draw3D(g, 130, 55, 80, 40, false, true);
}
}
. 11.3.
java.awt , : Color, Font, Image, Shape, Canvas .. , java.awt.geom, java.awt.color, java.awt.image .
/* # 3: : BuildShape.java */
package chapt11;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.*;
import javax.swing.JApplet;
public class BuildShape extends JApplet {
public void init() {
setSize(200, 205);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
setBackground(Color.LIGHT_GRAY);
g2.rotate(Math.PI / 6);
drawChessBoard(g);
//
g2.rotate(-Math.PI / 6);
g.setXORMode(new Color(200, 255, 250));
Shape e = new Ellipse2D.Float(70, 75, 70, 50);
//
g2.fill(e);
}
//
public void drawChessBoard(Graphics g) {
int size = 16;
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
if ((x + y) % 2 == 0)
g.setColor(Color.BLUE);
Else
g.setColor(Color.WHITE);
g.fillRect(75 + x * size, y * size - 25, size, size);
}
g.setColor(Color.BLACK);
g.drawString(new Character(
(char) ('8' - y)).toString(), 66, y * size - 13);
g.drawString(new Character(
(char) (y + 'a')).toString(),
79 + y * size, 8 * size - 14);
}
}
}
. 11.4.
// # 4: GIF-: DrawImage.java
package chapt11;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JApplet;
public class DrawImage extends JApplet {
private Image img;
public void init() {
//
img = getImage(getCodeBase(), "joli.gif");
}
public void paint(Graphics g){
g.drawImage(img, 0, 0, this);
}
}
<applet> HTML- . HTML- :
<html><head><title> </title></head>
<body>
<applet code=test.com.ReadParam.class
width=250 height=300>
<param name = bNumber value = 4>
<param name = state value = true>
</applet> </body> </html>
bNumber state :
/* # 5: : ReadParam.java */
package chapt11;
import java.awt.Color;
import java.awt.Graphics;
import java.applet.Applet;
public class ReadParam extends Applet {
private int bNum;
private boolean state;
public void start() { //
String param;
param = getParameter("state");
if (param!= null)
state = new Boolean(param);
try {
param = getParameter("bNumber");
if (param!= null)
bNum = Integer.parseInt(param);
} catch (NumberFormatException e) {
bNum = 0;
state = false;
}
}
public void paint(Graphics g) {
double d = 0;
if (state) d = Math.pow(bNum, 2);
else g.drawString("Error Parameter", 0, 11);
|
|
g.drawString("Statement: " + state, 0, 28);
g.drawString("Value b: " + bNum, 0, 45);
g.drawString("b power 2: " + d, 0, 62);
}
}
. 11.5.
, getParameter() null.
javax.swing, Java . - JButton, JCheckBox, JDialog, JMenu, JComboBox, JMenu, JTextField, JTextArea . , , ( Container), . getContentPane().
// # 6: : MyJApplet.java
package chapt11;
import java.awt.Container;
import javax.swing.JApplet;
import javax.swing.JLabel;
public class MyJApplet extends JApplet {
private JLabel lbl = new JLabel("Swing-applet!");
public void init() {
Container c = getContentPane();
c.add(lbl);
}
}
. 11.6.
JLabel . JLabel String . init() . add() Container, . add() , AWT. Swing , " " ContentPane, getContentPane() JApplet , add().
// # 7: : DemoLC.java
package chapt11;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
public class DemoLC extends JApplet {
private int starX[] =
{ 112, 87, 6, 71, 47, 112, 176, 151, 215, 136 };
private int starY[] =
{ 0, 76, 76, 124, 200, 152, 200, 124, 76, 76 };
private int i;
private Color c;
public void init() {
c = new Color(0, 0, 255);
setBackground(Color.LIGHT_GRAY);
i = 1;
}
public void start() {
int j = i * 25;
if (j < 255)
c = new Color(j, j, 255 - j);
else i = 1;
}
public void paint(Graphics g) {
g.setColor(c);
g.fillPolygon(starX, starY, starX.length);
g.setColor(Color.BLACK);
g.drawPolygon(starX, starY, starX.length);
}
public void stop() {
i++;
}
}
. 11.7.
, , . getAvailableFontFamilyNames() GraphicsEnvironment. , , .
// # 8: : Fonts.java
package chapt11;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import javax.swing.JApplet;
public class Fonts extends JApplet {
private String[] fonts;
public void init() {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
fonts = ge.getAvailableFontFamilyNames();
setSize(700, 400);
}
public void paint(Graphics g) {
int xSize = getWidth() / 170;
for (int i = 0; i < fonts.length; i++) {
g.setFont(new Font(
|
|
fonts[i], Font.PLAIN, 12)); // , ,
g.drawString(fonts[i],
170 * (i % xSize), 13 * (i / xSize + 1));
}
}
}
. 11.8.
Java ( ) . , java.awt.Frame jawax.swing.JFrame, . Frame JFrame, GUI (Graphics User Interface). Frame Component, Container Window. JFrame Swing Frame.
main() Frame JFrame.
/* # 9: : FrameDemo.java */
package chapt11;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
public class FrameDemo extends JFrame {
private String msg = "My Windows-Application";
public void paint(Graphics g) {
int diam = 230;
drawSphere(g, diam);
g.drawString(msg, 59, diam + 52);
g.drawLine(59, diam + 56, 190, diam + 56);
}
public void drawSphere(Graphics g, int diam) {
int r = diam / 2;
int alpha = 0;
int k = 20;
for (int i = 0; i < 4; i++) {
int width = (int) (r * Math.cos(Math.PI / 180 * alpha));
int height = (int) (r * Math.sin(Math.PI / 180 * alpha));
g.setColor(Color.MAGENTA);
g.drawArc(10 + r - width, r + height + i * 10,
2 * width, 80 - i * 20, 0, 180);
g.drawArc(10 + r - width, r - height + i * 10,
2 * width, 80 - i * 20, 0, 180);
g.setColor(Color.BLACK);
g.drawArc(10 + r - width, r + height + i * 10,
2 * width, 80 - i * 20, 0, -180);
g.drawArc(10 + r - width, r - height + i * 10,
2 * width, 80 - i * 20, 0, -180);
alpha += k;
k -= 1;
}
for (int i = 0; i < 4; i++) {
k = (i * i * 17);
g.drawOval(10 + k / 2, 40, diam - k, diam);
}
}
public static void main(String[] args) {
FrameDemo fr = new FrameDemo();
fr.setBackground(Color.LIGHT_GRAY);
// . !
fr.setSize(new Dimension(250, 300));
//
fr.setTitle("Windows-Application");
// . !
fr.setVisible(true);
// - paint()
fr.repaint();
}
}
. 11.9.
main() repaint(). , .
11
.
1. Point Line. n Point. Line , Point . Line Point.
2. Point Line. n Point , .
3. Triangle Point. n Point, , , , .
4. Rectangle Point. n Point. , , , .
5. AnyFigure . , , .
6. Line , A(x1, y1) B(x2, y2). Line. , , , . .
7. Triangle NAngle. ,
m- n- :
.
8. ( ) . .
9. , . .
10. , .
11. , . .
|
|
12. () ,
. .
13. () ,
, .
14. , .
15. , .
16. , .
11
11.1.
:
<applet code=MyApplet.class width=200 height=200>
<param name=count value=5>
</applet>
count i?
1) int i = new
Integer(getParameter("count")).intValue();
2) int i = getIntParameter("count");
3) int i = getParameter("count");
4) int i = new Integer(
getIntParameter("count")).intValue();
5) int i = new Integer(getParameter("count"));.
11.2.
show() . , ?
1) setbgcolor();
2) draw();
3) start();
4) repaint();
5) setColor().
11.3.
Container?
1) Window;
2) List;
3) Choice;
4) Component;
5) Panel;
6) Applet;
7) MenuComponent.
11.4.
Container add()? ( )
1) Button;
2) CheckboxMenuItem;
3) Menu;
4) Canvas.