Results 1 to 2 of 2
  1. #1
    Grid_iso is offline Member
    Join Date
    May 2011
    Posts
    12
    Rep Power
    0

    Default help importing pictures into paint component of another class.

    so I have this jpanel and it works fine- compiles and runs (unfinished) and I want to split things up so I can deal with the actions etc here but have objects in other classes to keep things clean. Here is my jpanel right now.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Color;
    
    public class A3JPanel extends JPanel { //implements KeyListener, ActionListener
    	private Timer t;
    
     	public A3JPanel() {
    		setBackground(Color.white);
    		//t= new Timer(30,this);
    		//addKeyListener(this);
    	}
    	Color lightBrown = new Color(150,60,40);
    	Color darkBrown = new Color(100,10,40);
    
       	public void paintComponent(Graphics g){
     	    super.paintComponent(g);
    		g.setColor(Color.GREEN);
    		g.fillRect(0, 0, 600, 500);
    		g.setColor(Color.BLUE);
    		g.fillRect(0, 400, 600, 100);
    		g.setColor(darkBrown);
    		g.fillRect(20, 430, 15, 100);
    		g.fillRect(100, 430, 15, 100);
    		g.fillRect(180, 430, 15, 100);
    		g.fillRect(260, 430, 15, 100);
    		g.fillRect(340, 430, 15, 100);
    		g.fillRect(420, 430, 15, 100);
    		g.fillRect(500, 430, 15, 100);
    		g.fillRect(580, 430, 15, 100);
    		g.setColor(Color.BLUE);
    		g.fillRect(0, 480, 600, 30);
    		g.setColor(lightBrown);
    		g.fillRect(0, 425, 600, 15);
    		g.setColor(Color.YELLOW);
    		g.fillRect(0, 0, 600, 20);
    		g.setColor(Color.GREEN);
    		g.fillRect(7,3, 30, 14);
    		g.fillRect(44,3, 30, 14);
    		g.fillRect(81,3, 30, 14);
    		g.fillRect(118,3, 30, 14);
    		g.fillRect(155,3, 30, 14);
    		g.fillRect(192,3, 30, 14);
    		g.fillRect(229,3, 30, 14);
    		g.fillRect(266,3, 30, 14);
    		g.fillRect(303,3, 30, 14);
    		g.fillRect(340,3, 30, 14);
    		g.fillRect(377,3, 30, 14);
    		g.fillRect(414,3, 30, 14);
    		g.fillRect(451,3, 30, 14);
    		g.fillRect(488,3, 30, 14);
    		g.fillRect(525,3, 30, 14);
    		g.fillRect(562,3, 30, 14);
    		
    	}
    
    }
    I want all of what is printed out to be put in another class, but last time I tried doing this I had some issues and it wouldn't print out, how would I go about inserting g.setColor(Color.GREEN); to g.fillRect(562,3, 30, 14); all in to its own class called something like background

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default

    To execute code in a method in another class, create an instance of the other class, save the reference to that class object and then use that reference to call the method.

Similar Threads

  1. Replies: 22
    Last Post: 07-25-2012, 09:51 PM
  2. Replies: 10
    Last Post: 04-11-2011, 09:34 PM
  3. using paint methode in another class
    By imorio in forum New To Java
    Replies: 8
    Last Post: 08-12-2010, 03:17 PM
  4. Custom component and paint outside of bounds
    By happy_hippie in forum AWT / Swing
    Replies: 2
    Last Post: 06-30-2010, 05:41 PM
  5. importing class files
    By bizso in forum New To Java
    Replies: 6
    Last Post: 03-28-2009, 02:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •