Professional Documents
Culture Documents
Circle
import javax.swing.*;
import java.awt.*;
public class CircleExample extends JPanel {
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int centerX = WIDTH / 2;
int centerY = HEIGHT / 2;
int radius = 150;
int x = 0;
int y = radius;
int d = 3 - 2 * radius;
g2d.setColor(Color.BLACK);
while (x <= y) {
drawCirclePoints(centerX, centerY, x, y, g2d);
x++;
if (d < 0) {
d = d + 4 * x + 6;
} else {
y--;
d = d + 4 * (x - y) + 10;
}
import java.awt.*;
@Override
super.paintComponent(g);
g.setColor(rgbColor);
g.setColor(argbColor);
g.setColor(hsbColor);
SwingUtilities.invokeLater(() -> {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 100);
frame.add(new ColorModelExample());
frame.setVisible(true);
});
OUTPUT:
3. DDA
import javax.swing.*;
import java.awt.*;
import java.sql.SQLOutput;
import java.util.Scanner;
@Override
public void paint(Graphics g) {
int dx, dy;
float Xinc, Yinc, x, y, steps;
dy = y1 - y0;
dx = x1 - x0;
g.fillOval(x0, y0, 10, 10);
if (dy > dx) {
steps = Math.abs(dy);
} else {
steps = Math.abs(dx);
}
x = x0;
y = y0;
Yinc = dy / steps;
Xinc = dx / steps;
while (steps != 0) {
steps--;
x += Xinc;
y += Yinc;
g.fillOval(Math.round(x), Math.round(y), 10, 10);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter First X0 and Y0");
int x0 = sc.nextInt();
int y0 = sc.nextInt();
System.out.println("Enter the last end x1 and y1");
int x1 = sc.nextInt();
int y1 = sc.nextInt();
DDA d = new DDA(x0, y0, x1, y1);
JFrame f = new JFrame();
f.add(d);
f.setSize(800, 800);
f.setVisible(true);
}
}
OUTPUT:
4. Double Buffering
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public DoubleBufferingExample() {
setTitle("Double Buffering Example");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
private void update() {
// Update the position of the shape (in this case, we'll just move it to
the
// right)
x = (x + 1) % WIDTH; }
private class MyPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
@Override
public void paint(Graphics g) {
int x,y,p;
x=0;
y=r;
fill(g,x,y,xc,yc);
p=1-r;
while(x<y)
{
x+=1;
if(p<0)
{
p+=2*x+1;
}
else
{
y-=1;
p+=2*x+1-2*y;
}
fill(g,x,y,xc,yc);
}
}
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 20, 20, this);
}
}
OUTPUT:
7. GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Output:
8. Line Styles
import javax.swing.*;
import java.awt.*;
public LineStylesExample() {
setTitle("Java 2D Line Styles");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(new LineStylesPanel());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(dashedStroke);
g2d.drawLine(startX, startY + 50, endX, startY + 50);
g2d.setStroke(dottedStroke);
g2d.drawLine(startX, startY + 100, endX, startY + 100);
g2d.setStroke(dashedDottedStroke);
g2d.drawLine(startX, startY + 150, endX, startY + 150);
}
}
OUTPUT:
9. Oval
import javax.swing.*;
import java.awt.*;
public OvalExample() {
setTitle("Java Oval Example");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(new OvalPanel());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int x = 100;
int y = 100;
int width = 200;
int height = 150;
import javax.swing.*;
import java.awt.*;
public RectangleExample() {
setTitle("Java Rectangle Example");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(new RectanglePanel());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int x = 100;
int y = 100;
int width = 200;
int height = 150;
import javax.swing.*;
import java.awt.*;
g2d.dispose();
}
super.paintComponent(g);
doDrawing(g);
}
}
public RotationEx() {
initUI();
}
add(new Surface());
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Original object
g2d.setColor(Color.black);
g2d.drawPolygon(xPoints, yPoints, numPoints);
// Rotation
double pivotX = 200;
double pivotY = 200;
double rotationAngle = Math.toRadians(45);
// Rotated object
g2d.setColor(Color.black);
g2d.drawPolygon(rotatedXPoints, rotatedYPoints, numPoints);
}
import javax.swing.*;
import java.awt.geom.AffineTransform;
import java.awt.*;
g2d.setTransform(tx1);
g2d.fillRect(0, 0, 80, 50);
g2d.setTransform(tx2);
g2d.fillRect(0, 0, 80, 50);
g2d.dispose();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public ScalingEx() {
initUI();
}
add(new Surfacee());
setTitle("Scaling");
setSize(330, 160);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
import javax.swing.*;
import java.awt.*;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Set color
g2d.setColor(Color.BLACK);
OUTPUT:
15. Texture
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public TextureExample() {
// Create the texture
int textureSize = 20;
texture = new BufferedImage(textureSize, textureSize,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = texture.createGraphics();
g2d.setColor(Color.YELLOW);
g2d.fillRect(0, 0, textureSize, textureSize);
g2d.setColor(Color.BLACK);
g2d.drawRect(0, 0, textureSize - 1, textureSize - 1);
g2d.dispose();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
OUTPUT:
16. Thick Line
import javax.swing.*;
import java.awt.*;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
import javax.swing.*;
import java.awt.*;
g2d.dispose();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public TranslationEx() {
initUI();
}
setTitle("Translation");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {