Results 1 to 1 of 1
- 04-12-2012, 10:50 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Write string using only lines(no drawString or System.out)
Hi All!
Wrote code to write a string "HELLO", everything works good but I feel that there has to be shortest way to do the same. Need to know, just curiosity :)
Thanks.
LetterH class(LetterE, LetterL and LetterO are written in same way):
LetterHelloComponent class:Java Code:import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.Graphics2D; public class LetterH { public void draw(Graphics2D g2) { Point2D.Double p1 = new Point2D.Double(0,0); Point2D.Double p2 = new Point2D.Double(0,20); Point2D.Double p3 = new Point2D.Double(0,10); Point2D.Double p4 = new Point2D.Double(10,0); Point2D.Double p5 = new Point2D.Double(10,20); Point2D.Double p6 = new Point2D.Double(10,10); Line2D.Double part1 = new Line2D.Double(p1, p2); Line2D.Double part2 = new Line2D.Double(p4, p5); Line2D.Double part3 = new Line2D.Double(p3, p6); g2.draw(part1); g2.draw(part2); g2.draw(part3); } }
LetterViewer class:Java Code:import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.Graphics2D; public class LetterO { public void draw(Graphics2D g2) { Point2D.Double p1 = new Point2D.Double(60,0); Point2D.Double p2 = new Point2D.Double(60,20); Point2D.Double p3 = new Point2D.Double(70,0); Point2D.Double p4 = new Point2D.Double(70,20); Line2D.Double part1 = new Line2D.Double(p1, p2); Line2D.Double part2 = new Line2D.Double(p1, p3); Line2D.Double part3 = new Line2D.Double(p3, p4); Line2D.Double part4 = new Line2D.Double(p2, p4); g2.draw(part1); g2.draw(part2); g2.draw(part3); g2.draw(part4); } }
Java Code:import javax.swing.JFrame; import javax.swing.JComponent; import java.awt.Graphics; import java.awt.Graphics2D; public class LetterViewer { public static void main(String[] args) { // TODO Auto-generated method stub JFrame frame = new JFrame(); frame.setSize(300, 400); frame.setTitle("Hello String"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); LetterHelloComponent component = new LetterHelloComponent(); frame.add(component); frame.setVisible(true); } }
Similar Threads
-
Splitting an string of number into multiple lines
By Juukamen in forum New To JavaReplies: 5Last Post: 04-09-2012, 10:30 AM -
Write a given number of lines and chararcters of a selected char
By KAS in forum New To JavaReplies: 4Last Post: 03-21-2011, 09:49 PM -
counting number of lines of system.out
By IYIaster in forum New To JavaReplies: 1Last Post: 07-21-2009, 12:37 AM -
printing an array of String through drawString
By kaemonsaionji in forum New To JavaReplies: 1Last Post: 02-23-2009, 04:38 PM -
How to split a string into multiple lines of x characters each
By JackJ in forum New To JavaReplies: 3Last Post: 12-17-2007, 02:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks