Hello, this is my first java program. I am attempting to get a racecar gif to show up on my scrollable pane racetrack gif. Here is my code. The track shows up in the scrollable pane but the car does not. Any direction would be greatly appreciated.
It looks like I need to add something to the paintcomponent of ScrollablePicture. When I add a circle like this it draws it:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
img = getToolkit().createImage("00Flames0.gif");
g.fillOval(10, 10, 10, 10);
}
When I go to change the oval to a draw image, it draws nothing?
protected void paintComponent(Graphics g) {
super.paintComponent(g);
img = getToolkit().createImage("00Flames0.gif");
/* g.fillOval(10, 10, 10, 10);*/
g.drawImage(img, 10, 10, this);
}
Thank you,
Dale
package CarOnTrack;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Canvas;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.image.*;
import java.net.*;
import javax.imageio.*;
public class CarOnTrack extends JPanel {
private JPanel mainPanel;
private JLabel imageLabel;
public CarOnTrack (){
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("CarOnTrack");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel)frame.getContentPane();
ScrollDemo trackscrollable = new ScrollDemo();
mainPanel = new JPanel();
Icon myIcon = new ImageIcon("00Flames0.gif");
imageLabel = new JLabel(myIcon);
mainPanel.add(imageLabel);
mainPanel.setVisible(true);
trackscrollable.add(mainPanel,1,1);
panel.add(trackscrollable);
frame.setBounds(0,0,500,300);
frame.setVisible(true);
}
public static void main(String[] args)
{
System.out.println("BEGIN PROGRAM");
CarOnTrack cot = new CarOnTrack();
System.out.println("END PROGRAM");
}
}
package CarOnTrack;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.*;
public class ScrollDemo extends JPanel {
private ScrollablePicture picture;
private ScrollablePicture picture2;
Image trackgif;
public ScrollDemo() {
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
//Get the image to use.
ImageIcon trackgif = createImageIcon("Daytona.gif");
//Set up the scroll pane.
picture = new ScrollablePicture(trackgif, 1);
JScrollPane pictureScrollPane = new javax.swing.JScrollPane(picture);
pictureScrollPane.setPreferredSize(new Dimension(200, 100));
pictureScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black));
add(pictureScrollPane);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ScrollDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
}
});
}
}
package CarOnTrack;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScrollablePicture extends JLabel
implements Scrollable,
MouseMotionListener {
private int maxUnitIncrement = 1;
private boolean missingPicture = false;
public ScrollablePicture(ImageIcon i, int m) {
super(i);
if (i == null) {
missingPicture = true;
setText("No picture found.");
setHorizontalAlignment(CENTER);
setOpaque(true);
setBackground(Color.white);
}
maxUnitIncrement = m;
//Let the user scroll by dragging to outside the window.
setAutoscrolls(true); //enable synthetic drag events
addMouseMotionListener(this); //handle mouse drags
}
//Methods required by the MouseMotionListener interface:
public void mouseMoved(MouseEvent e) { }
public void mouseDragged(MouseEvent e) {
//The user is dragging us, so scroll!
Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
scrollRectToVisible(r);
}
public Dimension getPreferredSize() {
if (missingPicture) {
return new Dimension(320, 480);
} else {
return super.getPreferredSize();
}
}
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction) {
//Get the current position.
int currentPosition = 0;
if (orientation == SwingConstants.HORIZONTAL) {
currentPosition = visibleRect.x;
} else {
currentPosition = visibleRect.y;
}
//Return the number of pixels between currentPosition
//and the nearest tick mark in the indicated direction.
if (direction < 0) {
int newPosition = currentPosition -
(currentPosition / maxUnitIncrement)
* maxUnitIncrement;
return (newPosition == 0) ? maxUnitIncrement : newPosition;
} else {
return ((currentPosition / maxUnitIncrement) + 1)
* maxUnitIncrement
- currentPosition;
}
}
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation,
int direction) {
if (orientation == SwingConstants.HORIZONTAL) {
return visibleRect.width - maxUnitIncrement;
} else {
return visibleRect.height - maxUnitIncrement;
}
}
public boolean getScrollableTracksViewportWidth() {
return false;
}
public boolean getScrollableTracksViewportHeight() {
return false;
}
public void setMaxUnitIncrement(int pixels) {
maxUnitIncrement = pixels;
}
}