Java by API/java.awt/Graphics
Содержание
- 1 Graphics: clipRect(int x, int y, int width, int height)
- 2 Graphics: draw3DRect(int x, int y, int width, int height, boolean raised)
- 3 Graphics: drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
- 4 Graphics: drawBytes(byte[] data,int offset,int length,int x,int y)
- 5 Graphics: drawChars(char[] data, int offset, int length, int x, int y)
- 6 Graphics: drawImage(Image img, int x, int y, ImageObserver ob)
- 7 Graphics: drawLine(int x1, int y1, int x2, int y2)
- 8 Graphics: drawOval(int x, int y, int width, int height)
- 9 Graphics: drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
- 10 Graphics: drawPolyline(int[] xPoints,int[] yPoints,int nPoints)
- 11 Graphics: drawRect(int x, int y, int width, int height)
- 12 Graphics: drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)
- 13 Graphics: drawString(String str, int x, int y)
- 14 Graphics: fill3DRect(int x, int y, int width, int height, boolean raised)
- 15 Graphics: fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
- 16 Graphics: fillOval(int x, int y, int width, int height)
- 17 Graphics: fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
- 18 Graphics: fillRect(int x, int y, int width, int height)
- 19 Graphics: fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)
- 20 Graphics: getFontMetrics()
- 21 Graphics: setClip(int x, int y, int width, int height)
- 22 Graphics: setColor(Color c)
- 23 Graphics: setPaintMode()
- 24 Graphics: setXORMode(Color cl)
Graphics: clipRect(int x, int y, int width, int height)
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.setColor (Color.red);
Graphics clippedGraphics = g.create();
clippedGraphics.drawRect (0,0,100,100);
clippedGraphics.clipRect (25, 25, 50, 50);
clippedGraphics.drawLine (0,0,100,100);
clippedGraphics.dispose();
clippedGraphics=null;
g.drawLine (0,100,100,0);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Graphics: draw3DRect(int x, int y, int width, int height, boolean raised)
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.setColor (Color.gray);
g.draw3DRect (25, 10, 50, 75, true);
g.draw3DRect (25, 110, 50, 75, false);
g.fill3DRect (100, 10, 50, 75, true);
g.fill3DRect (100, 110, 50, 75, false);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Graphics: drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
/*
* Output:
*
*/
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.drawArc(25, 25, 120, 120, 45, 270);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: drawBytes(byte[] data,int offset,int length,int x,int y)
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
byte [] barray = { 0x41, 0x42, 0x43 };
g.drawBytes (barray, 0, barray.length, 10, 30);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: drawChars(char[] data, int offset, int length, int x, int y)
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
char [] carray = { "w", "w", "w",".", "j","a","v","a","2","s",".","c","o","m"};
g.drawChars (carray, 0, carray.length, 10, 60);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: drawImage(Image img, int x, int y, ImageObserver ob)
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Image img = createImage();
g.drawImage(img, 20,20,this);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
private Image createImage(){
BufferedImage bufferedImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
g.drawString("www.jexp.ru", 20,20);
return bufferedImage;
}
}
Graphics: drawLine(int x1, int y1, int x2, int y2)
/*
* Output:
*
*/
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.drawLine(25, 25, 120, 120);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: drawOval(int x, int y, int width, int height)
/*
* Output:
*
*/
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.drawOval(25, 25, 120, 120);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
/*
* Output:
*
*/
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int xpoints[] = {25, 145, 25, 145, 25};
int ypoints[] = {25, 25, 145, 145, 25};
int npoints = 5;
g.drawPolygon(xpoints, ypoints, npoints);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: drawPolyline(int[] xPoints,int[] yPoints,int nPoints)
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int[] xs = {25, 75, 125, 85, 125, 75, 25, 65};
int[] ys = {50, 90, 50, 100, 150, 110, 150, 100};
g.drawPolyline(xs, ys, 8);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: drawRect(int x, int y, int width, int height)
/*
* Output:
*
*/
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawRect(10,10,100,100);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.drawRoundRect(25, 50, 100, 100, 25, 50);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: drawString(String str, int x, int y)
/*
* Output:
*
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Dimension d = this.getPreferredSize();
int fontSize = 20;
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
g.setColor(Color.red);
g.drawString("www.jexp.ru", 10, 20);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: fill3DRect(int x, int y, int width, int height, boolean raised)
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.setColor (Color.gray);
g.draw3DRect (25, 10, 50, 75, true);
g.draw3DRect (25, 110, 50, 75, false);
g.fill3DRect (100, 10, 50, 75, true);
g.fill3DRect (100, 110, 50, 75, false);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Graphics: fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setSize(400, 400);
f.add(new Main());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void paint(Graphics g) {
g.setColor (Color.yellow);
g.fillArc (5, 15, 50, 75, 25, 165);
}
}
Graphics: fillOval(int x, int y, int width, int height)
/*
* Output:
*
*/
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.fillOval(25, 25, 120, 120);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
/*
* Output:
*
*/
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int xpoints[] = {25, 145, 25, 145, 25};
int ypoints[] = {25, 25, 145, 145, 25};
int npoints = 5;
g.fillPolygon(xpoints, ypoints, npoints);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: fillRect(int x, int y, int width, int height)
/*
* Output:
*
*/
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.fillRect(0,0,200,200);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.fillRoundRect(150, 50, 100, 100, 50, 25);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: getFontMetrics()
/*
* Output:
*
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int fontSize = 20;
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
FontMetrics fm = g.getFontMetrics();
String s = "www.jexp.ru";
int stringWidth = fm.stringWidth(s);
int w = 200;
int h = 200;
int x = (w - stringWidth) / 2;
int baseline = fm.getMaxAscent() +
(h - (fm.getAscent() + fm.getMaxDecent()))/2;
int ascent = fm.getMaxAscent();
int descent = fm.getMaxDecent();
int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();
g.setColor(Color.white);
g.fillRect(x, baseline-ascent , stringWidth, fontHeight);
g.setColor(Color.gray);
g.drawLine(x, baseline, x+stringWidth, baseline);
g.setColor(Color.red);
g.drawLine(x, baseline+descent, x+stringWidth, baseline+descent);
g.setColor(Color.blue);
g.drawLine(x, baseline-ascent,
x+stringWidth, baseline-ascent);
g.setColor(Color.black);
g.drawString(s, x, baseline);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: setClip(int x, int y, int width, int height)
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
g.drawString(g.getClipBounds().toString(), 10, 30);
g.clipRect(10, 40, getSize().width - 20, getSize().height - 80);
g.fillOval(0, 0, getSize().width, getSize().height);
String newClip = g.getClipBounds().toString();
g.setClip(0, 0, getSize().width, getSize().height);
g.drawString(newClip, 10, getSize().height - 10);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Graphics: setColor(Color c)
/*
* Output:
*
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Dimension d = this.getPreferredSize();
int fontSize = 20;
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
g.setColor(Color.red);
g.drawString("www.jexp.ru", 10, 20);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Graphics: setPaintMode()
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int w = getSize().width;
int midW = w / 2;
g.drawString("XOR Mode", 0, 30);
g.drawOval(7, 37, 50, 50);
g.setXORMode(Color.white);
for (int i = 0; i < 15; i += 3) {
g.drawOval(10 + i, 40 + i, 50, 50);
}
g.setPaintMode();
g.drawString("Paint Mode", midW, 30);
g.drawOval(midW + 7, 37, 50, 50);
for (int i = 0; i < 15; i += 3) {
g.drawOval(midW + 10 + i, 40 + i, 50, 50);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
Graphics: setXORMode(Color cl)
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int w = getSize().width;
int midW = w / 2;
g.drawString("XOR Mode", 0, 30);
g.drawOval(7, 37, 50, 50);
g.setXORMode(Color.white);
for (int i = 0; i < 15; i += 3) {
g.drawOval(10 + i, 40 + i, 50, 50);
}
g.setPaintMode();
g.drawString("Paint Mode", midW, 30);
g.drawOval(midW + 7, 37, 50, 50);
for (int i = 0; i < 15; i += 3) {
g.drawOval(midW + 10 + i, 40 + i, 50, 50);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}