New Posts  All Forums:Forum Nav:

Java Help - Page 3

post #21 of 25
Quote:
Originally Posted by TheLegend View Post
This should do the trick. Let me know how it works. Keep in mind you'll need to change some of the code to work with your coordinates in the frame as well as the actual file name.
Hope you don't mind, I just complied that code and corrected some of it. There was 1 error i couldn't figure out how to fix. Here's the kind-of-fixed code (it contains 1 error):

Code:

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

public class UserInterface extends JPanel
{
    Dimension screenResolution;
    int sizeX;
    int sizeY;
    
    public UserInterface()
    {
        create displayMap();
    }
    
    public void startGUI()
    {
        JFrame frame = new JFrame("Plot Vacation");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getResolution();
        frame.setSize(sizeX, sizeY);
        frame.setLocation((sizeX/6),(sizeY/6));
        frame.setVisible(true);
    }
    
    public static void main(String[] args)
    {
        UserInterface GUI = new UserInterface();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GUI.startGUI();
    }
    
    public void getResolution()
    {        
        screenResolution = new Dimension(); 
        
        screenResolution = Toolkit.getDefaultToolkit().getScreenSize();
        
        sizeX = screenResolution.width*3/4;
        sizeY = screenResolution.height*3/4;
        
    }
    
    public void displayMap(JFrame frame)
    {
        //add content pane and set layout to null
       Container contentPane = getContentPane();
       contentPane.setBackground(Color.YOURCHOICE);
       contentPane.setLayout(null);
 
       //Set up the path for the image
       String strRelativePath = this.getClass().getResource("").toString();
       if(strRelativePath.startsWith("file:"))
       strRelativePath = strRelativePath.substring(6);
       strRelativePath = strRelativePath.replace(" " ," ");
       strRelativePath = strRelativePath.replaceAll("%20", " ");
 
       //set up frame
       frame = new JFrame();
       frame.setIcon(new ImageIcon(strRelativePath + "YOURIMAGENAME"));
       frame.setBounds(X,X,X,X);
       frame.setHorizontalAlignment(frame.CENTER);
       contentPane.add(frame); 
   }
}
The error the code contains is:

Code:
UserInterface.java:14: ';' expected
        create displayMap();
                         ^
1 error
I hoped I've helped a little bit, I'm doing a project in Java right now in my computing class and we're doing a lot of GUI work, unfortunately we aren't doing it the same way you're doing it here, so I tried to fix what i could - if what i did even fixes anything at all - i managed to reduce 5 errors down to one by adding semi colons where necessary, and deleting certain bits of text like the "\\" that was in second part of
Code:
strRelativePath = strRelativePath.replace(" " ," ");
basically when you do a backslash in a string you can set a new line or what not by doing "\
". Erm... I hope thats clear. It doesn't sound very clear to me. Anyway, I hoped I've helped.
post #22 of 25
Thread Starter 
Thanks for the help guys. I figured it out and I will post my code later. Not sure if it was the best way of doing it, but it worked and more or less made sense to me.
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
post #23 of 25
I just remembered I've got a word document that I refer to whenever I need help with Java Graphics. It has loads of useful code and explanations on how to use it (and its legal to share of course). If you want me to send you it, PM me and I'll get in touch.
post #24 of 25
just remember http://java.sun.com/j2se/1.4.2/docs/api/ makes everything easier!
Meh system
(13 items)
 
  
CPUMotherboardGraphicsRAM
e6750 @ 3.6(1.456 vcore.. load) Gigabyte p35-DS3R 8800gt 512mb 2gb Crucial Tracer ddr2-6400 @5-5-5-15 900mhz 
Hard DriveOptical DriveOSMonitor
2x 320 seagate (raid 0) some LG dvd/cd combo xp pro x86 22" samsung 2ms 
KeyboardPowerCaseMouse
dell quietkey Corsair 520Watt antec 900 518 
Mouse Pad
qck 
  hide details  
Reply
Meh system
(13 items)
 
  
CPUMotherboardGraphicsRAM
e6750 @ 3.6(1.456 vcore.. load) Gigabyte p35-DS3R 8800gt 512mb 2gb Crucial Tracer ddr2-6400 @5-5-5-15 900mhz 
Hard DriveOptical DriveOSMonitor
2x 320 seagate (raid 0) some LG dvd/cd combo xp pro x86 22" samsung 2ms 
KeyboardPowerCaseMouse
dell quietkey Corsair 520Watt antec 900 518 
Mouse Pad
qck 
  hide details  
Reply
post #25 of 25
Thread Starter 
Okay, new problem. I got my imagine up and I have also added scroll bars. I am trying to register a listener to move the map when the scroll bar is moved. Here is my code:

The problem is, Java says that "AdjustmentListener cannot be resolved to a type" at the bottom of my AddComponents class.

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

public class StartGUI extends JFrame
{
    StartGUI()
    {
        
    }
    
    public static void main(String[] args)
    {
        StartGUI frame = new StartGUI();
        frame.setTitle("VacPlot");
        frame.setLocation(findSize('x'),findSize('y'));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(findSize());
        
        AddComponents fill = new AddComponents();
        fill.addMap();
        fill.addScrollBar();
        
        frame.add(fill);
        
        frame.setVisible(true);
    }
    
    public static Dimension findSize()
    {
        Dimension desiredScreenResolution = new Dimension();
        
        desiredScreenResolution = Toolkit.getDefaultToolkit().getScreenSize();
        
        desiredScreenResolution.setSize(desiredScreenResolution.getWidth()*3/4, desiredScreenResolution.getHeight()*3/4);
        
        return desiredScreenResolution;
    }
    
    public static int findSize(char dimension)
    {
        Dimension desiredScreenResolution = new Dimension();
        
        desiredScreenResolution = Toolkit.getDefaultToolkit().getScreenSize();
        
        desiredScreenResolution.setSize(desiredScreenResolution.getWidth()*3/24, desiredScreenResolution.getHeight()*3/24);
        
        if(dimension == 'x')
        {
            return desiredScreenResolution.width;
        }
        else if(dimension == 'y')
        {
            return desiredScreenResolution.height;
        }
        else
        {
            return 0;
        }
    }

}
Code:
import javax.swing.*;
import java.awt.*;


public class AddComponents extends JPanel
{
        
    public void addMap()
    {
        ImageIcon map = new ImageIcon("teritory-map.gif");
        
        setLayout(new BorderLayout());
        add(new JLabel(map), BorderLayout.CENTER);
    }
    
    public void addScrollBar()
    {
        JScrollBar horizontalScroll = new JScrollBar(JScrollBar.HORIZONTAL);
        JScrollBar verticalScroll = new JScrollBar(JScrollBar.VERTICAL);
        
        add(horizontalScroll, BorderLayout.SOUTH);
        add(verticalScroll, BorderLayout.EAST);
    }
    
    horizontalScroll.addAdjustmentListener(new AdjustmentListener()
    {
        public void adjustmentValueChanged(AdjustmentEvent e)
        {
            double value = horizontalScroll.getValue();
            double maximumVal = horizontalScroll.getMaximum();
            double newX = (value * messagePanel.getWidth() / maximumValue);
            
            messagePanel.setXCoordinate((int) newX);
        }
    });
    
    verticalScroll.addAdjustmentListener(new AdjustmentListener()
    {
        public void adjustmentValueChanged(AdjustmentEvent e)
        {
            double value = verticalScroll.getValue();
            double maximumVal = verticalScroll.getMaximum();
            double newY = (value * messagePanel.getHeight() / maximumValue);
            
            messagePanel.setXCoordinate((int) newY);
        }
    });
}
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2500k ASRock P67 Extreme4 Gen 3 AMD 7970 16GB DDR3 
Hard DriveOptical DriveOSMonitor
Intel 520 256GB SATA DVD Burner Windows 7 64 bit Deal U2410 
KeyboardPowerMouse
Adesso Mechanical Silverstone OP650 Logitech G700 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming