Java Tutorial/2D Graphics/Gradient Paint — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:25, 31 мая 2010
Содержание
- 1 Acyclic Gradient Paint
- 2 A non-cyclic gradient
- 3 Control the direction of Gradients
- 4 Cyclic Gradient Paint
- 5 Drawing with a Gradient Color
- 6 Gradients: a smooth blending of shades from light to dark or from one color to another
- 7 Gradients in the middle
- 8 Horizontal Gradients
- 9 String gradient paint
- 10 Vertical Gradient Paint
Acyclic Gradient Paint
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
class GradientPane extends JComponent {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Point2D.Float p1 = new Point2D.Float(150.f, 75.f); // Gradient line start
Point2D.Float p2 = new Point2D.Float(250.f, 75.f); // Gradient line end
float width = 300;
float height = 50;
Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
GradientPaint g2 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, false); // Acyclic
// gradient
rect1.setRect(p1.x - 100, p1.y - 25, width, height);
g2D.setPaint(g2); // Gradient color fill
g2D.fill(rect1); // Fill the rectangle
g2D.setPaint(Color.BLACK); // Outline in black
g2D.draw(rect1); // Fill the rectangle
g2D.draw(new Line2D.Float(p1, p2));
}
}
public class AcyclicGradientPaint {
public static void main(String[] a) {
JFrame window = new JFrame("Acyclic Gradient Paint");
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new GradientPane());
window.setVisible(true);
}
}
A non-cyclic gradient
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class BasicDraw {
public static void main(String[] args) {
new BasicDraw();
}
BasicDraw() {
JFrame frame = new JFrame();
frame.add(new MyComponent());
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class MyComponent extends JComponent {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Color startColor = Color.red;
Color endColor = Color.blue;
int startX = 10, startY = 20, endX = 30, endY = 40;
GradientPaint gradient = new GradientPaint(startX, startY, startColor, endX, endY, endColor);
g2d.setPaint(gradient);
g2d.draw(new Rectangle(20,20,200,200));
}
}
Control the direction of Gradients
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GradientsDirection extends JPanel {
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gp1 = new GradientPaint(5, 25, Color.yellow, 20, 2, Color.black, true);
g2d.setPaint(gp1);
g2d.fillRect(20, 80, 300, 40);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GradientsDirection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GradientsDirection());
frame.setSize(350, 350);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Cyclic Gradient Paint
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
class GradientPane extends JComponent {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Point2D.Float p1 = new Point2D.Float(150.f, 75.f); // Gradient line start
Point2D.Float p2 = new Point2D.Float(250.f, 75.f); // Gradient line end
float width = 300;
float height = 50;
GradientPaint g1 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, true); // Cyclic
// gradient
Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
g2D.setPaint(g1); // Gradient color fill
g2D.fill(rect1); // Fill the rectangle
g2D.setPaint(Color.BLACK); // Outline in black
g2D.draw(rect1); // Fill the rectangle
g2D.draw(new Line2D.Float(p1, p2));
}
}
public class CyclicGradientPaint {
public static void main(String[] a) {
JFrame window = new JFrame("Cyclic Gradient Paint");
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new GradientPane());
window.setVisible(true);
}
}
Drawing with a Gradient Color
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class BasicDraw {
public static void main(String[] args) {
new BasicDraw();
}
BasicDraw() {
JFrame frame = new JFrame();
frame.add(new MyComponent());
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class MyComponent extends JComponent {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Color startColor = Color.red;
Color endColor = Color.blue;
int startX = 10, startY = 20, endX = 30, endY = 40;
GradientPaint gradient = new GradientPaint(startX, startY, startColor, endX, endY, endColor);
g2d.setPaint(gradient);
g2d.draw(new Rectangle(20,20,200,200));
}
}
Gradients: a smooth blending of shades from light to dark or from one color to another
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GradientsRedYellow extends JPanel {
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gp1 = new GradientPaint(5, 5, Color.red, 20, 20, Color.yellow, true);
g2d.setPaint(gp1);
g2d.fillRect(20, 20, 300, 40);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GradientsRedYellow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GradientsRedYellow());
frame.setSize(350, 350);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Gradients in the middle
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GradientsMiddle extends JPanel {
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gp1 = new GradientPaint(0, 0, Color.orange, 0, 20, Color.black, true);
g2d.setPaint(gp1);
g2d.fillRect(20, 260, 300, 40);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GradientsMiddle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GradientsMiddle());
frame.setSize(350, 350);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Horizontal Gradients
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GradientsLine extends JPanel {
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gp1 = new GradientPaint(5, 25, Color.green, 2, 2, Color.black, true);
g2d.setPaint(gp1);
g2d.fillRect(20, 140, 300, 40);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GradientsLine");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GradientsLine());
frame.setSize(350, 350);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
String gradient paint
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class StringGraidentPaint extends JFrame {
public StringGraidentPaint() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public static void main(String arg[]) {
new StringGraidentPaint();
}
}
class DrawingCanvas extends JPanel {
Font font = new Font("Dialog", Font.BOLD, 40);
FontMetrics fontMetrics;
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
fontMetrics = getFontMetrics(font);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
g2D.setFont(font);
GradientPaint gp = new GradientPaint(
30.0f, 50.0f,
Color.blue,
fontMetrics.stringWidth("Hello!"),
fontMetrics.getHeight(),
Color.red);
g2D.setPaint(gp);
g2D.drawString("Hello!", 20,200);
}
}
Vertical Gradient Paint
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GradientsVertical extends JPanel {
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gp1 = new GradientPaint(25, 25, Color.blue, 15, 25, Color.black, true);
g2d.setPaint(gp1);
g2d.fillRect(20, 200, 300, 40);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GradientsVertical");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GradientsVertical());
frame.setSize(350, 350);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}