📄 graphics 技术.txt
字号:
Graphics 技术 List.1 GraphicsApp/GraphicsApp.java
Graphics 技术 List.2 Gradient/Gradient.java
Graphics 技术 List.3 FontDemo/FontDemo.java
Graphics 技术 List.4 ShowPic/ShowPic.java
Graphics 技术 List.5 Offscreen/Offscreen.java
Graphics 技术 List.6 Filter/Filter.java
Graphics 技术 List.7 SwingPic/SwingPic.java
Graphics 技术 List.8 Animation/Animation.java
--------------------------------------------------------------------------------
Graphics 技术 List.1 GraphicsApp/GraphicsApp.java
Return to top
001: import javax.swing.*;
002: import java.awt.*;
003: import java.awt.event.*;
004:
005: public class GraphicsApp extends JFrame {
006:
007: // Constructor
008: public GraphicsApp() {
009: // Select local system look and feel
010: try {
011: UIManager.setLookAndFeel(
012: UIManager.getCrossPlatformLookAndFeelClassName());
013: } catch (Exception e) { }
014: // End program when window closes
015: addWindowListener(new WindowAdapter() {
016: public void windowClosing(WindowEvent e) {
017: System.exit(0);
018: }
019: });
020: }
021:
022: public void paint(Graphics g) {
023: // Get window size
024: Rectangle r = getBounds(null);
025: // Paint background yellow
026: g.setColor(Color.yellow);
027: g.fillRect(0, 0, r.width, r.height);
028: // Outline window in black
029: g.setColor(Color.black);
030: g.drawRect(0, 0, r.width, r.height);
031: // Draw grid inside window
032: for (int h = 0; h < r.height; h += 10)
033: g.drawLine(0, h, r.width, h);
034: for (int v = 0; v < r.width; v += 10)
035: g.drawLine(v, 0, v, r.height);
036: // Draw overlapping round rectangles
037: int cx = r.width / 8;
038: int cy = r.height / 3;
039: int w = (r.width / 4) * 3;
040: int h = cy;
041: g.setColor(Color.gray);
042: g.fillRoundRect(cx - 4, cy - 4, w, h, 10, 10);
043: g.setColor(Color.blue);
044: g.fillRoundRect(cx + 4, cy + 4, w, h, 10, 10);
045: // Draw text inside outer rectangle
046: Font f = new Font("TimesRoman",
047: Font.BOLD + Font.ITALIC, 24);
048: g.setFont(f);
049: g.setColor(Color.orange);
050: g.drawString("
Java学习源代码检索系统", cx + 25, cy + 36);
051: g.drawString("Graphics Demonstration", cx + 35, cy + 66);
052: }
053:
054: public static void main(String[] args) {
055: GraphicsApp app = new GraphicsApp();
056: app.setTitle("Graphics Demonstration (application)");
057: app.setSize(450, 280);
058: app.show();
059: }
060: }
Return to top
--------------------------------------------------------------------------------
Graphics 技术 List.2 Gradient/Gradient.java
Return to top
001: import javax.swing.*;
002: import java.awt.*;
003: import java.awt.event.*;
004:
005: public class Gradient extends JFrame {
006:
007: // Constructor
008: public Gradient() {
009: // Select local system look and feel
010: try {
011: UIManager.setLookAndFeel(
012: UIManager.getCrossPlatformLookAndFeelClassName());
013: } catch (Exception e) { }
014: // End program when window closes
015: addWindowListener(new WindowAdapter() {
016: public void windowClosing(WindowEvent e) {
017: System.exit(0);
018: }
019: });
020: }
021:
022: public void paint(Graphics g) {
023: int increment = 40;
024: Rectangle r = getBounds(null);
025: Color c = new Color(50, 255, 50);
026: int x = 0;
027: while (x < r.width) {
028: g.setColor(c);
029: g.fillRect(x, 0, x + increment, r.height);
030: c = c.darker();
031: x += increment;
032: }
033: }
034:
035: public static void main(String[] args) {
036: Gradient app = new Gradient();
037: app.setTitle("Gradient Color Demonstration");
038: app.setSize(320, 240);
039: app.show();
040: }
041: }
Return to top
--------------------------------------------------------------------------------
Graphics 技术 List.3 FontDemo/FontDemo.java
Return to top
001: import javax.swing.*;
002: import javax.swing.event.*;
003: import java.awt.*;
004: import java.awt.event.*;
005:
006: // Window frame class for showing selected font sample
007: class FontSample extends JFrame {
008: Font font; // Currently shown font
009: String text; // Sample text to display
010:
011: // Constructor
012: public FontSample() {
013: super();
014: font = null;
015: text = "Abcdefg 1234567890 !@#$%^&*()";
016: setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
017: setSize(425, 120);
018: }
019:
020: // Called before showing window
021: public void changeFont(Font f) {
022: font = f.deriveFont(24.0f); // Resize to 24 pts
023: setTitle(font.getFontName()); // Title = font name
024: if (isShowing()) repaint(); // Repaint if already visible
025: }
026:
027: // Paint sample text using current font in window
028: public void paint(Graphics g) {
029: Rectangle r = getBounds(null);
030: g.setColor(Color.white); // Erase background to white
031: g.fillRect(0, 0, r.width, r.height);
032: if (font != null) {
033: g.setFont(font);
034: g.setColor(Color.black);
035: g.drawString(text, 10, r.height / 2);
036: }
037: }
038: }
039:
040: // Main program class
041: public class FontDemo extends JFrame {
042: final protected Font[] fonts; // Array of fonts
043: final protected FontSample fontSample; // Sample window
044:
045: // Constructor
046: public FontDemo() {
047: super();
048: // Select local system look and feel
049: try {
050: UIManager.setLookAndFeel(
051: UIManager.getSystemLookAndFeelClassName());
052: } catch (Exception e) { }
053: // End program when window closes
054: addWindowListener(new WindowAdapter() {
055: public void windowClosing(WindowEvent e) {
056: System.exit(0);
057: }
058: });
059:
060: // Create child sample font window
061: fontSample = new FontSample();
062:
063: // Loading fonts may take a while; tell user
064: System.out.print("Loading font names...");
065:
066: // Get available fonts in 1pt sizes
067: GraphicsEnvironment ge =
068: GraphicsEnvironment.getLocalGraphicsEnvironment();
069: fonts = ge.getAllFonts();
070:
071: // Create a JComboBox object for listing font names
072: JComboBox fontBox = new JComboBox();
073: for (int i = 0; i < fonts.length; i++)
074: fontBox.addItem(fonts[i].getFontName());
075: fontBox.setEditable(false);
076:
077: // Respond to item selection
078: fontBox.addActionListener(
079: new ActionListener() {
080: public void actionPerformed(ActionEvent e) {
081: JComboBox box = (JComboBox)e.getSource();
082: int fontIndex = box.getSelectedIndex();
083: fontSample.changeFont(fonts[fontIndex]);
084: fontSample.show();
085: }
086: }
087: );
088:
089: System.out.println("/nSelect font for a sample");
090: Container content = getContentPane();
091: content.setLayout(new FlowLayout());
092: content.add(new JLabel("Available fonts"));
093: content.add(fontBox);
094: }
095:
096: public static void main(String[] args) {
097: FontDemo app = new FontDemo();
098: app.setTitle("Font Demonstration");
099: app.setSize(320, 240);
100: app.show();
101: }
102: }
Return to top
--------------------------------------------------------------------------------
Graphics 技术 List.4 ShowPic/ShowPic.java
Return to top
001: import java.applet.*;
002: import java.awt.*;
003:
004: public class ShowPic extends Applet
005: implements Runnable {
006:
007: // Instance variables
008: Image pic; // GIF image producer
009: int picID; // Arbitrary image ID
010: MediaTracker tracker; // Tracks loading of image
011: Thread loadingThread; // Thread for loading image
012: String filename = "ksc-01pp-0287.jpg"; // File name
013:
014: // Initialize applet
015: public void init() {
016: // Create MediaTracker object
017: tracker = new MediaTracker(this);
018: // Start image loading
019: pic = getImage(getDocumentBase(), filename);
020: picID = 0;
021: tracker.addImage(pic, picID);
022: // Create thread to monitor image loading
023: loadingThread = new Thread(this);
024: loadingThread.start();
025: }
026:
027: // Run loading thread
028: // Allows other processes to run while loading
029: // the image data
030: public void run() {
031: try {
032: tracker.waitForID(picID);
033: } catch (InterruptedException ie) {
034: return;
035: }
036: repaint(); // Cause paint() to draw loaded image
037: }
038:
039: // Paint window contents
040: // Displays loading or error message until
041: // image is ready, then shows image
042: public void paint(Graphics g) {
043: if (tracker.isErrorID(picID))
044: g.drawString("Error loading " + filename, 10, 20);
045: else if (tracker.checkID(picID))
046: g.drawImage(pic, 0, 0, this);
047: else
048: g.drawString("Loading " + filename, 10, 20);
049: }
050: }
Return to top
--------------------------------------------------------------------------------
Graphics 技术 List.5 Offscreen/Offscreen.java
Return to top
001: import java.applet.*;
002: import java.awt.*;
003: import java.util.Random;
004:
005: public class Offscreen extends Applet
006: implements Runnable {
007:
008: // Instance variables
009: Thread drawingThread;
010: Image offscreenImage;
011: Graphics offscreenContext;
012: Random gen;
013: boolean imageReady = false;
014: int imageW, imageH;
015: int numOvals = 100;
016:
017: // Initialize applet
018: public void init() {
019: // Size applet window
020: imageW = 320;
021: imageH = 240;
022: resize(imageW, imageH);
023: // Construct random number generator
024: gen = new Random();
025: // Create offscreen image and Graphics context
026: offscreenImage = createImage(imageW, imageH);
027: offscreenContext = offscreenImage.getGraphics();
028: }
029:
030: // Create and start drawing thread
031: public void start() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -