Instructions
Objective
Write a Java assignment program to implement bitmap and polymorphism.
Requirements and Specifications
Source Code
APP
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class App extends JFrame {
JFrame f;
JPanel jp;
public App()
{
// Random object used for testing
Random r = new Random();
f = new JFrame();
f.setTitle("Drawing Shapes");
f.setSize(800,500);//400 width and 500 height
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton addLineButton = new JButton("Line");//creating instance of JButton
addLineButton.setBounds(20,20,100, 20);
JButton addRectangleButton = new JButton("Rectangle");//creating instance of JButton
addRectangleButton.setBounds(140,20,100, 20);
JButton addEllipseButton = new JButton("Ellipse");//creating instance of JButton
addEllipseButton.setBounds(260,20,100, 20);
// Create RGB Sliders
JSlider RSlider = new JSlider(0, 255, 1);
JSlider GSlider = new JSlider(0, 255, 1);
JSlider BSlider = new JSlider(0, 255, 1);
RSlider.setBounds(380, 20, 100, 30);
RSlider.setBackground(Color.RED);
GSlider.setBounds(500, 20, 100, 30);
GSlider.setBackground(Color.GREEN);
BSlider.setBounds(620, 20, 100, 30);
BSlider.setBackground(Color.BLUE);
addLineButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("LINE BUTTON PRESSED");
int x1 = r.nextInt(800);
int y1 = r.nextInt(500);
int x2 = r.nextInt(800);
int y2 = r.nextInt(500);
Shape line = new Line(new Point(x1, y1), new Point(x2, y2));
// get RGB from Sliders
int R = RSlider.getValue();
int G = GSlider.getValue();
int B = BSlider.getValue();
line.setRGB(R, G, B);
jp = new ShapesDrawer(line);
f.add(jp);
f.revalidate();
f.repaint();
}
});
addRectangleButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("RECTANGLE BUTTON PRESSED");
int x = r.nextInt(800);
int y = r.nextInt(500);
Shape rect = new Rectangle(new Point(x, y), r.nextInt(100), r.nextInt(100));
// get RGB from Sliders
int R = RSlider.getValue();
int G = GSlider.getValue();
int B = BSlider.getValue();
rect.setRGB(R, G, B);
jp = new ShapesDrawer(rect);
f.add(jp);
f.revalidate();
f.repaint();
}
});
addEllipseButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("ELLIPSE BUTTON PRESSED");
int x = r.nextInt(800);
int y = r.nextInt(500);
shape ellipse = new Ellipse(new Point(x, y), r.nextInt(300), r.nextInt(300));
// get RGB from Sliders
int R = RSlider.getValue();
int G = GSlider.getValue();
int B = BSlider.getValue();
ellipse.setRGB(R, G, B);
jp = new ShapesDrawer(ellipse);
f.add(jp);
f.revalidate();
f.repaint();
}
});
f.add(addLineButton);
f.add(addRectangleButton);
f.add(addEllipseButton);
// add sliders
f.add(RSlider);
f.add(GSlider);
f.add(BSlider);
Shape rect = new Rectangle(new Point(40, 40), 60, 50);
rect.setRGB(255,255,255);
jp = new ShapesDrawer();
f.add(jp);
f.setVisible(true);//making the frame visible
}
public static void main(String[] args) throws Exception {
App app = new App();
//app.setVisible(true);
}
}
ELLIPSE
import java.awt.*;
public class Ellipse implements Shape {
private Point center;
private int width;
private int height;
private int R;
private int G;
private int B;
public Ellipse(Point center, int w, int h)
{
this.center = center;
this.width = w;
this.height = h;
}
public void setRGB(int R, int G, int B) {
this.R = R;
this.G = G;
this.B = B;
}
// getters
public Point getCenter() {return center;}
public int getWidth() {return width;}
public int getHeight() {return height;}
public int getR() {return this.R;}
public int getG() {return this.G;}
public int getB() {return this.B;}
// Convert RGB to color
public Color getColor()
{
return new Color(getR(), getG(), getB());
}
@Override
public void draw() {
// TODO Auto-generated method stub
}
}
LINE
import java.awt.*;
public class Line implements Shape {
private Point center;
private Point endpoint;
private int R;
private int G;
private int B;
public Line(Point center, Point endpoint)
{
this.center = center;
this.endpoint = endpoint;
}
// getters
public Point getCenter() {return center;}
public Point getEndPoint() {return endpoint;}
public void setRGB(int R, int G, int B) {
this.R = R;
this.G = G;
this.B = B;
}
public int getR() {return this.R;}
public int getG() {return this.G;}
public int getB() {return this.B;}
// Convert RGB to color
public Color getColor()
{
return new Color(getR(), getG(), getB());
}
@Override
public void draw() {
// TODO Auto-generated method stub
}
}
RECTANGLE
import java.awt.*;
public class Rectangle implements Shape {
private Point center;
private int width;
private int height;
private int R;
private int G;
private int B;
public Rectangle(Point center, int width, int height)
{
this.center = center;
this.width = width;
this.height = height;
}
// getters
public Point getCenter() {return center;}
public int getWidth() {return width;}
public int getHeight() {return height;}
public int getR() {return this.R;}
public int getG() {return this.G;}
public int getB() {return this.B;}
public void setRGB(int R, int G, int B) {
this.R = R;
this.G = G;
this.B = B;
}
// Convert RGB to color
public Color getColor()
{
return new Color(getR(), getG(), getB());
}
@Override
public void draw() {
// TODO Auto-generated method stub
}
}
Related Samples
At ProgrammingHomeworkHelp.com, we offer comprehensive support for Java assignments, including access to related samples that illustrate key concepts and solutions. Our Java assignment samples cover a wide range of topics, from basic syntax and object-oriented programming to advanced techniques like concurrency and data structures. These samples are designed to help students understand complex Java concepts and improve their coding skills. With expert guidance and detailed examples, our service aims to support students in mastering Java and achieving academic success.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java