473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Graphics Error

I have been trying to make a java graphics program but whenever i try to make it work nothing happens, no window appears. Here is my source code:

import java.awt.*;

public class graphics {
public static void main(String args[]){

}
public void paint(Graphics g){
g.drawRect(300, 200, 75, 75);
}
}
p.s. the reason i have a public static void main without anything in it is because otherwise when i run it, it says i need to have a public static void main(String args[]) in it.
Aug 22 '07 #1
11 2246
Nepomuk
3,112 Expert 2GB
public static void main(String[] args) is the main method - the program does, what this function tells it to do. So if you just change it like this:
Expand|Select|Wrap|Line Numbers
  1. public static void main(String args[]){
  2.     paint(graphic); // "graphic" being, what ever should be drawn
  3. }
  4.  
you should at least get some reaction. However, I haven't used awt yet, so I don't know, if that will be your problem solved.
Aug 22 '07 #2
r035198x
13,262 8TB
I have been trying to make a java graphics program but whenever i try to make it work nothing happens, no window appears. Here is my source code:

import java.awt.*;

public class graphics {
public static void main(String args[]){

}
public void paint(Graphics g){
g.drawRect(300, 200, 75, 75);
}
}
p.s. the reason i have a public static void main without anything in it is because otherwise when i run it, it says i need to have a public static void main(String args[]) in it.
OK. Um, you need to be painting somewhere you see. Now you don't paint to the console like that. Read Sun's painting tutorial to see how it's done.
Aug 22 '07 #3
Does that mean that i should place the public void paint(Graphics g) should be within the public static void main
Aug 22 '07 #4
r035198x
13,262 8TB
Does that mean that i should place the public void paint(Graphics g) should be within the public static void main
All that stuff is wrong. See my reply above.
Aug 22 '07 #5
Nepomuk
3,112 Expert 2GB
Does that mean that i should place the public void paint(Graphics g) should be within the public static void main
You will have to use the public static void main at some point, but as r035198x pointed out, there are other things to be done too.
Aug 22 '07 #6
JosAH
11,448 Expert 8TB
I have been trying to make a java graphics program but whenever i try to make it work nothing happens, no window appears. Here is my source code:

import java.awt.*;

public class graphics {
public static void main(String args[]){

}
public void paint(Graphics g){
g.drawRect(300, 200, 75, 75);
}
}
p.s. the reason i have a public static void main without anything in it is because otherwise when i run it, it says i need to have a public static void main(String args[]) in it.
The JVM invokes your public static void main(String[] args) allright, but that
method is just empty so it doesn't do anything. Who or what do you expect to
invoke that public void paint(Graphics g) method? Only AWT Components
or Swing JComponents have that method invoked when needed, but you
don't have any of those. Please read a Swing/AWT tutorial before you expect
automagic things to happen.

kind regards,

Jos
Aug 22 '07 #7
r035198x
13,262 8TB
The JVM invokes your public static void main(String[] args) allright, but that
method is just empty so it doesn't do anything. Who or what do you expect to
invoke that public void paint(Graphics g) method? Only AWT Components
or Swing JComponents have that method invoked when needed, but you
don't have any of those. Please read a Swing/AWT tutorial before you expect
automagic things to happen.

kind regards,

Jos
There's no such word as automagic.
Aug 22 '07 #8
JosAH
11,448 Expert 8TB
There's no such word as automagic.
Yes there is; you just saw it so it exists. It even associates with quite a lot of
other things so it's an existing, semantically rich word; it can even be a noun.

automagically yours,

Jos ;-)
Aug 22 '07 #9
praveen2gupta
201 100+
I have been trying to make a java graphics program but whenever i try to make it work nothing happens, no window appears. Here is my source code:

import java.awt.*;

public class graphics {
public static void main(String args[]){

}
public void paint(Graphics g){
g.drawRect(300, 200, 75, 75);
}
}
p.s. the reason i have a public static void main without anything in it is because otherwise when i run it, it says i need to have a public static void main(String args[]) in it.
Hi
I think you are not aware that what is your problem. Better Read Applets that will be good for the better understanding of your program.
Aug 23 '07 #10
I can make the window appear but nothing appears on it. This is my code:

Expand|Select|Wrap|Line Numbers
  1.  import java.awt.Graphics;
  2.  
  3. import javax.swing.SwingUtilities;
  4. import javax.swing.JFrame;
  5.  
  6. public class graphicsprogram {
  7.  
  8.     public static void main(String[] args) {
  9.         SwingUtilities.invokeLater(new Runnable() {
  10.             public void run() {
  11.                 createAndShowGUI();
  12.             }
  13.         });
  14.     }
  15.  
  16.    public void paint(Graphics g){
  17.  g.drawRect(100,100,75,75);
  18. }
  19.     private static void createAndShowGUI() {
  20.         JFrame f = new JFrame("graphics program");
  21.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         f.setSize(250, 250);
  23.         f.setVisible(true); 
  24.    }
  25. }
Aug 26 '07 #11
r035198x
13,262 8TB
I can make the window appear but nothing appears on it. This is my code:

Expand|Select|Wrap|Line Numbers
  1.  import java.awt.Graphics;
  2.  
  3. import javax.swing.SwingUtilities;
  4. import javax.swing.JFrame;
  5.  
  6. public class graphicsprogram {
  7.  
  8.     public static void main(String[] args) {
  9.         SwingUtilities.invokeLater(new Runnable() {
  10.             public void run() {
  11.                 createAndShowGUI();
  12.             }
  13.         });
  14.     }
  15.  
  16.    public void paint(Graphics g){
  17.  g.drawRect(100,100,75,75);
  18. }
  19.     private static void createAndShowGUI() {
  20.         JFrame f = new JFrame("graphics program");
  21.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         f.setSize(250, 250);
  23.         f.setVisible(true); 
  24.    }
  25. }

See what they used for a drawing surface in this example.
You really need to go through that tutorial before you start writing some code.
Aug 27 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: SamSpade | last post by:
Public Function PicCreateGraphics() As Graphics 'Client should dispose this PicCreateGraphics = Graphics.FromImage(mDocumentImage) mPicCreateGraphicsSaved = PicCreateGraphics 'Saved so I can...
8
by: Nathan Sokalski | last post by:
I am trying to write code to rotate a graphic that I have. Here is the code I am currently using: Dim frogbitmap As New Bitmap(Drawing.Image.FromFile(Server.MapPath("images/frog.gif"))) Dim...
8
by: Nathan Sokalski | last post by:
I am trying to write code to rotate a graphic that I have. Here is the code I am currently using: Dim frogbitmap As New Bitmap(Drawing.Image.FromFile(Server.MapPath("images/frog.gif"))) Dim...
1
by: IvoShalev | last post by:
Hi there, I just want to give some sudgestions on how to draw some plain things only using the header file <graphics.hand of course the standart files <stdio.h<conio.h<stdlib.h>. First of All...
4
by: Anil Gupte | last post by:
I copied the following almost directly from http://msdn2.microsoft.com/en-us/library/6xe5hazb.aspx Dim instance As Graphics Dim text As String 'Dim strfont As Font Dim returnValue As...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.