本文整理了Java中java.awt.Graphics
类的一些代码示例,展示了Graphics
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graphics
类的具体详情如下:
包路径:java.awt.Graphics
类名称:Graphics
[英]The Graphics
class is the abstract base class for all graphics contexts that allow an application to draw onto components that are realized on various devices, as well as onto off-screen images.
A Graphics
object encapsulates state information needed for the basic rendering operations that Java supports. This state information includes the following properties:
Component
object on which to draw.Coordinates are infinitely thin and lie between the pixels of the output device. Operations that draw the outline of a figure operate by traversing an infinitely thin path between pixels with a pixel-sized pen that hangs down and to the right of the anchor point on the path. Operations that fill a figure operate by filling the interior of that infinitely thin path. Operations that render horizontal text render the ascending portion of character glyphs entirely above the baseline coordinate.
The graphics pen hangs down and to the right from the path it traverses. This has the following implications:
All coordinates that appear as arguments to the methods of this Graphics
object are considered relative to the translation origin of this Graphics
object prior to the invocation of the method.
All rendering operations modify only pixels which lie within the area bounded by the current clip, which is specified by a Shape in user space and is controlled by the program using the Graphics
object. This user clip is transformed into device space and combined with the device clip, which is defined by the visibility of windows and device extents. The combination of the user clip and device clip defines the composite clip, which determines the final clipping region. The user clip cannot be modified by the rendering system to reflect the resulting composite clip. The user clip can only be changed through the setClip
or clipRect
methods. All drawing or writing is done in the current color, using the current paint mode, and in the current font.
[中]Graphics
类是所有图形上下文的抽象基类,允许应用程序绘制在各种设备上实现的组件以及屏幕外图像上。Graphics
对象封装Java支持的基本呈现操作所需的状态信息。此状态信息包括以下属性:
*要在其上绘制的Component
对象。
*用于渲染和剪裁坐标的平移原点。
*当前剪辑。
*当前颜色。
*当前字体。
*当前逻辑像素操作函数(XOR或Paint)。
*当前的异或交替颜色(请参见图形#setXORMode)。
坐标无限薄,位于输出设备的像素之间。绘制图形轮廓的操作通过使用悬挂在路径上定位点右侧的像素大小的笔遍历像素之间的无限细路径来进行。填充图形的操作通过填充无限细路径的内部来操作。渲染水平文本的操作会将字符图示符的升序部分完全渲染到基线坐标之上。
图形笔从它所穿过的路径向右下垂。这有以下影响:
*如果绘制一个覆盖给定矩形的图形,则与填充以该矩形为边界的图形相比,该图形在右边缘和下边缘多占用一行像素。
*如果沿与文本行基线相同的y坐标绘制水平线,则该线将完全绘制在文本下方,但任何下行线除外。
在调用该方法之前,作为该Graphics
对象的方法参数出现的所有坐标都被视为相对于该Graphics
对象的转换原点。
所有渲染操作仅修改位于当前剪辑所限定区域内的像素,该区域由用户空间中的形状指定,并由使用Graphics
对象的程序控制。此用户剪辑将转换为设备空间并与设备剪辑组合,设备剪辑由窗口和设备范围的可见性定义。用户剪辑和设备剪辑的组合定义了复合剪辑,从而确定最终剪辑区域。渲染系统无法修改用户剪辑以反映生成的合成剪辑。只能通过setClip
或clipRect
方法更改用户剪辑。所有绘图或书写均以当前颜色、当前绘制模式和当前字体完成。
代码示例来源:origin: loklak/loklak_server
/**
* test image generator
*/
private static RenderedImage generateTestImage(int width, int height, Random r, double angle) {
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.white); g.fillRect(0, 0, width, height); g.setColor(Color.BLUE);
int x = width / 2;
int y = height / 2;
int radius = Math.min(x, y);
g.drawLine(x, y, x + (int) (radius * Math.cos(angle)), y + (int) (radius * Math.sin(angle)));
g.drawString("giftest", r.nextInt(width), r.nextInt(height));
return img;
}
代码示例来源:origin: stackoverflow.com
BufferedImage otherImage = // .. created somehow
BufferedImage newImage = new BufferedImage(SMALL_SIZE, SMALL_SIZE, BufferedImage.TYPE_INT_RGB);
Graphics g = newImage.createGraphics();
g.drawImage(otherImage, 0, 0, SMALL_SIZE, SMALL_SIZE, null);
g.dispose();
代码示例来源:origin: wildfly/wildfly
public void drawTopology(Graphics g) {
int x=20, y=50;
String label;
Dimension box=getSize();
Color old=g.getColor();
if(coordinator) {
g.setColor(Color.cyan);
g.fillRect(11, 31, box.width - 21, box.height - 61);
g.setColor(old);
}
g.drawRect(10, 30, box.width - 20, box.height - 60);
g.setFont(myFont);
for(int i=0; i < members.size(); i++) {
label=members.get(i).toString();
drawNode(g, x, y, label, NormalStyle);
y+=50;
}
}
代码示例来源:origin: libgdx/libgdx
static Icon getColorIcon (java.awt.Color color) {
BufferedImage image = new BufferedImage(32, 16, BufferedImage.TYPE_INT_RGB);
java.awt.Graphics g = image.getGraphics();
g.setColor(color);
g.fillRect(1, 1, 30, 14);
g.setColor(java.awt.Color.black);
g.drawRect(0, 0, 31, 15);
return new ImageIcon(image);
}
代码示例来源:origin: runelite/runelite
/**
* Overrides the painting of the bar's track (the darker part underneath that extends
* the full page length).
*/
@Override
protected void paintTrack(Graphics graphics, JComponent jComponent, Rectangle rectangle)
{
graphics.setColor(trackColor);
graphics.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
代码示例来源:origin: shuzheng/zheng
private void shearY(Graphics g, int w1, int h1, Color color) {
int period = random.nextInt(40) + 10; // 50;
boolean borderGap = true;
int frames = 20;
int phase = 7;
for (int i = 0; i < w1; i++) {
double d = (double) (period >> 1)
* Math.sin((double) i / (double) period
+ (6.2831853071795862D * (double) phase)
/ (double) frames);
g.copyArea(i, 0, 1, h1, 0, (int) d);
if (borderGap) {
g.setColor(color);
g.drawLine(i, (int) d, i, 0);
g.drawLine(i, (int) d + h1, i, h1);
}
}
}
代码示例来源:origin: stackoverflow.com
long start = System.nanoTime();
super.paintComponent(g);
int w = this.getWidth();
int h = this.getHeight();
g.drawImage(background, 0, 0, this);
double theta = 2 * Math.PI * index++ / 64;
g.setColor(Color.blue);
rect.setRect(
(int) (Math.sin(theta) * w / 3 + w / 2 - RADIUS),
(int) (Math.cos(theta) * h / 3 + h / 2 - RADIUS),
2 * RADIUS, 2 * RADIUS);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
g.setColor(Color.white);
if (frameCount == FRAMES) {
averageTime = totalTime / FRAMES;
g.drawString(s, 5, 16);
background = gc.createCompatibleImage(w, h, Transparency.OPAQUE);
Graphics2D g = background.createGraphics();
g.clearRect(0, 0, w, h);
g.setColor(Color.green.darker());
for (int i = 0; i < 128; i++) {
g.drawLine(w / 2, h / 2, r.nextInt(w), r.nextInt(h));
g.dispose();
System.out.println("Resized to " + w + " x " + h);
代码示例来源:origin: stackoverflow.com
g2.setColor(Color.WHITE);
g2.fillRect(padding + labelPadding, padding, getWidth() - (2 * padding) - labelPadding, getHeight() - 2 * padding - labelPadding);
g2.setColor(Color.BLACK);
int y1 = y0;
if (scores.size() > 0) {
g2.setColor(gridColor);
g2.drawLine(padding + labelPadding + 1 + pointWidth, y0, getWidth() - padding, y1);
g2.setColor(Color.BLACK);
String yLabel = ((int) ((getMinScore() + (getMaxScore() - getMinScore()) * ((i * 1.0) / numberYDivisions)) * 100)) / 100.0 + "";
FontMetrics metrics = g2.getFontMetrics();
int labelWidth = metrics.stringWidth(yLabel);
g2.drawString(yLabel, x0 - labelWidth - 5, y0 + (metrics.getHeight() / 2) - 3);
g2.drawLine(x0, y0, x1, y1);
int y1 = y0 - pointWidth;
if ((i % ((int) ((scores.size() / 20.0)) + 1)) == 0) {
g2.setColor(gridColor);
g2.drawLine(x0, getHeight() - padding - labelPadding - 1 - pointWidth, x1, padding);
g2.setColor(Color.BLACK);
String xLabel = i + "";
FontMetrics metrics = g2.getFontMetrics();
int labelWidth = metrics.stringWidth(xLabel);
g2.drawString(xLabel, x0 - labelWidth / 2, y0 + metrics.getHeight() + 3);
g2.drawLine(x0, y0, x1, y1);
g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, padding + labelPadding, padding);
代码示例来源:origin: stackoverflow.com
private JFrame frame = new JFrame("sssssssss");
private JButton tip1Null = new JButton(" test button ");
final Border compound2;
compound2 = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl2));
tip1Null.setFont(new Font("Serif", Font.BOLD, 14));
tip1Null.setForeground(Color.darkGray);
tip1Null.setPreferredSize(new Dimension(50, 30));
tip1Null.addActionListener(new java.awt.event.ActionListener() {
@Override
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tip1Null, BorderLayout.CENTER);
frame.setLocation(150, 150);
frame.setPreferredSize(new Dimension(310, 75));
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
((Graphics2D) g).setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
g.drawRoundRect(x, y, width, height, MARGIN, MARGIN);
paintText(g, b, b.getBounds(), b.getText());
g.setColor(Color.red.brighter());
g.fillRect(0, 0, b.getSize().width, b.getSize().height);
代码示例来源:origin: stackoverflow.com
frame.pack();
frame.setVisible(true);
this.setBackground(Color.lightGray);
this.setPreferredSize(new Dimension(
image.getWidth(null), image.getHeight(null)));
this.addMouseListener(new MouseAdapter() {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
g2d.rotate(theta);
g2d.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
g2d.drawImage(image, 0, 0, null);
public void actionPerformed(ActionEvent e) {
theta += dt;
repaint();
return new Dimension(SIZE, SIZE);
g2d.setPaint(Color.getHSBColor(r.nextFloat(), 1, 1));
g2d.setStroke(new BasicStroke(size / 8));
g2d.drawLine(0, size / 2, size, size / 2);
g2d.drawLine(size / 2, 0, size / 2, size);
g2d.dispose();
return bi;
代码示例来源:origin: stackoverflow.com
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, BORDER_GAP, BORDER_GAP);
g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, getWidth() - BORDER_GAP, getHeight() - BORDER_GAP);
int y0 = getHeight() - (((i + 1) * (getHeight() - BORDER_GAP * 2)) / Y_HATCH_CNT + BORDER_GAP);
int y1 = y0;
g2.drawLine(x0, y0, x1, y1);
int y0 = getHeight() - BORDER_GAP;
int y1 = y0 - GRAPH_POINT_WIDTH;
g2.drawLine(x0, y0, x1, y1);
g2.setColor(GRAPH_COLOR);
g2.setStroke(GRAPH_STROKE);
for (int i = 0; i < graphPoints.size() - 1; i++) {
int x2 = graphPoints.get(i + 1).x;
int y2 = graphPoints.get(i + 1).y;
g2.drawLine(x1, y1, x2, y2);
g2.setColor(GRAPH_POINT_COLOR);
for (int i = 0; i < graphPoints.size(); i++) {
int x = graphPoints.get(i).x - GRAPH_POINT_WIDTH / 2;
int ovalW = GRAPH_POINT_WIDTH;
int ovalH = GRAPH_POINT_WIDTH;
g2.fillOval(x, y, ovalW, ovalH);
代码示例来源:origin: stackoverflow.com
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
return new Dimension(PREF_W, PREF_H);
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(STROKE);
g.setColor(POINTS_COLOR);
for (List<Point> pointList : pointsList) {
if (pointList.size() > 1) {
int x2 = p2.x;
int y2 = p2.y;
g.drawLine(x1, y1, x2, y2);
p1 = p2;
g.setColor(CURRENT_POINTS_COLOR);
if (currentPointList != null && currentPointList.size() > 1) {
Point p1 = currentPointList.get(0);
int x2 = p2.x;
int y2 = p2.y;
g.drawLine(x1, y1, x2, y2);
p1 = p2;
currentPointList = new ArrayList<Point>();
currentPointList.add(mEvt.getPoint());
repaint();
代码示例来源:origin: stackoverflow.com
Point point = e.getPoint();
int width = getWidth();
int height = getHeight();
repaint();
addMouseMotionListener(mouseHandler);
return new Dimension(200, 200);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int width = getWidth();
int height = getHeight();
g2d.setColor(Color.BLUE);
g2d.fill(cell);
g2d.setColor(Color.GRAY);
for (Rectangle cell : cells) {
g2d.draw(cell);
g2d.dispose();
代码示例来源:origin: stackoverflow.com
repaint();
repaint();
super.paintComponent(g);
for (Line line : lines) {
g.setColor(line.color);
g.drawLine(line.x1, line.y1, line.x2, line.y2);
JFrame testFrame = new JFrame();
testFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final LinesComponent comp = new LinesComponent();
comp.setPreferredSize(new Dimension(320, 200));
testFrame.getContentPane().add(comp, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel();
JButton newLineButton = new JButton("New Line");
JButton clearButton = new JButton("Clear");
buttonsPanel.add(newLineButton);
buttonsPanel.add(clearButton);
testFrame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
newLineButton.addActionListener(new ActionListener() {
testFrame.pack();
testFrame.setVisible(true);
代码示例来源:origin: stackoverflow.com
return new Dimension(image.getWidth(), image.getHeight());
w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, null);
g2d.setPaint(Color.red);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
String s = "Hello, world!";
FontMetrics fm = g2d.getFontMetrics();
int x = img.getWidth() - fm.stringWidth(s) - 5;
int y = fm.getHeight();
g2d.drawString(s, x, y);
g2d.dispose();
return img;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TextOverlay());
f.pack();
f.setVisible(true);
代码示例来源:origin: stackoverflow.com
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
plafComponents.setBorder(
new TitledBorder("FlowLayout(FlowLayout.RIGHT, 3,3)") );
plafComponents.add(plafChooser);
plafComponents.add(pack);
gui.add(plafComponents, BorderLayout.NORTH);
20f,20f,Color.red, 180f,180f,Color.yellow);
g.setPaint(gp);
g.fillRect(0,0,200,200);
ImageIcon ii = new ImageIcon(bi);
JLabel imageLabel = new JLabel(ii);
gui.add( splitPane, BorderLayout.CENTER );
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
代码示例来源:origin: stackoverflow.com
this.setPreferredSize(new Dimension(640, 480));
this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
g2d.setStroke(new BasicStroke(8,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
g.drawLine(p1.x, p1.y, p2.x, p2.y);
p1 = e.getPoint();
p2 = p1;
repaint();
this.add(new MoveButton("\u2190", KeyEvent.VK_LEFT, -DELTA, 0));
this.add(new MoveButton("\u2191", KeyEvent.VK_UP, 0, -DELTA));
this.add(new MoveButton("\u2192", KeyEvent.VK_RIGHT, DELTA, 0));
this.add(new MoveButton("\u2193", KeyEvent.VK_DOWN, 0, DELTA));
ControlPanel.this.getInputMap(WHEN_IN_FOCUSED_WINDOW)
.put(k, k.toString());
ControlPanel.this.getActionMap()
.put(k.toString(), new AbstractAction() {
代码示例来源:origin: stackoverflow.com
+ "The effect we want is a multi-line label.";
JFrame f = new JFrame("Label Render Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Color.orange);
imageGraphics.setPaint(gp);
imageGraphics.fillRect(0, 0, 400, 300);
textLabel.setSize(textLabel.getPreferredSize());
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.setColor(new Color(255, 255, 255, 128));
g.fillRoundRect(
0,
0,
15,
10);
g.setColor(Color.black);
textLabel.paint(g);
Graphics g2 = image.getGraphics();
g2.drawImage(bi, 20, 20, f);
f.setVisible(true);
代码示例来源:origin: loklak/loklak_server
/**
* show the images as stream of JFrame on desktop
*/
public void show() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
JLabel label = null;
while (true) {
for (int i = 0; i < this.frames.size(); i++) {
Frame frame = this.frames.get(i);
if (label == null) {
label = new JLabel(new ImageIcon(frame.image));
f.getContentPane().add(label);
f.pack();
} else {
label.getGraphics().drawImage(frame.image,0,0, label);
}
try {Thread.sleep(frame.delayMillis);} catch (InterruptedException e) {}
}
}
}
}
代码示例来源:origin: stackoverflow.com
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(600, 400);
JPanel panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(0, 0, 100, 100);
}
};
frame.add(panel);
// Graphics g = panel.getGraphics();
// g.setColor(Color.BLUE);
// g.fillRect(0, 0, 100, 100);
frame.validate(); // because you added panel after setVisible was called
frame.repaint(); // because you added panel after setVisible was called
}
}
内容来源于网络,如有侵权,请联系作者删除!