Home Answers Viewqa Java-Beginners Java Graphics Programming

 
 


deepakpuligundla
Java Graphics Programming
1 Answer(s)      10 months ago
Posted in : Java Beginners

Hi<BR> I am newbie to java and I have written a code in java that creates a window in which a line and rectangle appears. Here is the code.

import java.awt.*;<BR> public class FirstCanvase extends Canvas<BR> {<BR> public FirstCanvase()<BR> {<BR> setSize(500,500);<BR> setBackground(Color.white);<BR> }<BR> public static void main(String[] args) <BR> {<BR>

FirstCanvase FC=new FirstCanvase();<BR>

Frame f1=new Frame();<BR> f1.add(FC);<BR> f1.setSize(250,250);<BR> f1.setVisible(true);<BR> }<BR> public void paint(Graphics g)<BR> {<BR> g.setColor(Color.blue);<BR> g.drawLine(30,30,80,80);<BR> g.drawRect(50,50,100,100);<BR> }<BR> }<BR>

EVERY THING IS WORKING FINE. I GOT A WINDOW DISPLAYED WITH A LINE & RECT. BUT THE PROBLEM IS I AM UNABLE TO CLOSE THE WINDOW.

I AM USING ECLIPSE TO RUN THE PROGRAM.

WHAT CAN I DO TO SOLVE IT?

View Answers

July 23, 2012 at 12:27 PM


The given code draws a line and a rectangle on a swing frame using Graphics class.

 import javax.swing.*;
import java.awt.*;

public class DrawLineAndRectangle{
  public static void main(String[] args) {
  DrawLineAndRectangle d = new DrawLineAndRectangle();
  }

  public DrawLineAndRectangle(){
  JFrame frame = new JFrame("Drawing with Alpha");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(new MyComponent());
  frame.setSize(400,400);
  frame.setVisible(true);  
  }

  public class MyComponent extends JComponent{
  public void paint(Graphics g){
  int height = 200;
  int width = 120;
  g.setColor(Color.red);
  g.drawRect(10,10,180,100);

  g.setColor(Color.red);
  g.drawLine(250,50, 200,100);
  }
  }
}









Related Pages:
Java Graphics Programming
Java Graphics Programming  Hi<BR> I am newbie to java and I have written a code in java that creates a window in which a line and rectangle...;BR> public void paint(Graphics g)<BR> {<BR> g.setColor
java graphics
java graphics  Name three Component subclasses that support painting
graphics - Java Beginners
graphics  In java-graphics.. I want to draw a rectangle and resize that rectangle(small,big) ..by using mouse cursors
Graphics class
Java NotesGraphics class Description The Graphics class contains drawing methods. A Graphics object is passed to your paintComponent(Graphics g). You... equal to the height. Advanced There are also Graphics methods for drawing images
Java 2D Graphics - Applet
Java 2D Graphics  I am working on GIS project.I want represent a line in different forms like railroad, airplane etc. How can i acheive this I know...://www.roseindia.net/java/example/java/swing/graphics2D/ Here you will get lot
java 2d graphics - Java Beginners
java 2d graphics  Hello All I need to use 2d graphics in java to build up a map from the given geographic coordinates. What i am not getting is how to scale down these geographic coordinates to device coordinates. I would
Summary: Graphics
Java: Summary: Graphics This is a summary of simple graphics methods... classes define more advanced graphics, eg, javax.swing.Graphics2D. Related... methods Draw on a JPanel. Assume g is a Graphics object, and all variables
Summary: GUI Graphics
Java: Summary: GUI Graphics This is a summary of simple graphics methods... classes define more advanced graphics, eg, javax.swing.Graphics2D. java.awt.Graphics Class Draw on a JPanel. Assume g is a Graphics object, and all
Graphics 2D
.    Noise Image in Graphics This Java Tutorial is all about creating Images with the use of graphics in Java. For this we... Example In this section, we are going to develop a small Java Graphics
programming question - Java Magazine
programming question  I have an SGVT map displaying on a phone... Vector Graphics)which is XML based. SVGT(Scalable vector graphics tinny 1.1 )is one that mobile devices support. Map was drawn with IKIVO animator graphics
java code along wid graphics design
java code along wid graphics design  actually i want to design nd make an apllication to solve a rubics cube of 3*3.. i have an algos wid me bt jus need ur guidance..plzz hlp out or if u have ny other ideas do suggest
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 8.3 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... the vertices of the polygon. The Graphics class includes commands for drawing
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.1 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... a subroutine, "void drawDie(Graphics g, int val, int x, int y)"
programming
Java Constructor programming for single and double constructor  write a program which have no argument constructor ,single parameter constructor constructor,double parameter constor,and the now when we create a object
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 8.4 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... in a graphics context. The parameter, g, must be of type Graphics. (The drawing color
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.6 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... doInitialization() synchronized public void drawFrame(Graphics g, int width
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 7.7 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... The original applet, SimplePaint3, has a method drawFigure(Graphics g
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 3.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook...;. Putting this all together into syntactically correct Java code, the algorithm
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.3 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... Java. (Note: Since there was only one button in this applet, I added
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 7.8 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... Graphics undoGr = undoBuffer.getGraphics(); undoGr.drawImage
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.8 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... of the applet: Sorry, but your browser doesn't support Java
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.2 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook...(Graphics g, int val, int x, int y) { // Draw a die with upper left
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.4 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... the programming into a drawing surface class named Board. Besides this nested class
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 9.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... paintComponent(Graphics g) { // Draw the graph of the function
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 7.4 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook.... The programming of this applet assumes that the applet
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 3.6 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook.... */ public void drawFrame(Graphics g
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 7.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... is in progress. The programming of this applet assumes that the applet
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook...: Sorry, but your browser doesn't support Java. Discussion
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 6.7 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook...(Graphics g) { // Draw the game, showing the square in its current
Java Programming: Solution to Programming Exercise
Solution for Programming Exercise 8.5 THIS PAGE DISCUSSES ONE POSSIBLE SOLUTION to the following exercise from this on-line Java textbook... a method to draw a piece: private void drawPiece(Graphics g, int piece
java graphics - Design concepts & design patterns
java graphics  i dont wanna use Java3D but put solid 3D objects on screen, use z-buffering, ray-casting etc. to make realistic images...this will help build 3D fractals etc...Where must i begin reading
Create Human Face using Graphics
Create Human Face using Graphics In this section, you will learn how to create human face using Graphics. A Graphics provides contexts that allow... off-screen images. The Graphics class provides several methods through which we
Noise Image in Graphics
of graphics in Java. For this we have to import awt and swing package in our Java code... that we have created in java using graphics... Noise Image in Graphics      
Java get Graphics
Java get Graphics     ... image.getGraphics() returns the Graphics object. The method...(400,400,BufferedImage.TYPE_INT_ARGB);   Graphics graphics=image.getGraphics
Java Programming: Contents
Introduction to Programming Using Java, Fourth Edition Table of Contents... to Applets and Graphics Programming Exercises Quiz on this Chapter Chapter 4... on this Chapter Chapter 7: Advanced GUI Programming Section 1: More about Graphics
programming
Java no argument constructor programming  write a program which have no argument constructor ,single parameter constructor constructor,double parameter constor,and the now when we create a object of in this constructor he we
programming
Java single parameter constructor  write a program which have no argument constructor ,single parameter constructor constructor,double parameter constor,and the now when we create a object of in this constructor he we tell
programming
Java parameter constructor  write a program which have no argument constructor ,single parameter constructor constructor,double parameter constor,and the now when we create a object of in this constructor he we tell the how much
programming
Constructor in java application  write a program which have no argument constructor ,single parameter constructor constructor,double parameter constor,and the now when we create a object of in this constructor he we tell the how
programming
Constructing a constructor in java application  write a program which have no argument constructor ,single parameter constructor constructor,double parameter constor,and the now when we create a object of in this constructor he
programming
constructor object in java application programming  write a program which have no argument constructor ,single parameter constructor constructor,double parameter constor,and the now when we create a object of in this constructor
programming
Construct a constructor of object in Java programming  write a program which have no argument constructor ,single parameter constructor constructor,double parameter constor,and the now when we create a object
Noise Image in Graphics
of graphics in Java. For this we have to import awt and swing package in our Java code... a noise image that we have created in java using graphics.  ... Noise Image in Graphics     
Programming
Programming  how to save output of java file in .txt format?   Hi, If you are running the example from dos prompt you can use the > bracket to direct the output to a text file. Here is the example: C:>java
Add RenderingHints to a Graphics
Add RenderingHints to a Graphics       This Java tutorial teaches you about adding the rendering hints to a graphics on the frame. The rendering hints uses the Graphics2D and creates
how to draw lines,circles, rectangles on JSP (using Java Graphics)
how to draw lines,circles, rectangles on JSP (using Java Graphics)  how to draw lines,circles, rectangles on JSP (using Java Graphics)   ... context Graphics g = image.getGraphics(); // Fill background g.setColor(Color.white
Java Programming: Chapter 7 Index
Chapter 7 Advanced GUI Programming IT'S POSSIBLE TO PROGRAM A WIDE... chapter. In many cases, the basic events, components, layouts, and graphics... to Swing. Although the title of the chapter is "Advanced GUI Programming
Java Programming: Chapter 6 Index
Chapter 6 Applets, HTML, and GUI's JAVA IS A PROGRAMMING LANGUAGE... contained in objects. In Java, event-oriented programming is object-oriented programming. This chapter covers the basics of applets, graphics, components
Java Programming
Java Programming  Hi, What is Java Programming? How I can learn Java Programming in one month? Thanks

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.