• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scrolls and pics and drawing

 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
p.add(labelPic);
img=picture.getImage();
this.add(new PaintSurface()); //test
p.setPreferredSize(new Dimension(5000,5000));
int height;
height=picture.getIconHeight();
int width;
width=picture.getIconWidth();
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)
{
g2.setPaint(Color.BLACK);
g2.draw(s);
}

if(startDrag !=null && endDrag !=null)
{
g2.setPaint(Color.LIGHT_GRAY);
Shape r=new Line2D.Float(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
g2.draw(r);
}
}

thanks in advance..
roba
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks ill take a look at it and try and understand it on a printed copy.
Thanks
roba
 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

roba
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic