New Posts  All Forums:Forum Nav:

Java Help!

post #1 of 16
Thread Starter 
I don't understand where paintComponent(Graphics g) is being called in this program. Where is graphics g ever made?

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

public class TestPanelDrawing extends JFrame
{
     public TestPanelDrawing()
     {
          add(new NewPanel());
     }

     public static void main(String[] args)
     {
          TestPanelDrawing frame = new TestPanelDrawing();

          frame.setTitle("TestPanelDrawing");
          frame.setLocationRelativeTo(null);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(100,100);
          frame.setVisible(true);
     }
}

class NewPanel extends JPanel
{
     protected void paintComponent(Graphics g)
     {
          super.paintComponent(g);
          g.drawLine(0,0,50,50);
          g.drawString("Banner",0,40);
     }
}
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 #2 of 16
Its been a while since I've done java, but it looks like that paintComponent function is just overloaded from JPanel. Therefore, whenever a JPanel's paintComponent function would be called, the NewPanel's is instead. The graphics object is probably created in the constructor of the JPanel... or somewhere similar. Any Java nuts want to chime in?
It's about time!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 @3.3Ghz Gigabyte EP45-UD3P Sapphire 2900Pro Flashed to XT 4Gb Gskill 1066Mhz PK's 
Hard DriveOptical DriveOSMonitor
Seagate Barracuda 320Gb & WD Black 640Gb Lite On Vista Business and VMWare Ubuntu Acer AL2223W 22" 
KeyboardPowerCaseMouse
Ergonomic 4000 Corsair HX 620W CM 690 G5 
  hide details  
Reply
It's about time!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 @3.3Ghz Gigabyte EP45-UD3P Sapphire 2900Pro Flashed to XT 4Gb Gskill 1066Mhz PK's 
Hard DriveOptical DriveOSMonitor
Seagate Barracuda 320Gb & WD Black 640Gb Lite On Vista Business and VMWare Ubuntu Acer AL2223W 22" 
KeyboardPowerCaseMouse
Ergonomic 4000 Corsair HX 620W CM 690 G5 
  hide details  
Reply
post #3 of 16
Its an argument for the function. 'Graphics' is used as a data type and 'g' is the assigned name of the value. For example.

Code:
paintComponent(20)
'g' would then be equal to 20 when executing the code. Thats just an example i didn't read it well enough to get the jist.

I'm not 100% sure i'm just guessing from what i know, and what i know comes from C#, not Java.
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 #4 of 16
Thread Starter 
Quote:
Originally Posted by Licht View Post
Its an argument for the function. 'Graphics' is used as a data type and 'g' is the assigned name of the value. For example.

Code:
paintComponent(20)
'g' would then be equal to 20 when executing the code. Thats just an example i didn't read it well enough to get the jist.

I'm not 100% sure i'm just guessing from what i know, and what i know comes from C#, not Java.
The thing is, the program never explicitly creates a graphics object, g and it never explicitly calls the method.
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 #5 of 16
Quote:
Originally Posted by pauldovi View Post
The thing is, the program never explicitly creates a graphics object, g and it never explicitly calls the method.
Very puzzling.
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 #6 of 16
i'm not familiar much with java, but could it be a parent method called from the two imports at the top of the code?
post #7 of 16
Java does that stuff behind the scenes. When you do something with graphics it makes a new thread which is invisible to you. Somewhere in that thread it creates the graphics object and calls paintComponent. You don't need to know the context in which it is called, but you do need to specify what happens when the call is made
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 #8 of 16
Is this code that was provided to you by your professor?
Legendary
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6750 ASUS P5B Deluxe XFX 8600GT 2 x 2GB G.Skill PQ 
Hard DriveOSMonitorKeyboard
Seagate 250GB Windows 7 x64 Samsung 225BW Saitek Eclipse 
PowerCaseMouseMouse Pad
PC P&C Quad 750W Silencer Thermaltake Tsunami Dream Logitech MX518 Func 1030 
  hide details  
Reply
Legendary
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6750 ASUS P5B Deluxe XFX 8600GT 2 x 2GB G.Skill PQ 
Hard DriveOSMonitorKeyboard
Seagate 250GB Windows 7 x64 Samsung 225BW Saitek Eclipse 
PowerCaseMouseMouse Pad
PC P&C Quad 750W Silencer Thermaltake Tsunami Dream Logitech MX518 Func 1030 
  hide details  
Reply
post #9 of 16
Thread Starter 
Quote:
Originally Posted by TheLegend View Post
Is this code that was provided to you by your professor?
Book. And it doesn't make logical 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 #10 of 16
Quote:
Originally Posted by pauldovi View Post
Book. And it doesn't make logical sense to me.
Wish I could be of more help, but I don't have much experience with Java graphics.
Legendary
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6750 ASUS P5B Deluxe XFX 8600GT 2 x 2GB G.Skill PQ 
Hard DriveOSMonitorKeyboard
Seagate 250GB Windows 7 x64 Samsung 225BW Saitek Eclipse 
PowerCaseMouseMouse Pad
PC P&C Quad 750W Silencer Thermaltake Tsunami Dream Logitech MX518 Func 1030 
  hide details  
Reply
Legendary
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6750 ASUS P5B Deluxe XFX 8600GT 2 x 2GB G.Skill PQ 
Hard DriveOSMonitorKeyboard
Seagate 250GB Windows 7 x64 Samsung 225BW Saitek Eclipse 
PowerCaseMouseMouse Pad
PC P&C Quad 750W Silencer Thermaltake Tsunami Dream Logitech MX518 Func 1030 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming