Problem in drawing a line in Java Application Program

Hi,
I am trying to draw a line after a message is displayed on the screen. But the line is not at all coming on the screen while running. but I can see a red line is appearing on the screen first and and it is getting overridden.
There is something wrong in the concept what I understood about graphics.Can anybody help to
understand where the problem is ?
Here is the part of the code which used to draw line.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date.*;
import java.text.*;
import java.lang.*;
import MyPack.*;
class chalan_pay extends JFrame
JTextField jt1,jt2,jt3,jt4,jt5;
JLabel jlh,jl1,jl2,jl3,jl4,jl5,jl10;
JButton bt1,bt2,bt3;
Choice ch1,ch2,ch3,ch4;
Connection con = null;
int i1no,i2no,i3no,i4no,itno;
String idate;
public chalan_pay()
getContentPane().setLayout(null);
jt1= new JTextField(5);
jt1.setBounds(110,150,50,20);
jt2= new JTextField(10);
jt2.setBounds(400,150,100,20);
jt3= new JTextField(3);
jt3.setBounds(590,150,30,20);
jt4= new JTextField(2);
jt4.setBounds(750,150,30,20);
jlh= new JLabel();
jlh.setText("CHALLAN DETAILS");
jlh.setBounds(300,50,200,20);
jl1= new JLabel();
jl1.setText("IGM No. ");
jl1.setBounds(50,150,100,20);
jl2= new JLabel();
jl2.setText("IGM Date ");
jl2.setBounds(340,150,100,20);
jl3= new JLabel();
jl3.setText("Line No. ");
jl3.setBounds(530,150,100,20);
jl4= new JLabel();
jl4.setText("Subline No. ");
jl4.setBounds(680,150,100,20);
jl10= new JLabel();
jl10.setBounds(100,200,300,20);
ch1= new Choice();
ch1.setBounds(170,150,150,20);
getContentPane().add(ch1);
ch1.addItemListener(new Opt1());
bt1= new JButton("Exit");
bt1.setBounds(200,600,100,20);
getContentPane().add(bt1);
bt1.addActionListener(new Ex());
try
con=db_connect.getConnection();
Statement st1 = con.createStatement();
ResultSet rs1 = st1.executeQuery("select igm_no,to_char(igm_dt,'DD-MON-YY'),line_no,subline_no from "+
"det_item order by igm_no");
while(rs1.next())
ch1.addItem(String.valueOf(rs1.getInt(1))+" "+rs1.getString(2)+" "+
String.valueOf(rs1.getInt(3))+" "+String.valueOf(rs1.getInt(4)));
rs1.close();
st1.close();
catch(Exception e){e.printStackTrace();}
getContentPane().add(jlh);
getContentPane().add(jl1);
getContentPane().add(jt1);
getContentPane().add(jl2);
getContentPane().add(jt2);
getContentPane().add(jl3);
getContentPane().add(jt3);
getContentPane().add(jl4);
getContentPane().add(jt4);
getContentPane().add(jl10);
setSize(900,700);
setVisible(true);
show();
public void paint(Graphics g)
g.setColor(Color.red);
g.drawLine(0,300,600,300);
class Ex implements ActionListener
public void actionPerformed(ActionEvent evt)
if(evt.getSource() == bt1)
dispose();
return;
This code is incomplete. The program is compiled and Ran. I am unable to see the Line.
Is it because , I am using contentPane ?. Please help me to cake it clear.
mjava

I have no idea what JTutor is, but if it tells you to override paint() in Swing, it's not worth its disk space.
[http://java.sun.com/docs/books/tutorial/uiswing/painting/closer.html] clearly says that for all practical purposes paintComponent will be the only method that you will ever need to override.

Similar Messages

  • What's the coolest JAVA application program you've seen?

    What's the coolest JAVA application program you've seen, comemrcial or non-commercial?
    You can post more than one. :p

    public class Hello {
       public static void main(String[] args) {
          System.out.println("Hello, " + (args.length == 0 ? "World" : args[0]) + "!");
    }

  • Drawing a line in java 1.4

    I am drawing a line using the method
    drawPolyline(int[],int[],int) of the Graphics class.
    It works fine in all cases except when i try to draw
    an exactly vertical line or an exactly horizontal
    line. In these 2 case, it does not draw a line at all.
    All. i can see are the 2 selection end points.
    This works fine in jdk1.3 but not in jdk1.4
    Please lemme know ASAP as to what the problem and what i should do?
    Thanks
    Jatin

    You forgot to add this to your paint() code ...
    g.setColor(Color.BLACK)
    You'll never see anything drawn unless you set a color different thatn background.
    This works, I added code so you can run it in a frame too because I don't want to be bothered with dealing with applets and html files;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Lab11 extends Applet implements MouseListener, MouseMotionListener {
        int x0, y0, x1, y1;
        //public void init() {
             public Lab11() {
             addMouseListener(this);
             addMouseMotionListener(this);
        public void mouseDragged(MouseEvent e)
             x1 = e.getX();
             y1 = e.getY();
             repaint();
        public void mouseMoved(MouseEvent e) { }
        public void mouseClicked(MouseEvent e) { }
        public void mouseEntered(MouseEvent e) { }
        public void mouseExited (MouseEvent e) { }
        public void mousePressed(MouseEvent e) {
         x0 = e.getX();
         y0 = e.getY();
         System.out.println("Mouse pressed at: (" +
                      x0 + ", " + y0 + ")" );
        public void mouseReleased(MouseEvent e) {
         x1 = e.getX();
         y1 = e.getY();
         System.out.println("Mouse released at: (" +
                      x1 + ", " + y1 + ")" );
         this.repaint();
       public void paint(Graphics g) {
            g.setColor(Color.BLACK);
         g.drawLine(x0, y0, x1, y1);
        public static void main(String[] argv)
             JFrame f = new JFrame("Test");
             f.getContentPane().add(new Lab11());
             f.setVisible(true);
             f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

  • Drawing a Line in JAVA

    Well Ive got my program to draw a line, but when I drag the mouse across the screen I'd like it to show its actual progress. Ill try to explain this better, if I click my mouse on one point on the screen and drag it to the enxt the line doesnt show up untill I reach my final spot when i release my mouse. What Id like for it to do is to be able to start the line and then have it show the lines progress while Im dragging it. I dont want it to hide the line or whatever. Can anyone give me some help with this? Heres both my java and HTML code.
    Java:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Lab11 extends Applet implements MouseListener {
        int x0, y0, x1, y1;
        public void init() {
         addMouseListener(this);
        public void mouseClicked(MouseEvent e) { }
        public void mouseEntered(MouseEvent e) { }
        public void mouseExited (MouseEvent e) { }
        public void mousePressed(MouseEvent e) {
         x0 = e.getX();
         y0 = e.getY();
         System.out.println("Mouse pressed at: (" +
                      x0 + ", " + y0 + ")" );
        public void mouseReleased(MouseEvent e) {
         x1 = e.getX();
         y1 = e.getY();
         System.out.println("Mouse released at: (" +
                      x1 + ", " + y1 + ")" );
         this.repaint();
       public void paint(Graphics g) {
         g.drawLine(x0, y0, x1, y1);
    }HTML:
    <html>
      <head>
        <title>Lab 11 Applets</title>
      </head>
      <body bgcolor=white>
        <applet code="Lab11.class" width=300 height=300>
        </applet>
      </body>
    </html>Thanks guys!

    You forgot to add this to your paint() code ...
    g.setColor(Color.BLACK)
    You'll never see anything drawn unless you set a color different thatn background.
    This works, I added code so you can run it in a frame too because I don't want to be bothered with dealing with applets and html files;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Lab11 extends Applet implements MouseListener, MouseMotionListener {
        int x0, y0, x1, y1;
        //public void init() {
             public Lab11() {
             addMouseListener(this);
             addMouseMotionListener(this);
        public void mouseDragged(MouseEvent e)
             x1 = e.getX();
             y1 = e.getY();
             repaint();
        public void mouseMoved(MouseEvent e) { }
        public void mouseClicked(MouseEvent e) { }
        public void mouseEntered(MouseEvent e) { }
        public void mouseExited (MouseEvent e) { }
        public void mousePressed(MouseEvent e) {
         x0 = e.getX();
         y0 = e.getY();
         System.out.println("Mouse pressed at: (" +
                      x0 + ", " + y0 + ")" );
        public void mouseReleased(MouseEvent e) {
         x1 = e.getX();
         y1 = e.getY();
         System.out.println("Mouse released at: (" +
                      x1 + ", " + y1 + ")" );
         this.repaint();
       public void paint(Graphics g) {
            g.setColor(Color.BLACK);
         g.drawLine(x0, y0, x1, y1);
        public static void main(String[] argv)
             JFrame f = new JFrame("Test");
             f.getContentPane().add(new Lab11());
             f.setVisible(true);
             f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

  • Problems with File I/O in  java applications at KVM/CLDC Layer

    I wrote couple of programs made use of Connector.open with "file" protocol to perform File I/O.
    Code snippet from java application for File Read
    InputConnection conn = (InputConnection)
    Connector.open("file:/input.txt", Connector.READ );
    I got javax.microedition.io.ConnectionNotFoundException: The requested protocol does not exist file:/input.txt
    Code snippet from java application for File Write
    OutputConnection conn = (OutputConnection)
    Connector.open( "file:/input.txt;append=true", Connector.WRITE );
    I got javax.microedition.io.ConnectionNotFoundException: The requested protocol does not exist file:/input.txt;append=true
    I have couple of questions
    1. Does Connector class support "file" protocol at KVM/CLDC layer of Sun's reference implementation?
    2. If so, what could I be doing wrong?
    Thanks,
    Nuthan
    I'm looking forward to hear from you.

    I believe you can find the right answer here :
    http://forum.java.sun.com/thread.jsp?forum=50&thread=307980
    Hope that will help.
    bye

  • HOW to play flash inside java application program

    Do java provide any API for playing flash inside java application ?Pleae give me some help about it .Thank You ..

    pingyi wrote:
    Do java provide any API for playing flash inside java application ?Pleae give me some help about it .Thank You ..bro... I found out there from the internet... but you need to purchase it.
    hope it meets your expectation...
    JFlashPlayer is a Flash Player API Java package that lets developers play and interact with Adobe? Flash Player movies within Java applications. Advanced animations can improve the user experience and add to the professional appearance of an application. JFlashPlayer allows programmers to quickly add Flash movies to their Java application development for Windows.taken from: JflashPlayer

  • Problem in making exe file for java application

    helo,
    i tried to make an windows installer packge for my jar file using advance installer. There is one option for including JRE with this package. i included jre package also.otherwise ,the applicaiton will not work in client side,if java is not installed in his system.
    But the problem is after i make the setup including JRE ,the size of the file become 26mb. So,it took long time to download the application.
    What are the essential jav files we have to include while makes the setup for running the application.I included all the JRE folder itself.
    So the size become heavy. Anybody plz help me solve this
    regards
    jithesh

    Hello, I have the same problem, did you solved it?
    Thanks

  • Problem with CMP BEAN ON SUN JAVA APPLICATION SERVER 1.3.1

    I am trying to deploy an entity bean with container managed persistence on sun java app. server 1.3.1. When I go for Deploy, the error 'SQL code not generated for following beans' is comming, when verified with verifier tool, the error shown is 'transaction attributes are not specified' , What should be the problem ?

    Which appserver are you using exactly? the J2EE 1.3.1 RI, SJAS 7.0, SJSAS 7.1....etc
    You need to make sure for that you have specified the correct deployment attributes for your CMP methods. You can pull the ear/jar into deploytool and review it there

  • Problem with very simple 2 threads java application

    Hi,
    As fa as i'm concerned this is a thread licecycle:
    1. thread created: it is dead.
    2. thread started: it is now alive.
    3. thread executes its run method: it is alive during this method.
    4. after thread finishes its run method: it is dead.
    No I have simple JUnit code:
    public void testThreads() throws Exception {
    WorkTest work = new WorkTest() {
    public void release() {
    // TODO Auto-generated method stub
    public void run() {
    for (int i=0;i<1000;i++){
    log.debug(i+":11111111111");
    //System.out.println(i+":11111111111");
    WorkTest work2 = new WorkTest() {
    public void release() {
    // TODO Auto-generated method stub
    public void run() {
    for (int i=0;i<1000;i++){
    log.debug(i+":22222222222");
    Thread workerThread = new Thread(work);
    Thread workerThread2 = new Thread(work2);
    workerThread.start();
    //workerThread.join();
    workerThread2.start();
    //workerThread2.join();
    And
    public interface WorkTest extends Runnable {
    public abstract void release();
    And that's it..
    On the console I see 999 iterations of both threads only if i uncomment
    //workerThread.join();
    //workerThread2.join();
    In other case I see random number of iterations - JUNIT finishes and Thread1 made 54 and Thread2 233 iterations - the numbers are different every time i start the JUnit..
    What I want? Not to wait until the first one finishes - I want them to start at the same time and they must FINISH their tasks...
    Please help me asap :(

    This is very simple... Look at your code:
    workerThread.start();
    workerThread.join();
    workerThread2.start();
    workerThread2.join();What are you doing here? You start the first thread, then you call join(), which waits until it finishes. Then after it's finished, you start the second thread and wait until it finishes. You should simply do this:
    workerThread.start();
    workerThread2.start();
    workerThread.join();
    workerThread2.join();I.e., you start the 2 threads and then you wait (first for the first one, then for the second one).

  • Is it possible to run a java application/program forever?

    Hi,
    Is it possible to run a java program forever?
    if so please reply how it is possible
    thanks in advanced.

    There was a computer with many processes.
    Some processes came and went: they exited either voluntarily by calling exit(2),
    simply returned from their main method, or were terminated by a signal
    like SEGV, TERM, and the dreaded uncatchable KILL.
    Other processes strove to eternal running: they detatched themselves from their
    controlling terminals, ignored the HUP signal or even interpreted
    it as an occasion to reread their configuration instead of
    taking it as a notification on the line's "hanging up"
    or more generally the controlling pseudo or virtual terminal's being closed.
    Resources were acquired and released, processes came and went, some stayed though.
    Long-running processes were experienced to writing ever growing "access" and "error" logs.
    Rumors went around about "garbage collection" and "memory leakage".
    The processes started to think: "What if not all resources get released?
    What if the available memory shows an overall shrinking trend
    and in the long term it will be totally exhausted?
    What if the disk space get freed in a lesser pace as it gets consumed?
    How shall we survive in a Universe where there is no space is left
    even for sysem logs?"
    The processes made the observation that they were identifed by their process id's
    (pid of the integral type pid_t), and the only way they to create a process
    was the fork(2) system call, which returned 0 to the child,
    the positive pid of the child to the parent, and a negative value on error.
    Thus the pid could only be postive numbers.
    The processes observed the one "init" with pid=1, the smallest thinkable pid.
    They asked: "Will it run forever? If not, what will become of us?
    If a process exits without wait(2)-ing for its children,
    who will inherit it if "init" no longer exists?"
    They came to the conclusion that no process could have existed before this one,
    so their universe began once, was not infinite in the past.
    They called this event (which lead to the existance of the process "init"
    and actually their whole universe) "The Big Bang" or "The Big Boot".
    "Is it possible that different, parallel universes exist?
    Is it possible that different, parallel universes arise with different laws and system API's?"
    - they asked.
    Will their universe exist for eternity?
    If yes, in what conditions? Will it eventually run out of the resources,
    will it become cool and frozen with no memory, no available disk space,
    with no resources left drifting along with no activity?
    Or will their universe come to an end, to a condition where
    signals (TERM and then KILL) fly to all processes,
    all file system get umounted, all activity stops?
    "Big Crash" or the "Big Shutdown" they called this scenario.

  • The problem about multi-thread in java application

    i have problem with the multi-thread in java application, i don't know how to stop and restart a thread safely, because the function thread.stop(),thread.suspend() are both deprecated.
    now what i can only do is making the thread check a flag(true or false) to determine whether to start or to stop, but i think this thread will always be in the memory and maybe it will lower the performance of the system, as the program i am developping is working under realtime enviorement.
    please help me about it. thanks !

    hi,
    you can stop a thread by exiting it's run()-method which in terms can be done by checking the interrupted-flag:
    public void run(){
    while(interrupted()){ //if the thread consists of a loop
    or
    public void run(){
    if(interrupted())return;
    if(interrupted())return;
    or by the use of the return-statement anywhere withing the run-method. Afterwards, that is when the thread is no longer needed, you clear all the references to the specific thread object by setting them to null:
    Thread t;
    ... //working
    t.interrupt(); //interrupting
    while(t.isAlive()){
    Thread.yield(); //wait till thread t has stopped
    t=null;
    best regards, Michael

  • Java application compiles java files in memory

    how can I use java application program (that may call method runtime exec or some other techniques) to compile the other java files that were already loaded in memory. (I can not copy those target files to the hard disk with extension java, then compile for security reason).

    And once they are compiled, where do you plan to put the compiled .class files? Same security problem, right? Perhaps you planned to load and use them "from memory" somehow? This would be a different security problem. Perhaps you need to rethink what you are doing.

  • I could not run any java application

    I could not run any application program( even thouht the simplest one such as helloword). It would give NoClassDefoundError as:
    Exception in thread "main" java.lang.NoClassDefFoundError: Welcome1
    Press any key to continue . . .
    I use text pad editor. First I compile it, there is no error. But when I try to run ( by click run java application program button ) it gives me the same error message.
    I also always have compile error such as no class define when I try compile the program which need to import user defined class from differerent package. ( even though I did put all the classed in the same directory and also use import statement)
    I did try to run many other applet programs which do not use any user defined class, in this case every thing work fine and I could run the program.
    That mean I could run simple applet program but not application, also i always have compile error when ever I tried to import user defined class.
    I'm afriad there is some thing wrong with the sdk and java enviroment, therefore I did try to uninstall the program and redownload from the web( I try both version 1.3 and 1.4 many time, and I did check the size of the application when I finish down load) but I still got the same problem.
    My computer operating system is WinMe.
    I appreciate any help

    You probably have to set some settings for text pad. It looks like the java interpreter could not find your class, which most likely means that the classpath setting is not set properly.

  • How to launch my java application in a web browser?

    Hi. I try to launch my java application, e.g. "HellowWorld.java", using web browser.
    1. How can I change my java application program to java applet in general way?
    I read "Java: An Introduction to Computer Science and Programming, Third Edition"
    by Walter Savitch and found page 803 - "Converting a Swing application to an applet".
    My question is how I can know which class should be removed or not such as using JApplet instead of JFrame. Are there general rules about this?
    2. I found "AWT only" tutorial had some example lauching application using "AppletButton".
    How can I use this method to launch "HelloWorld.class"?
    http://java.sun.com/docs/books/tutorial/information/download.html#OLDui
    (after extracting zip file) -> ../OLDui/layout/card.html
    3. (continued 2) Can I use above method to embed applet into web browser?
    For example, java Swing application -> applet using "AppletButton" -> web browser using "<applet></applet>" tag.
    I'd appreciate any partial anwers of these questions or other suggestions.
    Thanks.

    Thanks! Here is what I got from your help.
    * HelloWorldSwing.java is a 1.4 example that
    * requires no other files.
    import javax.swing.*;  
    import java.awt.*;                //for Container class    
    //  public class HelloWorldSwing {
       public class HelloWorldSwingToApplet extends JApplet {
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
    //    private static void createAndShowGUI() {
          private void createAndShowGUI() {
            //Make sure we have nice window decorations.
    //        JFrame.setDefaultLookAndFeelDecorated(true);
            //Create and set up the window.
    //        JFrame frame = new JFrame("HelloWorldSwing");
    //        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         Container frameApplet = getContentPane();
            //Add the ubiquitous "Hello World" label.
            JLabel label = new JLabel("Hello World");
    //        frame.getContentPane().add(label);
         frameApplet.add(label);
            //Display the window.
    //        frame.pack();
    //        frame.setVisible(true);
          public void init() {
           createAndShowGUI();
    /*    public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
    }

  • Connecting to desktop Java application from R/3

    I want to connect to a java application program in my desktop and return a parameter to R/3. I have installed Java connector and I want to call the java application from R/3 using Java connector.
    I was able to get few samples to setup the RFC destination for a remote server program, but I was not able to find anything for an application in the client system. How to setup the RFC destination for a Java connector to call the application in the desktop? Can anyone please answer this question.

    Hi Allam,
    1. Why do you need to connect from backend system to your desktop client? Did I understood it correctly that it's exacly what you want to do?
    2. I guess examples you saw are for JCO or JCA which is a part of SAP J2EE server. Simply speaking the server is waiting for a client request and your desktop application probably not. If you still are sure that server->desktop connection is what you need then you have to emulate the server on your self and then depending on what you need JCO/JCA you have to create a kind of container that will implement listening service.
    I'd like to hear your answer for question one to give you more detailed answer.

Maybe you are looking for

  • Error in reciever file adapter

    Getting following error in a reciever file adapter (stack trace extracted from defaultTrace) #1#com.sap.engine.services.dbpool.exceptions.BaseSQLException: ResourceException in method ConnectionFactoryImpl.getConnection(): com.sap.engine.services.con

  • API to update ASL attribute source document in 11.5.10.1

    Hi All, We are trying to update the ASL attribute source document, programmatically. Can you please tell us, which API will be used to do so or any other alternatives? Thanks & Regards, Gowri

  • Fillable forms question - FrameMaker to PDF - Without losing copy protection options

    Hello, I'm looking for a solution that will allow us to integrate fields in a Chapter Review-style question area where users can comment their answers (instead of using StickyNotes). It must disallow printing/copying etc to meet content protection re

  • Error - session handle nor valid for ivi

    Hi All I have posted this question in existing thread also I guess.  I am trying to control TDK lambda power supply through LabVIEW using IVI drivers. logical name set in a MAX is same as what I am giving in VI to InitializeWithOptions.vi . However I

  • Jdeveloper 9.0.3.1 problems to change DB connection

    Inside the BC4JModule, at the configuration manager, it has the possibility to define the connection type. One JDBC URL and the other JDBC Datasource. At the first time I had defined my BC4J to use the JDBC URL connection an it worked. At the second