You are on page 1of 39

1.

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;
}

drawCirclePoints(centerX, centerY, x, y, g2d)


}
}
private void drawCirclePoints(int centerX, int centerY, int x, int y, Graphics2D
g2d) {
g2d.drawLine(centerX + x, centerY + y, centerX + x, centerY + y);
g2d.drawLine(centerX - x, centerY + y, centerX - x, centerY + y);
g2d.drawLine(centerX + x, centerY - y, centerX + x, centerY - y);
g2d.drawLine(centerX - x, centerY - y, centerX - x, centerY - y);
g2d.drawLine(centerX + y, centerY + x, centerX + y, centerY + x);
g2d.drawLine(centerX - y, centerY + x, centerX - y, centerY + x);
g2d.drawLine(centerX + y, centerY - x, centerX + y, centerY - x);
g2d.drawLine(centerX - y, centerY - x, centerX - y, centerY - x);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Circle Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.add(new CircleExample());
frame.setVisible(true);
});
}
OUTPUT:
2. Color Model
import javax.swing.*;

import java.awt.*;

public class ColorModelExample extends JPanel {

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

// RGB color model

Color rgbColor = new Color(255, 0, 0);

g.setColor(rgbColor);

g.fillRect(20, 20, 50, 50);

// ARGB color model with transparency

Color argbColor = new Color(100, 0, 255, 0);

g.setColor(argbColor);

g.fillRect(100, 20, 50, 50);

// HSB color model

float hue = 0.33f; // 0.33 corresponds to green hue

float saturation = 1.0f; // full saturation

float brightness = 1.0f; // full brightness

Color hsbColor = Color.getHSBColor(hue, saturation, brightness);

g.setColor(hsbColor);

g.fillRect(180, 20, 50, 50);


public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

JFrame frame = new JFrame("Color Model Example");

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;

public class DDA extends Canvas {


static int x0, y0, x1, y1;

DDA(int x0, int y0, int x1, int y1) {


this.x0 = x0;
this.y0 = y0;
this.x1 = x1;
this.y1 = y1;
}

@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 class DoubleBufferingExample extends JFrame {

private static final int WIDTH = 400;


private static final int HEIGHT = 300;

private MyPanel myPanel;


private int x = 0;

public DoubleBufferingExample() {
setTitle("Double Buffering Example");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

myPanel = new MyPanel();


add(myPanel);

Timer timer = new Timer(16, new ActionListener() {


@Override
public void actionPerformed(ActionEvent e) {
update();
myPanel.repaint();
}
});
timer.start();

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);

// Clear the panel


g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());

// Draw the shape on the panel


g.setColor(Color.RED);
g.fillRect(x, getHeight() / 2 - 25, 50, 50);
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new DoubleBufferingExample();
}
});
}
}
OUTPUT:
5. Drawing Circle Algorithm

import javax.swing.*;
import java.awt.*;
import java.util.Scanner;

public class DrawCircleAlgorithm extends Canvas{


static int xc,yc,r;
DrawCircleAlgorithm(int xc,int yc,int r)
{
this.xc=xc;
this.yc=yc;
this.r=r;
}

@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);
}
}

public void fill (Graphics g,int x,int y,int xc,int yc)


{
g.fillOval(xc+x,yc+y,5,5);
g.fillOval(xc+x,yc-y,5,5);
g.fillOval(xc-x,yc+y,5,5);
g.fillOval(xc-x,yc-y,5,5);
g.fillOval(xc+y,yc+x,5,5);
g.fillOval(xc+y,yc-x,5,5);
g.fillOval(xc-y,yc+x,5,5);
g.fillOval(xc-y,yc-x,5,5);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the center (xc,yc)");
int xc=sc.nextInt();
int yc=sc.nextInt();
System.out.println("Enter Radius :r");
int r=sc.nextInt();
DrawCircleAlgorithm c=new DrawCircleAlgorithm(xc,yc,r);
JFrame f=new JFrame();
f.add(c);
f.setSize(800,800);
f.setVisible(true);
}
}
OUTPUT:
6. Grayscale Image

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class GrayScaleImageExample {


public static void main(String[] args) {
int width = 400;
int height = 400;

BufferedImage image = new BufferedImage(width, height,


BufferedImage.TYPE_BYTE_GRAY);

// Set the intensity values for each pixel


for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int intensity = (x + y) % 256; // Some example intensity
calculation
image.setRGB(x, y, (intensity << 16) | (intensity << 8) |
intensity);
}
}

// Save the grayscale image to disk


File output = new File("grayscale.jpg");
try {
ImageIO.write(image, "jpg", output);
} catch (Exception e) {
e.printStackTrace();
}

// Display the grayscale image in a frame


JFrame frame = new JFrame("Grayscale Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.add(new GrayscaleImagePanel(image));
frame.setVisible(true);
}
}
class GrayscaleImagePanel extends JPanel {
private BufferedImage image;

public GrayscaleImagePanel(BufferedImage image) {


this.image = image;
}

@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;

public class GUI implements ActionListener {


private int count=0;
JLabel label;
JFrame frame;
JPanel panel;
public GUI()
{
frame=new JFrame();
panel=new JPanel();
JButton button=new JButton("Click Me");
button.addActionListener(this);
label=new JLabel("Number of Clicks");
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout(0,1));
panel.add(button);
panel.add(label);
frame.add(panel,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("OUR GUI");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
public void actionPerformed(ActionEvent e)
{
count++;
label.setText("Number of Clicks:"+count);
}
}

Output:
8. Line Styles

import javax.swing.*;
import java.awt.*;

public class LineStylesExample extends JFrame {

public LineStylesExample() {
setTitle("Java 2D Line Styles");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(new LineStylesPanel());
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> new
LineStylesExample().setVisible(true));
}
}

class LineStylesPanel extends JPanel {

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;

int startY = 50;


int endY = 300;
int startX = 50;
int endX = 300;

// Define different stroke styles


BasicStroke solidStroke = new BasicStroke(4.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
BasicStroke dashedStroke = new BasicStroke(4.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0,
new float[] { 10 }, 0);
BasicStroke dottedStroke = new BasicStroke(2.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0,
new float[] { 2, 5 }, 0);
BasicStroke dashedDottedStroke = new BasicStroke(2.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0,
new float[] { 10, 5, 2, 5 }, 0);

// Draw lines with different styles


g2d.setColor(Color.BLACK);
g2d.setStroke(solidStroke);
g2d.drawLine(startX, startY, endX, startY);

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 class OvalExample extends JFrame {

public OvalExample() {
setTitle("Java Oval Example");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(new OvalPanel());
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> new OvalExample().setVisible(true));
}
}

class OvalPanel extends JPanel {

@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;

// Draw an oval outline


g2d.setColor(Color.BLACK);
g2d.drawOval(x, y, width, height);
// Fill the oval with a color
g2d.setColor(Color.GREEN);
g2d.fillOval(x + 1, y + 1, width - 1, height - 1);
}
}
OUTPUT:
10. Rectangle

import javax.swing.*;
import java.awt.*;

public class RectangleExample extends JFrame {

public RectangleExample() {
setTitle("Java Rectangle Example");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(new RectanglePanel());
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> new
RectangleExample().setVisible(true));
}
}

class RectanglePanel extends JPanel {

@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;

// Draw a rectangle outline


g2d.setColor(Color.BLACK);
g2d.drawRect(x, y, width, height);

// Fill the rectangle with a color


g2d.setColor(Color.BLUE);
g2d.fillRect(x + 1, y + 1, width - 1, height - 1);
}
}
OUTPUT:
11. Rotation

import javax.swing.*;
import java.awt.*;

class Surface extends JPanel {

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g.create();

g2d.setPaint(new Color(150, 150, 150));


g2d.fillRect(20, 20, 80, 50);
g2d.translate(180, -50);
g2d.rotate(Math.PI/4);
g2d.fillRect(80, 80, 80, 50);

g2d.dispose();
}

public void paintComponent(Graphics g) {

super.paintComponent(g);
doDrawing(g);
}
}

public class RotationEx extends JFrame {

public RotationEx() {

initUI();
}

private void initUI() {


setTitle("Rotation");

add(new Surface());

setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {

RotationEx ex = new RotationEx();


ex.setVisible(true);
}
});
}
}
OUTPUT:
12. Rotation Origin

import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;

public class RotationExampleOrigin extends JPanel {

private static final int WIDTH = 400;


private static final int HEIGHT = 400;

private int[] xPoints = {100, 200, 300};


private int[] yPoints = {200, 100, 200};
private int numPoints = xPoints.length;

@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);

AffineTransform rotation = new AffineTransform();


rotation.rotate(rotationAngle, pivotX, pivotY);

// Apply rotation to each point individually


int[] rotatedXPoints = new int[numPoints];
int[] rotatedYPoints = new int[numPoints];
for (int i = 0; i < numPoints; i++) {
Point rotatedPoint = rotatePoint(xPoints[i], yPoints[i], rotation);
rotatedXPoints[i] = rotatedPoint.x;
rotatedYPoints[i] = rotatedPoint.y;
}

// Rotated object
g2d.setColor(Color.black);
g2d.drawPolygon(rotatedXPoints, rotatedYPoints, numPoints);
}

private Point rotatePoint(int x, int y, AffineTransform rotation) {


double[] point = new double[] {x, y};
rotation.transform(point, 0, point, 0, 1);
return new Point((int) Math.round(point[0]), (int)
Math.round(point[1]));
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Original and Rotated Objects");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.add(new RotationExampleOrigin());
frame.setVisible(true);
});
}
}
OUTPUT:
13. Scaling

import javax.swing.*;
import java.awt.geom.AffineTransform;
import java.awt.*;

class Surfacee extends JPanel {

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g.create();

g2d.setColor(new Color(150, 150, 150));


g2d.fillRect(20, 20, 80, 50);

AffineTransform tx1 = new AffineTransform();


tx1.translate(110, 22);
tx1.scale(0.5, 0.5);

g2d.setTransform(tx1);
g2d.fillRect(0, 0, 80, 50);

AffineTransform tx2 = new AffineTransform();


tx2.translate(170, 20);
tx2.scale(1.5, 1.5);

g2d.setTransform(tx2);
g2d.fillRect(0, 0, 80, 50);

g2d.dispose();
}
@Override
public void paintComponent(Graphics g) {

super.paintComponent(g);
doDrawing(g);
}
}

public class ScalingEx extends JFrame {

public ScalingEx() {

initUI();
}

private void initUI() {

add(new Surfacee());

setTitle("Scaling");
setSize(330, 160);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {

ScalingEx ex = new ScalingEx();


ex.setVisible(true);
}
});
}
}
OUTPUT:
14. Text Display

import javax.swing.*;
import java.awt.*;

public class TextDisplayExample extends JPanel {

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// Convert to Graphics2D for advanced rendering capabilities


Graphics2D g2d = (Graphics2D) g;

// Set font properties


Font font = new Font("Arial", Font.BOLD, 24);
g2d.setFont(font);

// Set color
g2d.setColor(Color.BLACK);

// Display text at coordinates (x, y)


int x = 50;
int y = 100;
g2d.drawString("Hello, Java 2D!", x, y);
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Java 2D Text Display Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.add(new TextDisplayExample());
frame.setVisible(true);
});
}
}

OUTPUT:
15. Texture

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class TextureExample extends JPanel {

private BufferedImage texture;

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();

setPreferredSize(new Dimension(400, 400));


}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// Enable texture on the Graphics2D object


Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(new TexturePaint(texture, new Rectangle(0, 0,
texture.getWidth(), texture.getHeight())));

g2d.fillRect(50, 50, 300, 300); }


public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Texture Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TextureExample());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}

OUTPUT:
16. Thick Line

import javax.swing.*;
import java.awt.*;

public class ThickLineExample extends JPanel {

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;

int width = getWidth();


int height = getHeight();

// Set the background color


g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);

// Set line width


float lineWidth = 5.0f;

// Create a stroke with the desired line width


BasicStroke stroke = new BasicStroke(lineWidth);

// Set the stroke and draw the line


g2d.setStroke(stroke);
g2d.setColor(Color.BLACK);
g2d.drawLine(50, 50, width - 50, height - 50);
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Thick Line Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.add(new ThickLineExample());
frame.setVisible(true);
});
}}
OUTPUT:
17. Translation

import javax.swing.*;
import java.awt.*;

class Surface extends JPanel {

private void doDrawing(Graphics g) {

Graphics2D g2d = (Graphics2D) g.create();

g2d.setPaint(new Color(150, 150, 150));


g2d.fillRect(20, 20, 80, 50);
g2d.translate(150, 50);
g2d.fillRect(20, 20, 80, 50);

g2d.dispose();
}

@Override
public void paintComponent(Graphics g) {

super.paintComponent(g);
doDrawing(g);
}
}

public class TranslationEx extends JFrame {

public TranslationEx() {

initUI();
}

private void initUI() {


add(new Surface());

setTitle("Translation");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {

TranslationEx ex = new TranslationEx();


ex.setVisible(true);
}
});
}
}
OUTPUT:

You might also like