Results 1 to 2 of 2
  1. #1
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default How to open a class from another class

    I have two classes and i want to open one from another how do i do this.

    StartScreen.java
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class StartScreen extends JFrame implements ActionListener
    {
    	public static void main(String [] args)
    	{
    	new StartScreen();	
    	}
    	
    	private JButton button1;
    	
    	public StartScreen()
    	{
    		this.setSize(300, 150);
    		this.setTitle("Start");
    		JPanel panel1 = new JPanel();
    		button1 = new JButton ("CLICK TO START");
    		button1.addActionListener(this);
    		panel1.add(button1);
    		this.add(panel1);
    		this.setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		if (e.getSource() == button1){this is where i want to get the other class from}
    	}
    	}
    Pong.java

    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.geom.*;
    import java.util.Scanner;
    import java.util.concurrent.*;
    
    public class Pong extends JFrame
    {
    	private static final long serialVersionUID = 1L;
    	public static final int WIDTH = 900;
    	public static final int HEIGHT = 725;
    	int x;
    	int y=0;
    	private PaintSurface canvas;
    public Pong()
    	{
    		this.setSize(WIDTH, HEIGHT);
    		canvas = new PaintSurface();
    		this.add(canvas, BorderLayout.CENTER);
    		this.setVisible(true);
    		ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
    		executor.scheduleAtFixedRate(new AnimationThread(this),
    		0L, 20L, TimeUnit.MILLISECONDS);
    	}
    }
    
    class AnimationThread implements Runnable
    {
    	Pong c;
    
    	public AnimationThread(Pong pong)
    	{
    		this.c = pong;	
    
    	}
    
    	public void run()
    	{
    		c.repaint();
    	}
    }
    
    class PaintSurface extends JComponent // this is the class i want to open
    {
    	private static final long serialVersionUID = 1L;
    	int x_pos = 0;
    	int y_pos = 0;
    	double x_speed = 1;
    	double y_speed = 1;
    	int d = 20;
    	int width = Pong.WIDTH;
    	int height = Pong.HEIGHT;
    	int x;
    	int o = 0;
    	public PaintSurface()
    	{
    		addMouseMotionListener(new MouseMotionAdapter() 
    		{
    			public void mouseMoved(MouseEvent e) {
    				x = e.getX();
    			}
    			
    		});		
    	}
    	
    	public void paint(Graphics g) {
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
    		if (x_pos < 0) {
    			x_speed = -x_speed;
    		}
    		if (x_pos > width - d) {
    			x_speed = -x_speed;
    		}
    
    		if (y_pos < 0) {
    			y_speed = -y_speed;
    			;
    		}
    
    		if (y_pos + 20 > 675 && x_pos > x && x_pos < x + 50) {
    			y_speed = -(y_speed + .5);
    			x_speed = x_speed + .5;
    			o = (o + 1);
    		}
    
    		if (y_pos > 695)
    		{System.exit(8);}
    		String p = Integer.toString(o);
    		x_pos += x_speed;
    		y_pos += y_speed;
    		Shape ball = new Ellipse2D.Float(x_pos, y_pos, d, d);
    		g2.setColor(Color.BLACK);
    		g2.fill(ball);
    		Shape paddle = new Rectangle.Float(x, 675, 50, 8);
    		g2.setColor(Color.BLACK);
    		g2.fill(paddle);		
    		g2.drawString("score " + (p), 7, 10);}
    	
    	public static void main(String[] args) {
    		new Pong();
    	}
    }

  2. #2
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: How to open a class from another class

    nevermind figured it out myself

Similar Threads

  1. how to open class(jpanel) from external jar?
    By skadron_alfa in forum New To Java
    Replies: 3
    Last Post: 01-15-2012, 06:41 AM
  2. Replies: 6
    Last Post: 07-25-2011, 11:34 AM
  3. how can i open a .class in a text editor
    By footballdude2k3 in forum New To Java
    Replies: 1
    Last Post: 04-14-2011, 10:00 PM
  4. Open next class help
    By Tastosis in forum New To Java
    Replies: 2
    Last Post: 03-04-2011, 02:12 AM
  5. How do I open a file with a class, or can I?
    By bobleny in forum New To Java
    Replies: 4
    Last Post: 04-02-2009, 10:59 AM

Posting Permissions

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