Home Answers Viewqa Swing-AWT Line Drawing

 
 


P.Umesh Reddy
Line Drawing
1 Answer(s)      4 years and 11 months ago
Posted in : Swing AWT

View Answers

July 15, 2008 at 4:19 PM


Hi friend,

i am sending code of draw line using java Swing

import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;


public class DrowLine {
public static void main(String[] args) {
System.out.println("Line draw example using java Swing");
JFrame frame = new JFrame("Draw line example");
BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_BGR);
Graphics2D g = image.createGraphics();
g.setColor(Color.pink);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.BLACK);
RoundRectangle2D round = new RoundRectangle2D.Double(50, 50, 100, 100, 50, 50);
g.draw(round);
Point2D mousePoint = new Point2D.Double(101, 101);
Point2D center = new Point2D.Double(round.getCenterX(), round.getCenterY());
Point2D point2 = new Point2D.Double();
if (center.getX() == mousePoint.getX())
point2.setLocation(mousePoint.getX(), (mousePoint.getY() < center.getY()) ? round.getMinY() : round.getMaxY());
else {
double a = (center.getY() - mousePoint.getY()) / (center.getX() - mousePoint.getX());
double b = mousePoint.getY() - a * mousePoint.getX();
double x = (mousePoint.getX() < center.getX()) ? round.getMinX() : round.getMaxX();
point2.setLocation(x, a * x + b);
}
Line2D line = new Line2D.Double(mousePoint, point2);
g.setClip(round);
g.draw(line);
frame.getContentPane().add(new JLabel(new ImageIcon(image)));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}

-----------------------------------------

Read for more information.

http://www.roseindia.net/java/

Thanks









Related Pages:
Line Drawing - Swing AWT
Line Drawing  How to Draw Line using Java Swings in Graph chart,by giving x & Y axis values  Hi friend, i am sending code of draw line...) { System.out.println("Line draw example using java Swing"); JFrame frame = new
Java: Example - Drawing a Face - v2
Java: Example - Drawing a Face - v2 This simple application creates... the paintComponent method to do the drawing. 1 2 3 4 5 6 7 8...()); // Put drawing in window. window.pack(); // Adjust
Java: Example - Drawing a Face - v3
Java: Example - Drawing a Face - v3 Two separate source files...); window.setContentPane(new Face()); // Put drawing in window...) { super.paintComponent(g); // Required as first line. //... Get
Java: Example - Drawing a Face - v5
Java: Example - Drawing a Face - v5 Main - Setting the instance variables...); // Put drawing in window. window.pack(); // Adjust to fit...) { super.paintComponent(g); // Required as first line. //... Get panel width
Different Line Styles
provides several line drawing attributes: the line width, the dash pattern.... The setStroke() method sets the settings of the strokes of line used when drawing... Different Line Styles     
Java: Example - Drawing a Face
Java: Example - Drawing a Face This simple application creates... the paintComponent method to do the drawing. 1 2 3 4 5 6 7...()); // set its layout PacLady drawing = new PacLady
Java: Example - Drawing a Face - v5 - Fill-in
Java: Example - Drawing a Face - v5 - Fill-in Main - Setting the instance...(aFace); // Put drawing in window. window.pack(); // Adjust...) { super.paintComponent(g); // Required as first line. //... Get
Drawing
the basis for drawing simple graphics (lines, circles, text, ...). It is easy to use, and can be used with the Color class to set the drawing color and Font class to set the font for drawing text. Additional graphical classes In Java 2
Simple Line Canvas Example
Simple Line Canvas Example       This is a simple example of drawing lines using canvas class in J2ME. In this example we are creating three different lines at different
importing excel file and drawing multiple graphs from one excel file
importing excel file and drawing multiple graphs from one excel file  thanks a lot sir for replying with code for importing excel file... index in the line HSSFCell cell1 = row.getCell(a); i want to pass column ID..how
JPanel - Drawing Area
Java: JPanel - Drawing Area   To use a JPanel for graphics  ...; Example of drawing panel drawing area.   Define a subclass of JPanel... background . . . // do your drawing here
How to create LineDraw In Java
will learn how to create Line Drawing. This program  implements a line Drawing component. A java program explains the stroke line i.e. how to make thick... named, LineDraw in program for creating a java AWT Line Drawing component
Drawing a Face v2
Java: Example - Drawing a Face v2 This simple application creates... the paintComponent method to do the drawing. 1 2 3 4 5 6 7 8 9... content.setLayout(new BorderLayout()); // set its layout PacLady drawing = new
Drawing a Shape in Excel Sheet
Drawing a Shape in Excel Sheet In this section, you will learn how to draw... a shape with the help of Microsoft Office drawing tools. The top-most shape is the patriarch. This is not visible on the sheet at all. To start drawing you
Setting Grid Line in JTable
component. The grid line is a line that  separates  the cells by drawing... Setting Grid Line in JTable     ... JTable that contains predefined grid line with black color. But in this Java
Drawing a Face - v4
Java: Example - Drawing a Face - v4 Instance variables representing... of the drawing. These could be declared as constants (static final), but we're going...); // Required as first line. //... Get panel width and height
How many types of drawing objects are available in drawing toolbar
How many types of drawing objects are available in drawing toolbar  How many types of drawing objects are available in drawing toolbar
Drawing Graphs - Swing AWT
Drawing Graphs  hi i am doing a small project in java swings . there is a database table having two columns A,B i want to plot a graph to that values in database the graph must be interactive graph
Drawing Graphs - Java3D
Drawing Graphs  how to draw graphs using certain parameters  Hi Friend, To draw a graph using JfreeChart library, please visit the following link: http://www.roseindia.net/chartgraphs/index.shtml Here you
scrolling a drawing..... - Swing AWT
scrolling a drawing.....  I am using a canvas along with other components like JTable over a frame the drawing which i am going to show over canvas is dynamic and hence there is no fixed size, that's why i need to make
drawing out of bounds
drawing out of bounds  How to throw an exception when a rectangle goes out of the boundaries of a JFrame. The program is asking to draw a triangle, and resize it. If the triagle size goes out of the JFrame throw an execption. I
How to convert a picture to drawing, convert a picture to drawing, picture to drawing
How to convert a picture to drawing       Learn here a simple processing to make a color picture to drawing, we have tried here to take simple step to learn easily for the beginners
Please help in drawing Flowchart
Please help in drawing Flowchart  we found that the student names with marks in a text file the marks decreases significantly. when exams conducted fast 4 times.THe first column is the student name and the second column is marks
drawing shapes in applet - Applet
drawing shapes in applet  hi, i need a single applet prgm which draws cone,cube and square in circle.  Hi manju, This is cube example import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import
Java - Drawing Shapes Example in java
Java - Drawing Shapes Example in java   ... the different types of shapes like line, circle and rectangle...() method has been used in the program to draw the line in the applet. Here
drawing a human in java - Java Beginners
drawing a human in java  i wanna to draw a human in java can u help me how can i draw.? the question is : how to draw a human in java add the nose + the eye + mouth .   where is the answer.? plz i want
An Applet program for Drawing picture & graph - Applet
An Applet program for Drawing picture & graph  How can I write an Applet program for drawing picture and graph having various colours and tools provided
drawing a christmas tree - Java Beginners
drawing a christmas tree  how do i write a program that draws a chrimas tree using the knowledge of loops only. i.e * *** ***** ******* the program will draw a Christ mas tree  Hi ChristmasTree.javaimport java.awt.
problem in scrolling SVG drawing in canvas - MobileApplications
problem in scrolling SVG drawing in canvas  hi all, i'm developing an app that display maps on mobile -i used SVG graphics in drawing the maps... over the drawing , scrolling works well in wireless toolkit emulator , but when i
Draw Line
Draw Line  sir i want to draw a moving line in j2me.That line should also show arrow in moving direction. How can we do so
line graph
line graph  I have developed an application and in that i have to compare 2 reports using line graph from the data taken from ms access database?please help
on line exam
on line exam  how we can upload the data on the server from any site
Applet program for drawing picture and graph - Applet
Applet program for drawing picture and graph  Please send me the program(code) of drawing picture and graph in Applet.  Hi Friend, Please visit the following links: http://www.roseindia.net/java/example/java
Java - Drawing Shapes Example using color in java
Java - Drawing Shapes Example using color in java  ... the different types of colorful shapes like line, circle and the rectangle. It also... been used to fill or set the color of line, text, circle background and rectangle
Java gui program for drawing rectangle and circle
Java gui program for drawing rectangle and circle   how to write java gui program for drawing rectangle and circle? there shoud be circle and rectangle button, check box for bold and italic, and radio button red,green and blue
drawing triange using stars - Java Beginners
drawing triange using stars  How do i Write a program to draw the boundaries of a plot of land with the structure shown in the diagram, for any set of input values for the dimensions. Use ?*? to represent the sides   
AJAX Line
AJAX Line       AJAX Community with a blog, forum and tutorials Read full Description
objective c read file line by line
objective c read file line by line  Please explain how to read files in Objective C line by line
Summary: Graphics
in the java.awt.Graphics class for drawing with shapes, colors, text, ... Other... classes: Fonts, Images - ImageIcon java.awt.Graphics Class - Basic drawing...) g.setColor(Color c); // All drawing after this uses the Color c
Java - How to read a string line per line
Java - How to read a string line per line  Java - How to read a string line per line I am trying to find the tutorial for reading the String line per line. I have a String data, which contains many lines. What is the best way
Java read file line by line
Java read file line by line In this section, you will learn how to read a file line by line. Java has provide several classes for file manipulation. Here we are going to read a file line by line. For reading text from a file it's better
How to Read a file line by line using BufferedReader?
How to Read a file line by line using BufferedReader?  Hello Java... to Read a file line by line using BufferedReader, efficiently and using less memory... java.io.BufferedReader in your program and use the class for reading the file line
Java write to file line by line
Java write to file line by line In this section, you will learn how to write a file line by line. It is better to use Writer class instead of OutputStream... to a byte array. The method newLine() is used for writing a new line character. We
Command line arguments
Command line arguments  Please explain me what is the use of "command line arguments" and why we need to use
Line Breaks in PHP
Line Breaks in PHP  Hi, I am beginner in PHP language. How to do the line-break in PHP? Does somebody help me in this regard. Thanks
sorting Line in c
sorting Line in c  Hi I need help with sorting line in programming c asking user to enter 1-10 characters and then sort them in lower case, alphabetical and ascending order. Any help will be appreciated! please help! void
divide the line in to two
divide the line in to two  In| this |example |we| are| creating |an |string| object| .We| initialize| this| string |object| as| "Rajesh Kumar"|.| We| are| taking| sub| string | In above mentioned line based on the tag i want
Java Command Line Input
Java Command Line Input  How to get input from command line in java ?   Hi please Find the code for inputting data from the command line System.out.println("Please Input A Number"); BufferedReader br
Java: Example - Sleepy Source
Java: Example - Sleepy Source This program combines components (eg, buttons) and drawing. The Sleeper panel is used for graphics...); // MUST be first line //--- draw head g.setColor
character as command line arguments
character as command line arguments  public class testing { public static void main(String args[]) { char a; a=args[0]; System.out.println("character is:"+a); } } what will be the above program output

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.