Drawing in general (NOT RTP)
Author |
Message |
Insectoid
|
Posted: Wed Oct 22, 2008 12:15 pm Post subject: Drawing in general (NOT RTP) |
|
|
Hi,
I Really want to be able to 'draw' things in Java, but it isn't in my school's curriculum, which means I have to either learn it for myself or take what small amounts of time my teacher has available to teach. I did the latter, but the explanation was rushed and not very...explaining. So, I got this far into drawing a simple line. I does not work, and to you guys that will be obvious why. Help?
Java: |
import java.awt.*;
import javax.swing.*;
public class Draw_Test {
public static void main(String [] args) {
JFrame window = new JFrame ();
window.setSize (100, 100);
window.setVisible (true);
gui
}
public void gui (Graphics2D g){
g.drawLine (10, 10, 50, 50);
}
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
[Gandalf]
|
Posted: Wed Oct 22, 2008 3:17 pm Post subject: RE:Drawing in general (NOT RTP) |
|
|
There's a bit more to it in Swing, it's probably easier with Applets, but that's a different story. Also, your gui() call needs to have the brackets, and obviously pass in a Graphics2D object. However, in general, you won't be doing that.
I suggest reading through this tutorial on the java.sun.com website:
http://java.sun.com/docs/books/tutorial/uiswing/painting/step3.html
Keep in mind that it might look a bit more complicated than it actual is, since after all it is Java. I've found the tutorials occasionally complicate things by making them overly Java-esque, such as always using Dimension objects and so on. Try to get the main idea and apply it to your program, don't just copy their code. You'll need paint() and update() methods, foremost.
Edited to sound less discouraging and for clarity. |
|
|
|
|
|
Insectoid
|
Posted: Wed Oct 22, 2008 4:58 pm Post subject: RE:Drawing in general (NOT RTP) |
|
|
I don't want to do it in applets, I want to do it in an application. More complicated = more bonus for me! I actually did have a look at some examples/tutorials, but they were all either applets or more complicated than they needed to be. I didn't notice the gui() missing brackets, this is a slightly older version of my code. Doesn't matter, the logic (or rather lack of it) is the same. |
|
|
|
|
|
[Gandalf]
|
Posted: Wed Oct 22, 2008 5:02 pm Post subject: RE:Drawing in general (NOT RTP) |
|
|
Nevertheless, much of the same advice remains, for both Applets and AWT/Swing. For example, for your example to work as you expect you'll need a paint() method. |
|
|
|
|
|
Insectoid
|
Posted: Wed Oct 22, 2008 6:32 pm Post subject: Re: Drawing in general (NOT RTP) |
|
|
I looked over the tutorial, and have a general idea as to how it works. There are several spots I don't understand, such as this:
code: |
SwingUtilities.invokeLater(new Runnable() {
public void run() {
|
and this:
code: | super.paintComponent(g); |
So far, I have got a window with a border, but the line won't draw. There are no syntax errors, and I can't figure out where the problem is. So, without further rambling, revision!
Java: |
import javax.swing.*;
import java.awt.*;
public class Draw_Test {
public static void main(String[] args) {
JFrame frame = new JFrame ("Draw Test");
frame.setSize (250, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MyPanel());
frame.setVisible(true); //paint (null);
}
static void paint (Graphics g){
}
}
class MyPanel extends JPanel {
public MyPanel (){
setBorder (BorderFactory.createLineBorder(Color.red));
}
public void paintcomponent (Graphics g){
super.paintComponent (g);
g.drawLine(10,10,50,50);
}
}
|
I suppose, after all this code, anything else I want to draw will go inside paintComponent?
EDIT: D'oh, in paintComponent, I spelled it with a little 'C'! problem solved! Huzzah! |
|
|
|
|
|
DemonWasp
|
Posted: Wed Oct 22, 2008 9:06 pm Post subject: RE:Drawing in general (NOT RTP) |
|
|
If you get bonus marks for making it "complicated", try this: http://www.jmonkeyengine.com/
It's actually pretty easy, but it's well beyond the complexity your teacher is probably expecting of you . |
|
|
|
|
|
Insectoid
|
Posted: Thu Oct 23, 2008 11:45 am Post subject: RE:Drawing in general (NOT RTP) |
|
|
Actually, no. I just wanted to learn to draw things in applications, as I haven't started learning applets yet. Now I just have to learn re-drawing. |
|
|
|
|
|
[Gandalf]
|
Posted: Thu Oct 23, 2008 6:21 pm Post subject: RE:Drawing in general (NOT RTP) |
|
|
Look into repaint() and update(). |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|