Jump to content
  • Advertisement
Sign in to follow this  
Gallivan

[java] Java2D With An Application?

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm very confused regarding drawing simple 2D shapes with a JAVA application. In school we learned how to draw with an Applet, but now I am looking to draw with an application. Will I be using paint(Graphics g) or Swing? I've read http://java.sun.com/docs/books/tutorial/2d/overview/index.html and http://java.sun.com/docs/books/tutorial/2d/index.html but I get confused when I see the import JApplet and various commands. Are there any tutorials out there for applications?

Share this post


Link to post
Share on other sites
Advertisement
I just had a quick browse through the tutorials and couldn't identify the part you mentioned.

Could you specify which part of the tutorials that is confusing you (maybe cut and paste the paragraph)?

Depending on what you are trying to do, its possible to use paint(g) to draw in your swing component.

(pseudo code)

class Swingwindow extends JFrame

public void paint(graphics g){

super.paint(g);
g.drawLine(0,0,50,50);
}

Java will display swing components without a draw/paint call, but if you need anything else it must be drawn or painted.

Share this post


Link to post
Share on other sites
That actually was all I needed. I guess I was just under the assumption that 'paint' was for Applets only. Here's what I ended up with, thanks to your pseudo code:


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

public class Swingwindow extends JFrame {

public static void main(String[] args) {
JFrame f = new JFrame("Weather Wizard");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
super.paint(g2);
g2.drawLine(0,0,50,50);
}
}

Share this post


Link to post
Share on other sites
I'm glad to hear you found your way...

Paint is a method from Component (or maybe its Container), so any class under that will have access to the paint method through inheritance. This includes JApplet, which is a sub-class of Component/Container.

Its a good idea to find some meaning in draw/paint/repaint/drawimage/paintcomponent/update and so on.

Here are a few links:

http://leepoint.net/notes-java/GUI-lowlevel/graphics/15who-calls-paintcomponent.html

http://java.sun.com/products/jfc/tsc/articles/painting/index.html

Share this post


Link to post
Share on other sites
Quote:
Original post by Gallivan
That actually was all I needed. I guess I was just under the assumption that 'paint' was for Applets only.
If you want to do your own painting in Swing it's preferable not to override paint but instead to override paintComponent, for more information see the links above in MONDARIZ's post.

Share this post


Link to post
Share on other sites
I'm having trouble adding a JLabel to a direct position on-screen.

The documentation reads:

add(javax.swing.JLabel jLab, int x, int y, int dx, int dy)
Adds a JLabel at position x;y on the window and with a width/height of dx/dy.


But my code of:

frame.getContentPane().add(unitLabel, x, y, 16, 16);


Gives me an error of:

cannot find symbol - method add(javax.swing.JLabel,int,int,int,int)

This was my reference:

http://ltiwww.epfl.ch/sJava/version2/javadocDir/epfl/lti/Utilities/AFrame.html

Share this post


Link to post
Share on other sites
I've tried this:

unitLabel.setBounds(x, y, unitImage.getIconWidth(), unitImage.getIconHeight());

It compiles, but the image icon remains centered in my frame.

Share this post


Link to post
Share on other sites
I think its because you add to the toplevel container (directly to the JFrame).
JFrame inherits the add() from Container and that add() does not accept the parameter (unitLabel, x, y, 16, 16).

It accepts these:

add(Component comp)
add(Component comp, int index)
add(Component comp, Object constraints)
add(Component comp, Object constraints, int index)
add(String name, Component comp)


You should look at the LayoutManagers, maybe the GridBagLayout can help you.

You can also post your entire code, then it might be easier to explain whats happening.

Share this post


Link to post
Share on other sites

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

public class Display extends JFrame {

public void main(String[] args) {
createGUI();
showUnit(0,0);
showUnit(0,0);
}

protected ImageIcon createImageIcon(String path) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}

private void createGUI() {

}

private void showUnit(int x, int y) {
JFrame frame = new JFrame("TopLevelDemo");
ImageIcon unitImage = createImageIcon("images/middle.gif");
JLabel unitLabel = new JLabel(unitImage);
unitLabel.setOpaque(true);
unitLabel.setBackground(new Color(248, 213, 131));
unitLabel.setPreferredSize(new Dimension(16, 16));
unitLabel.setBounds(x, y, unitImage.getIconWidth(), unitImage.getIconHeight());
frame.getContentPane().add(unitLabel);
frame.pack();
frame.setVisible(true);
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!