New Posts  All Forums:Forum Nav:

Java File I/O Help - Page 3

post #21 of 30
Quote:
Originally Posted by pauldovi View Post
Is this ^^^ possible to do?
That is not possible. bottomDisplay is declared locally within your main method. None of the other classes will have access to that instance unless you pass it into a method somehow. Why not just give each class their own version of the setXYCoord method?
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
post #22 of 30
Thread Starter 
Quote:
Originally Posted by C-bro View Post
That is not possible. bottomDisplay is declared locally within your main method. None of the other classes will have access to that instance unless you pass it into a method somehow. Why not just give each class their own version of the setXYCoord method?
Is there any way to declare bottomDisplay non locally?

I thought that was what public was for.
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 30
Quote:
Originally Posted by pauldovi View Post
Is there any way to declare bottomDisplay non locally?

I thought that was what public was for.
Declare it outside of a class.

Code:
main{
   global;
   private void example()
   {
      local;
   }
}
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
post #24 of 30
Quote:
Originally Posted by pauldovi View Post
Is there any way to declare bottomDisplay non locally?

I thought that was what public was for.
Public allows external access to internal elements of a class. setXYCoord is public so all other classes have access to that method, but if ScrollablePicture doesn't have any reference to an AddComponents object in the first place, the public modifier doesn't help.

I know you do a lot of C programming, and it seems more like you're breaking things into classes rather than methods. Just because you can make objects, doesn't necessarily mean you should. I would try and combine those methods into a single class, but that's just me.

Code:
public class StartGUI extends JFrame
{
          AddComponents bottomDisplay;
// even though the variables are now global, there 
// is no way to access it from INSIDE of ScrollablePicture.
// You need a method as a way to pass bottomDisplay INTO ScrollablePicture for modification. 
      public void main(String[] args)
     {
          bottomDisplay = new AddComponents();
          ScrollablePicture mapWindow = new ScrollablePicture();
     }
}

public class AddComponents extends JPanel
{
     int xCoord;
     int yCoord;

     public void setXYCoord(int x, int y)
     {
          xCoord = x;
          yCoord = y;
     }
}

public class ScrollablePicture extends JLabel
                                       inplements Scrollable,
                                                      MouseMotionListener
{
     public void mouseMoved(MouseEvent e)
     {
          bottomDisplay.setXYCoord(e.geyX(),e.getY());
     }
}
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
post #25 of 30
Thread Starter 
Quote:
Originally Posted by C-bro View Post
Public allows external access to internal elements of a class. setXYCoord is public so all other classes have access to that method, but if ScrollablePicture doesn't have any reference to an AddComponents object in the first place, the public modifier doesn't help.

I know you do a lot of C programming, and it seems more like you're breaking things into classes rather than methods. Just because you can make objects, doesn't necessarily mean you should. I would try and combine those methods into a single class, but that's just me.

Code:
public class StartGUI extends JFrame
{
          AddComponents bottomDisplay;
// even though the variables are now global, there 
// is no way to access it from INSIDE of ScrollablePicture.
// You need a method as a way to pass bottomDisplay INTO ScrollablePicture for modification. 
      public void main(String[] args)
     {
          bottomDisplay = new AddComponents();
          ScrollablePicture mapWindow = new ScrollablePicture();
     }
}

public class AddComponents extends JPanel
{
     int xCoord;
     int yCoord;

     public void setXYCoord(int x, int y)
     {
          xCoord = x;
          yCoord = y;
     }
}

public class ScrollablePicture extends JLabel
                                       inplements Scrollable,
                                                      MouseMotionListener
{
     public void mouseMoved(MouseEvent e)
     {
          bottomDisplay.setXYCoord(e.geyX(),e.getY());
     }
}
Thanks man.

I am really having a hard time with Java. I understand C very well and it seems very logical to me. Java on the otherhand doesn't.

If I pass an Object to a class, is that passing the object by value or by reference. I guess what I am trying to get at is, if the passed object changes after I pass it, will the receiving object / method be able to see that?
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 #26 of 30
Quote:
Originally Posted by pauldovi View Post
Thanks man.

I am really having a hard time with Java. I understand C very well and it seems very logical to me. Java on the otherhand doesn't.

If I pass an Object to a class, is that passing the object by value or by reference. I guess what I am trying to get at is, if the passed object changes after I pass it, will the receiving object / method be able to see that?
When you pass an object in java you're passing it by reference. Only primitives (int, char, etc) get passed by value. Any change to an object will be reflected in any class that the object was passed to
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #27 of 30
Thread Starter 
Okay, so I created a class DrawPoint. The idea is that I create an instance of this class which draws a point on the specified JPanel:

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

public class DrawPoint
{
    int x;
    int y;
    Color color;
    JPanel panel;
    
    
    DrawPoint(JPanel panel1, int xPoint, int yPoint, Color color1)
    {
        x = xPoint;
        y = yPoint;
        color = color1;
        panel = panel1;
    }
    
    public void paintComponent(Graphics g)
    {
        panel.paintComponent(g);
        g.setColor(color);
        g.fillOval(x, y, 10, 10);
        
    }
}
However, the code has a problem with panel.paintComponent(Graphics g).

It says:

"The method paintComponent(Graphics) from the type JComponent is not visible"

What am I doing wrong?
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 #28 of 30
I believe paintComponent(Graphics G), in the context of a JPanel, is a private method. In other words, you can't call it directly from outside the class. You should use either panel.repaint(); or panel.paintImmediately(panel.getBounds()); The first option will put the request in a sort of queue, so when the resources are available, it'll repaint the panel. The second option puts the current thread on hold, paints the panel and continues on afterwards. The paintImmediately should be used if you are doing real-time outputs or tracking.

Edit: I used this chunk of code to paint a player marker on a Monopoly game board that I made. You can use it as a template for drawing a circle to a JPanel.

Code:
public class PlayerMarker extends javax.swing.JPanel{

    private int x=401;
    private int y=411;
    private java.awt.Color col;
    private boolean dispose=false;
    
    public PlayerMarker() {
        super();
    }
    
    public PlayerMarker(java.awt.Color c) {
        super();
        col = c;
    }
    
    public void paint(java.awt.Graphics g){
        java.awt.Graphics Gr = this.getGraphics();
        if(Gr != null)
            Gr.dispose();
        if(dispose)
            g.setClip(0,0,1,1);
        g.setColor(col);
        g.fillOval(x,y,21,21);
        g.setColor(java.awt.Color.BLACK);
        g.drawOval(x,y,20,20);
    }
    
    public void setColor(java.awt.Color c){
        col = c;
    }
    
    public void setCoord(int xloc, int yloc){
        x = xloc;
        y = yloc;
        repaint();
    }
    
    public void dispose(){
        dispose = true;
    }
}
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
post #29 of 30
Thread Starter 
Quote:
Originally Posted by C-bro View Post
I believe paintComponent(Graphics G), in the context of a JPanel, is a private method. In other words, you can't call it directly from outside the class. You should use either panel.repaint(); or panel.paintImmediately(panel.getBounds()); The first option will put the request in a sort of queue, so when the resources are available, it'll repaint the panel. The second option puts the current thread on hold, paints the panel and continues on afterwards. The paintImmediately should be used if you are doing real-time outputs or tracking.

Edit: I used this chunk of code to paint a player marker on a Monopoly game board that I made. You can use it as a template for drawing a circle to a JPanel.

Code:
public class PlayerMarker extends javax.swing.JPanel{

    private int x=401;
    private int y=411;
    private java.awt.Color col;
    private boolean dispose=false;
    
    public PlayerMarker() {
        super();
    }
    
    public PlayerMarker(java.awt.Color c) {
        super();
        col = c;
    }
    
    public void paint(java.awt.Graphics g){
        java.awt.Graphics Gr = this.getGraphics();
        if(Gr != null)
            Gr.dispose();
        if(dispose)
            g.setClip(0,0,1,1);
        g.setColor(col);
        g.fillOval(x,y,21,21);
        g.setColor(java.awt.Color.BLACK);
        g.drawOval(x,y,20,20);
    }
    
    public void setColor(java.awt.Color c){
        col = c;
    }
    
    public void setCoord(int xloc, int yloc){
        x = xloc;
        y = yloc;
        repaint();
    }
    
    public void dispose(){
        dispose = true;
    }
}
We are not suppose to use the getGraphics method because when you resize the screen it goes away.

I tried using the paintComponent method but it does not display the item:

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

public class GUI extends JFrame
{        
    public static void main(String[] args)
    {
        Color currentColor = Color.red;
        
        GUI gui = new GUI();
        gui.setLayout(new BorderLayout());
                
        mouseClass mouse1 = new mouseClass(currentColor);
            
        gui.add(mouse1, BorderLayout.CENTER);
        
        gui.setLocationRelativeTo(null);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(500,500);
        gui.setVisible(true);
    }
    
}
Code:
import java.awt.Color;
import java.awt.event.*;

import javax.swing.*;

public class mouseClass extends JPanel
                        implements MouseListener, MouseMotionListener
{
    Color currentColor;
    boolean currentClickStatus = false;
    int x1,x2,y1,y2;
    DrawLine currentLine;
    
    mouseClass(Color currentColor1)
    {
        currentColor = currentColor1;
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }
    
    public int getX1()
    {
        return x1;
    }
    
    public int getY1()
    {
        return y1;
    }
    
    public void mouseClicked(MouseEvent e)
    {
        this.add(new DrawPoint(this, e.getX(),e.getY(),currentColor));
        
        if(currentClickStatus == false)
        {
            currentClickStatus = true;
            x1 = e.getX();
            y1 = e.getY();
        }
        else
        {
            x2 = e.getX();
            y2 = e.getY();
            
            this.add(new DrawLine(this, x1, y1, x2, y2, currentColor));
            
            x1 = x2;
            y1 = y2;
        }
    }
    
    public void mousePressed(MouseEvent e)
    {
        
    }
    
    public void mouseReleased(MouseEvent e)
    {
        
    }
    public void mouseEntered(MouseEvent e)
    {
        
    }
    public void mouseExited(MouseEvent e)
    {
        
    }
    public void mouseDragged(MouseEvent e)
    {
        
    }
    public void mouseMoved(MouseEvent e)
    {
        boolean runOnce = false;
        
        if(currentClickStatus == true)
        {
            if(runOnce == false)
            {
                currentLine = new DrawLine(this, x1, y1, e.getX(), e.getY(), currentColor);
                //runOnce = true;
            }
        }
    }
}
Code:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;

public class DrawPoint extends JPanel
{
    private int x;
    private int y;
    private Color color;
    private JPanel panel;
    
    DrawPoint(JPanel panel1, int xPoint, int yPoint, Color color1)
    {
        x = xPoint;
        y = yPoint;
        color = color1;
        panel = panel1;
        
        //Graphics point = panel.getGraphics();
        //point.setColor(color);
        //point.fillOval(x-2,y-2,5,5);
    }
    
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(color);
        g.fillOval(x-2,y-2,5,5);
    }
}
Code:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;

public class DrawLine extends JPanel
{
    private int x1, x2, y1, y2;
    private Color color;
    JPanel panel;
    Graphics line;
    
    DrawLine(JPanel panel1, int xPoint1, int yPoint1, int xPoint2, int yPoint2, Color color1)
    {
        x1 = xPoint1;
        x2 = xPoint2;
        y1 = yPoint1;
        y2 = yPoint2;
        color = color1;
        panel = panel1;
        
        //line = panel.getGraphics();
        //line.setColor(color);
        //line.drawLine(x1, y1, x2, y2);
    }
    
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(color);
        g.drawLine(x1,y1,x2,y2);
    }
}
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 #30 of 30
Rather than using paintComponent(Graphics g), try over riding paint(Graphics g){} with that same code you used in paintComponent. Then by default whenever you call repaint(), it should do what you overrode in paint(). In my example, the only reason I used getGraphics() was to delete the current graphic image that was painted. I'm not sue what you mean by "it goes away" when you resize. Perhaps you mean that when you resize the window, the reference returned by getGraphics() changes? If that's the case, it's not an issue because that line is only used to check if there is an existing graphic drawn.
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming