Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package paint; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.awt.geom.*; import java.util.*; @SuppressWarnings("serial") /** * * @author ramytamer */ public final class Paint extends JFrame { JButton lineBtn, circleBtn, ellipseBtn, rectangleBtn, squareBtn, triangleBtn, colorBtn; public JButton getSquareBtn() { return squareBtn; } public void setSquareBtn(JButton squareBtn) { this.squareBtn = squareBtn; } public JButton getLineBtn() { return lineBtn; } public void setLineBtn(JButton lineBtn) { this.lineBtn = lineBtn; } public JButton getCircleBtn() { return circleBtn; } public void setCircleBtn(JButton circleBtn) { this.circleBtn = circleBtn; } public JButton getEllipseBtn() { return ellipseBtn; } public void setEllipseBtn(JButton ellipseBtn) { this.ellipseBtn = ellipseBtn; } public JButton getRectangleBtn() { return rectangleBtn; } public void setRectangleBtn(JButton rectangleBtn) { this.rectangleBtn = rectangleBtn; } public JButton getTriangleBtn() { return triangleBtn; } public void setTriangleBtn(JButton triangleBtn) { this.triangleBtn = triangleBtn; } public JButton getColorBtn() { return colorBtn; } public void setColorBtn(JButton colorBtn) { this.colorBtn = colorBtn; } public Graphics2D getGraphSettings() { return graphSettings; } public void setGraphSettings(Graphics2D graphSettings) { this.graphSettings = graphSettings; } public int getDrawAction() { return drawAction; } public void setDrawAction(int drawAction) { this.drawAction = drawAction; } public Color getColour() { return colour; } public void setColour(Color colour) { this.colour = colour; } Graphics2D graphSettings; int drawAction = 1; Color colour = Color.BLACK; /** * @param args the command line arguments */ public static void main(String[] args) { Paint paint = new Paint(); } public Paint() { this.setSize(800, 670); this.setResizable(false); this.setTitle("The awesome paint program"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel btnPanel = new JPanel(); JPanel colorBtnPanel = new JPanel(); Box btnsBox = Box.createVerticalBox(); Box colorBtnBox = Box.createVerticalBox(); setLineBtn(addBtn("./src/line.png", 1, false)); setCircleBtn(addBtn("./src/circle.png", 2, false)); setEllipseBtn(addBtn("./src/ellipse.png", 3, false)); setRectangleBtn(addBtn("./src/rectangle.png", 4, false)); setSquareBtn(addBtn("./src/square.png", 5, false)); setTriangleBtn(addBtn("./src/triangle.png", 6, false)); setColorBtn(addBtn("./src/color.png", 7, true)); btnsBox.add(getLineBtn()); btnsBox.add(getCircleBtn()); btnsBox.add(getEllipseBtn()); btnsBox.add(getRectangleBtn()); btnsBox.add(getSquareBtn()); btnsBox.add(getTriangleBtn()); colorBtnBox.add(getColorBtn()); btnPanel.add(btnsBox); colorBtnPanel.add(colorBtnBox); this.add(btnPanel, BorderLayout.WEST); this.add(colorBtnPanel, BorderLayout.EAST); this.add(new DrawingBoard(), BorderLayout.CENTER); this.setVisible(true); } public JButton addBtn(String path, int dAction, boolean isColor) { JButton btn = new JButton(); Icon btnIcon = new ImageIcon(path); btn.setIcon(btnIcon); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (isColor) { setColour(JColorChooser.showDialog(null, "Pick a Color", Color.BLACK)); } else { setDrawAction(dAction); System.out.println(getDrawAction()); } } }); return btn; } private class DrawingBoard extends JComponent { ArrayList<Shape> shapes = new ArrayList<>(); ArrayList<Color> shapeColor = new ArrayList<>(); Point startPoint, endPoint; public Point getStartPoint() { return startPoint; } public void setStartPoint(Point startPoint) { this.startPoint = startPoint; } public Point getEndPoint() { return endPoint; } public void setEndPoint(Point endPoint) { this.endPoint = endPoint; } public DrawingBoard() { this.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { // When the mouse is pressed get x & y position setStartPoint(new Point(e.getX(), e.getY())); System.out.println("Setting start & end point: " + e.getX() + "," + e.getY()); setEndPoint(getStartPoint()); repaint(); } @Override public void mouseReleased(MouseEvent e) { // Create a shape using the starting x & y // and finishing x & y positions Shape theShape = null; if (getDrawAction() == 1) { // Line System.out.println("(x1,y1)->(" + getStartPoint().x + "," + getStartPoint().y + ")"); System.out.println("(x2,y2)->(" + e.getX() + "," + e.getY() + ")"); theShape = drawLine(getStartPoint().x, getStartPoint().y, e.getX(), e.getY()); } else if (getDrawAction() == 2) { // Circle // theShape = drawCircle(getStartPoint().x, getEndPoint().y, e.getX(), e.getY()); } else if (getDrawAction() == 3) { // Ellipse // theShape = drawEllipse(getStartPoint().x, getEndPoint().y, e.getX(), e.getY()); } else if (getDrawAction() == 4) { // Rectangle theShape = drawRectangle(getStartPoint().x, getStartPoint().y, e.getX(), e.getY()); } else if (getDrawAction() == 5) { // Square // theShape = drawSquare(getStartPoint().x, getEndPoint().y, e.getX(), e.getY()); } else if (getDrawAction() == 6) { // Triangle // theShape = drawTriangle(getStartPoint().x, getEndPoint().y, e.getX(), e.getY()); } // Add shapes, fills and colors to there ArrayLists shapes.add(theShape); System.out.println("Adding Shape"); System.out.println(theShape); shapeColor.add(getColour()); setStartPoint(null); setEndPoint(null); repaint(); } }); this.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { // Get the final x & y position after the mouse is dragged setEndPoint(new Point(e.getX(), e.getY())); System.out.println("Setting end point: " + e.getX() + "," + e.getY()); repaint(); } }); } private Line2D.Float drawLine(int x1, int y1, int x2, int y2) { return new Line2D.Float(x1, y1, x2, y2); } private Ellipse2D.Float drawEllipse( int x1, int y1, int x2, int y2) { int x = Math.min(x1, x2); int y = Math.min(y1, y2); int width = Math.abs(x1 - x2); int height = Math.abs(y1 - y2); return new Ellipse2D.Float( x, y, width, height); } private Ellipse2D.Float drawCircle( int x1, int y1, int x2, int y2) { int x = Math.min(x1, x2); int y = Math.min(y1, y2); int width = Math.abs(x1 - x2); int height = width; return new Ellipse2D.Float( x, y, width, height); } private Rectangle2D.Float drawRectangle(int x1, int y1, int x2, int y2) { int x = Math.min(x1, x2); int y = Math.min(y1, y2); int width = Math.abs(x1 - x2); int height = Math.abs(y1 - y2); return new Rectangle2D.Float(x, y, width, height); } private Rectangle2D.Float drawSquare(int x1, int y1, int x2, int y2) { int x = Math.min(x1, x2); int y = Math.min(y1, y2); int width = Math.abs(x1 - x2); int height = width; return new Rectangle2D.Float(x, y, width, height); } @Override public void paint(Graphics graphics) { setGraphSettings((Graphics2D) graphics); getGraphSettings().setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); getGraphSettings().setStroke(new BasicStroke(4)); Iterator<Color> colourCounter = shapeColor.iterator(); for (Shape shape : shapes) { // actually i don't know what is 0.40f :D getGraphSettings().setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); getGraphSettings().draw(shape); getGraphSettings().setPaint(colourCounter.next()); getGraphSettings().fill(shape); } Shape theShape = null; if (getStartPoint() != null && getEndPoint() != null) { // Color.LIGHT_GRAY // getGraphSettings().setPaint(Color.BLUE); if (getDrawAction() == 1) { // Line theShape = drawLine(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y); } else if (getDrawAction() == 2) { // Circle theShape = drawCircle(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y); } else if (getDrawAction() == 3) { // Ellipse theShape = drawEllipse(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y); } else if (getDrawAction() == 4) { // Rectangle theShape = drawRectangle(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y); } else if (getDrawAction() == 5) { // Square theShape = drawSquare(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y); } else if (getDrawAction() == 6) { // Triangle // theShape = new Triangle().draw(startpoint.x,startpoint.y,e.getX(),e.getY()); } } if (theShape != null) { graphSettings.draw(theShape); } } } }
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
Untitled
1 hour ago | 12.49 KB
Untitled
3 hours ago | 12.40 KB
programacion_algoritmos_flashcards.tsv
5 hours ago | 2.67 KB
Untitled
5 hours ago | 17.13 KB
Untitled
7 hours ago | 18.40 KB
Untitled
9 hours ago | 17.70 KB
Untitled
11 hours ago | 16.97 KB
Untitled
13 hours ago | 21.75 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!