Hello Everyone, I decided to make a random problem to understand new ideas a little better. I decided to make a fun drawing program, with which I'm only drawing rectangles at the moment. The code is extremely messy and not well written, since I dint make any plans nor do I know how to use design patterns. The following code works and can draw a rectangle without any lines to help you understand the shape of it. I'm assuming one must use the mouseDragged method, the paint Method, and the repaint method.
Here is the code.
Driver.Java
Display.java
Drawer.java
Rectangle
Here is the code.
Driver.Java
public class Driver {
/*
* Author: Vairon M. Date 2/12/2013 Purpose:Draw rectangles... add other
* objects in the future
*/
static final String TITLE = "Drawing";
static final int WIDTH = 800, HEIGHT = 600;
public static void main(String[] args) {
new Display(TITLE, WIDTH, HEIGHT);
}
}
Display.java
import java.awt.Dimension;
import javax.swing.JFrame;
public class Display extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public Display(String TITLE, int WIDTH, int HEIGHT) {
setTitle(TITLE);
setPreferredSize(new Dimension(WIDTH,HEIGHT));
setMaximumSize(new Dimension(WIDTH,HEIGHT));
setMinimumSize(new Dimension(WIDTH,HEIGHT));
add(new Drawer(WIDTH,HEIGHT));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
Drawer.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Drawer extends JPanel implements ActionListener, MouseListener,
MouseMotionListener {
private static final long serialVersionUID = 1L;
private Timer timer = new Timer(5, this);
private Vector<Rectangle> rectangles = new Vector<Rectangle>();
public Drawer(int WIDTH, int HEIGHT) {
setSize(WIDTH, HEIGHT);
setBackground(Color.black);
addMouseListener(this);
addMouseMotionListener(this);
timer.start();
}
public void paint(Graphics canvas) {
super.paint(canvas);
canvas.setColor(Color.white);
for (int i = 0; i < rectangles.size(); i++) {
rectangles.elementAt(i).draw(canvas);
}
canvas.setColor(Color.white);
canvas.drawString("Rectangle Count:" + rectangles.size(), 650, 10);
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
rectangles.add(new Rectangle());
int x = e.getX();
int y = e.getY();
rectangles.elementAt(rectangles.size() - 1).setX(x);
rectangles.elementAt(rectangles.size() - 1).setY(y);
}
public void mouseReleased(MouseEvent e) {
int startX = e.getX();
int startY = e.getY();
int endX = rectangles.elementAt(rectangles.size() - 1).getX();
int endY = rectangles.elementAt(rectangles.size() - 1).getY();
rectangles.elementAt(rectangles.size() - 1).setWidth(startX - endX);
rectangles.elementAt(rectangles.size() - 1).setHeight(startY - endY);
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
}
Rectangle
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Rectangle {
private int x, y, width, height;
private Random colorRandom = new Random();
private final Color color = new Color(colorRandom.nextInt(255),
colorRandom.nextInt(255), colorRandom.nextInt(255));
public Rectangle() {
}
public void draw(Graphics _canvas) {
_canvas.setColor(color);
_canvas.fill3DRect(x, y, width, height, true);
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int _x) {
x = _x;
}
public void setY(int _y) {
y = _y;
}
public void setHeight(int _height) {
height = _height;
}
public void setWidth(int _width) {
width = _width;
}
// add delete method
}