954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

dram line on a Jpanel

:( Sorry the topic spelled incorrectly.. I can't edit it noe.. sorry for that

Friends again need you so badly.. I'm developing my simple GIS application, actually here's what I developed so far..

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


 class GUItest01 {

 

public void go(){
    JFrame frame = new JFrame("app");
    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ImageIcon image = new ImageIcon("C:/icon.jpg");

    JLabel label = new JLabel(image);
    label.setPreferredSize(new Dimension(1200, 1000));
    JScrollPane jScrollPane = new JScrollPane(label);


    JPanel panel = new JPanel();

    

    frame.add(jScrollPane, BorderLayout.CENTER);
    //frame.setResizable(false);
   
    panel.setBackground(Color.cyan);
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    JButton button = new JButton("Built Environment");
    JButton button1 = new JButton("Natural Environment");
    JButton button2 = new JButton("Roads Environment");
    JButton button3 = new JButton("Traffic Sensors");
    JButton button4 = new JButton("Refresh");

    panel.add(button);
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);

    frame.getContentPane().add(BorderLayout.EAST,panel);
    frame.setSize(1500, 700);
    frame.pack();
    frame.setVisible(true);
    
    }


    public static void main(String[] args) {
        GUItest01 tg = new GUItest01();
        tg.go();
    }
}

I want to know how can I draw lines one the plane which the image on it.. there are many examples on the web but I can't find out how to implement those in this code.. please can you show me how to draw a single line.. thank you all

baby_c
Junior Poster
112 posts since May 2010
Reputation Points: 47
Solved Threads: 0
 
can you show me how to draw a single line


Create a class that extends JPanel, override the paintComponent method,
add a call to the Graphics drawLine() to the paint method, add the panel to your GUI where you want to see the line.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
Create a class that extends JPanel, override the paintComponent method, add a call to the Graphics drawLine() to the paint method, add the panel to your GUI where you want to see the line.


thank you very much.. Can you just give a code example for that. I unable to code it thats ehy i'm asking.. thanks again

baby_c
Junior Poster
112 posts since May 2010
Reputation Points: 47
Solved Threads: 0
 

There must be code samples here on the Forum or Google.
Have you tried Searching here for drawLine?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
Create a class that extends JPanel, override the paintComponent method, add a call to the Graphics drawLine() to the paint method, add the panel to your GUI where you want to see the line.


What did you mean by "add the panel to your GUI where you want to see the line " ??

baby_c
Junior Poster
112 posts since May 2010
Reputation Points: 47
Solved Threads: 0
 

The "panel" refers to the class that extends JPanel. You add an instance of that class to a container in your GUI.
"Where you want to see the line" is the location on your GUI where you want to see the line.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
The "panel" refers to the class that extends JPanel. You add an instance of that class to a container in your GUI. "Where you want to see the line" is the location on your GUI where you want to see the line.


yeah.. got it.. thank you. I drew an oval...

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


 class GUItest01 extends javax.swing.JFrame {



public GUItest01(){
    JFrame frame = new JFrame("GIS Application");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ImageIcon image = new ImageIcon("C:/icon.jpg");

    JLabel label = new JLabel(image);

    label.setPreferredSize(new Dimension(1200, 1000));
    JScrollPane jScrollPane = new JScrollPane(new Image(image));


    JPanel panel = new JPanel();



    frame.add(jScrollPane, BorderLayout.CENTER);
    //frame.setResizable(false);
   
    panel.setBackground(Color.cyan);
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    JButton button = new JButton("Built Environment");
    JButton button1 = new JButton("Natural Environment");
    JButton button2 = new JButton("Roads Environment");
    JButton button3 = new JButton("Traffic Sensors");
    JButton button4 = new JButton("Refresh");

    panel.add(button);
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);

    frame.getContentPane().add(BorderLayout.EAST,panel);
    frame.setSize(1500, 700);
    frame.pack();
    frame.setVisible(true);


    }

class Image extends JLabel{

        public Image(ImageIcon image) {
            super(image);
        }
        @Override
     public void paintComponent(Graphics g){
             super.paintComponent(g);
            g.fillOval(400, 300, 50, 50);
    }


}

    public static void main(String[] args) {
        GUItest01 tg = new GUItest01();
        


    }
}
baby_c
Junior Poster
112 posts since May 2010
Reputation Points: 47
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: