File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes scrolls and pics and drawing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "scrolls and pics and drawing" Watch "scrolls and pics and drawing" New topic

scrolls and pics and drawing

rob armstrong
Ranch Hand

Joined: Jul 25, 2005
Posts: 77
Just finishing a program that has a jpg in the background (large pic) and needs a scrollbar and that i can draw on the top by making lines. I can get it to draw the lines and have no picture. I can get a picture and no lines will draw or I can have a picture and make lines and have no scrollbar! Just can't have it all at once. What am i doin wrong here?

Key points...
class Testing extends JFrame
Image img;
ImageIcon picture;
JLabel labelPic;
JMenuItem newMap;
JMenuItem openMap;
JMenuItem exitMap;
JMenuItem eraseLastLine;
JMenuItem searchTrackNum;
ArrayList<Shape> shapes=new ArrayList<Shape>();

public Testing()
blah blah
JPanel p = new JPanel();
ImageIcon picture=new ImageIcon("c:\\programing\\javaclasses\\Trackbuilder\\9998.jpg");
JLabel labelPic=new JLabel(picture);
this.add(new PaintSurface()); //test
p.setPreferredSize(new Dimension(5000,5000));
int height;
int width;
p.setPreferredSize(new Dimension(width,height));
JScrollPane sp = new JScrollPane (p);
getContentPane().add(sp); // lose scroll if i disable this but can draw and have a pic

paint class here ...
public void paint(Graphics g)
g.drawImage(img,0,0,this); // this makes the pic appear
Graphics2D g2=(Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

Color[] colors={Color.RED, Color.BLUE, Color.PINK, Color.YELLOW, Color.CYAN};
int colorIndex=0;
g2.setStroke(new BasicStroke(2));
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));

for (Shape s:shapes)

if(startDrag !=null && endDrag !=null)
Shape r=new Line2D.Float(startDrag.x, startDrag.y, endDrag.x, endDrag.y);

thanks in advance..
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The JFrame paint method is a Container method and is called to render the JFrame and all of its child components (see api). Using this requires some care and might be better left until you have had more practice. An easy way to get started is to use a separate JComponent and do your rendering in its paintComponent method.
There are so many ways to put these things together that it's difficult to talk about. Your personal preferences/requirements play a large part in this. Java gives us a lot to work with and you can do about anything you want with it. The flip side is that there are a lot of pitfalls and ways to get into trouble.
So here is a minimal example to help you get started:

And here are some resources that you can use to learn how things work:
1 — two links found on this page Trail: Creating a GUI with JFC/Swing:
....a — Lesson: Performing Custom Painting
....b — Trail: 2D Graphics (which it looks like you have found)
2 — Unleash Your Creativity with Swing and the Java 2D API! which contains additional links.
Imagination, patience and practice can be good friends in this.

edit: tags for sublist, formatting resources
[ February 09, 2006: Message edited by: Craig Wood ]
rob armstrong
Ranch Hand

Joined: Jul 25, 2005
Posts: 77
ok thanks ill take a look at it and try and understand it on a printed copy.
rob armstrong
Ranch Hand

Joined: Jul 25, 2005
Posts: 77
Wow this one kind of slammmed me! I thought I just about had it figured out till I got your reply and found out it was totally wrong. Looks like I will have to get back into the books and figure it out.

I agree. Here's the link:
subject: scrolls and pics and drawing
It's not a secret anymore!