Hi i need to add crosshairs to my code, like draw 2 lines across the screen on a JPanel, i also need to be able to turn them on and off with a checkbox which is on a JPanel. the other panel needs to be drawable on like a paint program. But even if I could get the crosshairs working it would be a huge help.

//import classes
import javax.swing.*; 


import java.awt.*;
import java.awt.Paint;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class v2 extends JFrame { //class name
	
	
	public static void main(String[] args) // start of main method
	{
		
		
		jane();
	
		
		

	}
	 public static void jane()
	 {
		//make frame
		 
			
			Frame f1 = new JFrame();
			f1.setVisible(true);
			f1.setTitle("Mirror Draw");
			f1.setSize(640, 480);
			f1.setAlwaysOnTop(true);
			f1.setLocation(100, 100); //this code sets where the Jframe appears when the program is run
		   // f1.setResizable(false);
			
			JPanel contentPane = new JPanel(); //create panel
			
			contentPane.setSize(480, 480);
			contentPane.setBackground(Color.LIGHT_GRAY);
			//contentPane.setLocation(0, 0);
			//contentPane.setLayout(null); //this allows me to place items wherever I want in the panel
			//f1.add(contentPane); //add content pane(Panel) to JFrame
			
			JPanel buttonPane = new JPanel();	
			buttonPane.setBackground(Color.RED);
			buttonPane.setLocation(480, 0);
			buttonPane.setSize(160, 480);
			//f1.add(buttonPane);
		
			
			JButton btnHorizonal = new JButton("Horizontal Mirroring");
			btnHorizonal.setVisible(true);
			btnHorizonal.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
				
					
					
				}
			});
			
			JButton btnVertical = new JButton("Vertical Mirroring    ");
			btnVertical.setVisible(true);
			btnVertical.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
				
					
					
				}
			});
			
			JButton btnDiagonal = new JButton("Diagonal Mirroring   ");
			btnDiagonal.setVisible(true);
			btnDiagonal.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
				
					
					
				}
			});
			
			JButton btnRed = new JButton("         ");
			btnRed.setBackground(Color.red);
			
			JButton btnBlue = new JButton("        ");
			btnBlue.setBackground(Color.blue);
			
			JButton btnGreen = new JButton("         ");
			btnGreen.setBackground(Color.GREEN);
			
			JButton btnBlack = new JButton("        ");
			btnBlack.setBackground(Color.BLACK);
			
			JButton btnClear = new JButton("Clear All Drawing");
			
			JCheckBox crossHairs = new JCheckBox("Crosshairs");
			
			
			
			
			buttonPane.add(btnHorizonal);
			buttonPane.add(btnVertical);
			buttonPane.add(btnDiagonal);
			buttonPane.add(btnRed);
			buttonPane.add(btnBlue);
			buttonPane.add(btnGreen);
			buttonPane.add(btnBlack);
			buttonPane.add(crossHairs);
			buttonPane.add(btnClear);
			
			
			f1.add(buttonPane);
			f1.add(contentPane);
			
			
			
			
			
			
					
					
				}
		
	 }

Override the JPanel class's paintComponent method and do the drawing there. You can make the drawing conditional on values set by other methods the the program,

I dont really understand that to be honest im dreadful at it

Which part?
Create a class that extends JPanel
Override that class's paintComponent method
Add code to that method that draws the lines
Create an instance of that class
Add that instance of the class to the GUI (JFrame?)

i have a class called painting which creates the cross lines
public void paintComponent(Graphics g){
g.drawLine(240,0,240,480);
g.drawLine(0,240,480,240);
}
which draws them when i add in a main method in that class,
but how do i create an instance and add them the the jpanel?
could you give me the lines of code just to add them?

Does Painting extend the JPanel class? It should.
Painting aPntng = new Painting(); // create an instance
theJFrame.add(aPntng, <layout info>); // add instance to the JFrame

For <layout info> see the API doc for the layout manager and for the add() method.
For example it could be: BorderLayout.CENTER If you are using BorderLayout manager.

//import classes
import javax.swing.*;


import java.awt.*;
import java.awt.Paint;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class v2 extends JFrame { //class name


	public static void main(String[] args) // start of main method
	{


		jane();




	}
	 public static void jane()
	 {
		//make frame


			Frame f1 = new JFrame();
			f1.setVisible(true);
			f1.setTitle("Mirror Draw");
			f1.setSize(640, 480);
			f1.setAlwaysOnTop(true);
			f1.setLocation(100, 100); //this code sets where the Jframe appears when the program is run
		   // f1.setResizable(false);

			JPanel contentPane = new JPanel(); //create panel

			contentPane.setSize(480, 480);
			contentPane.setBackground(Color.LIGHT_GRAY);
			//contentPane.setLocation(0, 0);
			//contentPane.setLayout(null); //this allows me to place items wherever I want in the panel
			//f1.add(contentPane); //add content pane(Panel) to JFrame

			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(Color.RED);
			buttonPane.setLocation(480, 0);
			buttonPane.setSize(160, 480);
			//f1.add(buttonPane);


			JButton btnHorizonal = new JButton("Horizontal Mirroring");
			btnHorizonal.setVisible(true);
			btnHorizonal.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {




				}
			});

			JButton btnVertical = new JButton("Vertical Mirroring    ");
			btnVertical.setVisible(true);
			btnVertical.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {




				}
			});

			JButton btnDiagonal = new JButton("Diagonal Mirroring   ");
			btnDiagonal.setVisible(true);
			btnDiagonal.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {




				}
			});

			JButton btnRed = new JButton("         ");
			btnRed.setBackground(Color.red);

			JButton btnBlue = new JButton("        ");
			btnBlue.setBackground(Color.blue);

			JButton btnGreen = new JButton("         ");
			btnGreen.setBackground(Color.GREEN);

			JButton btnBlack = new JButton("        ");
			btnBlack.setBackground(Color.BLACK);

			JButton btnClear = new JButton("Clear All Drawing");

			JCheckBox crossHairs = new JCheckBox("Crosshairs");

			painting p1 = new painting();
			contentPane.add(p1);





			buttonPane.add(btnHorizonal);
			buttonPane.add(btnVertical);
			buttonPane.add(btnDiagonal);
			buttonPane.add(btnRed);
			buttonPane.add(btnBlue);
			buttonPane.add(btnGreen);
			buttonPane.add(btnBlack);
			buttonPane.add(crossHairs);
			buttonPane.add(btnClear);


			f1.add(buttonPane);
			f1.add(contentPane);











				}

	 }
import javax.swing.*;
    import java.awt.*;
    public class painting extends JPanel{
    public void paintComponent(Graphics g){
    g.drawLine(240,0,240,480);
    g.drawLine(0,240,480,240);
    }

    //public static void main(String args[]){
   // JFrame jf = new JFrame();
   // jf.add(new painting());


    //jf.setSize(500,500);
   // jf.setVisible(true);

   }

ive tried what you said but it doesnt seem to work for some reason? thats all the
code i have so if you can see the error in it that would be great anything with the // is commented out

it doesnt seem to work

Please explain?

When I uncomment the main() method, the Painting class compiles, executes and draws lines.

It has to run in the v2 class, not the painting class, but
I cant get it running in the v2 class these are two completely
seperate classes. It needs to be in the panel called
content pane.

In the V2 class you could create an instance of the Painting class and add it to the V2 class.

It wont work for some reason?
I made a paint p1 = new painting();
Then contentPane.add(p1);
Still got a blank screen

Please write a small simple program that compiles, executes and shows what you are doing and post it here.
There is no way to tell what you are doing without seeing the code.

I gave all the code i have above i just need the lines
In the 2nd class to show up in the first class
On the contentpane

Can you make a new smaller program to show the problem. The above code is old and not the code that is uptodate with the change I suggested.

Its not old its still the same the problem is i cant fix it
Ive tried what you said it didnt work so i removed it
As it was unneccesary as it didnt work i want the
Code from the second class to run in the first class
In the contentpane jpanel which is the gray one not
The red one... Can you make it run? I just want the 2
Lines to show up on my gray jpanel if you run it you
Will see the gray jpanel

Make a small program that compiles, executes and shows the problem.
Leave your big program alone until you can get the Painting class to be added to a window and draw the lines. Work on making the small program work.


Then go back to the large program and using the logic and techniques you worked out with the small program to add the class to do the drawing.

Yeah I Got it working now had to restart it
But now i have to draw chicken nuggets
On it in a fish bowl while a girraffe eats a tree
And loads of other random stuff
How would i draw them?
And like the user is supposed to draw them?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.