Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 31 of 31

Thread: Another printing topic

  1. #26
    Junior Member
    Join Date
    Apr 2012
    Posts
    21
    My Mood
    Torn
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Another printing topic

    norm i think im screwing up here :/..

    i wanna start from the beginning.

    public class Printer implements Printable{
     
    public static void main(String args[]){
     
    }
    public int print(){
     
    }

    So to begin with, i will need a text area where the user will type in. But for now we can use a String variable.

    public class Printer implements Printable{
    String textPrint = "test" //assuming the next test will be on a new line
     "test2";
    public static void main(String args[]){
     
    }
    public int print(){
     
    }
    }

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Another printing topic

    What is the question?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Ajan (April 12th, 2012)

  4. #28
    Junior Member
    Join Date
    Apr 2012
    Posts
    21
    My Mood
    Torn
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Another printing topic

    from my last post. how would i start the print method?
    keeping in my mind:
    Must print multiple lines
    Must print multiple pages
    Must print within its borders.

    So can you guide me through this problem please?

  5. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Another printing topic

    Create a special test program to work out the logic. Create a class that extends JPanel, override its paintComponent() method and add an instance of it to a JFrame so you can see the results.
    In the paintComponent method, add code that is almost exactly the same as the code in your drawString() method. Have the code take a String with \n characters, split it and draw the substrings that are in the array returned by split using the Graphics drawstring() method in a loop where the value of the y location is increased each time around the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #30
    Junior Member
    Join Date
    Apr 2012
    Posts
    21
    My Mood
    Torn
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Another printing topic

    could you go step by step with me?

    ok so, made the GUI:

    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    /**
     *
     * @author User
     */
    public class Frame extends javax.swing.JFrame {
    String textPrint;
        /** Creates new form Frame */
        public Frame() {
            initComponents();
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            jScrollPane1 = new javax.swing.JScrollPane();
            text = new javax.swing.JTextArea();
            Print = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            text.setColumns(20);
            text.setRows(5);
            jScrollPane1.setViewportView(text);
     
            Print.setText("Print");
            Print.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    PrintActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addGap(267, 267, 267)
                    .addComponent(Print)
                    .addContainerGap(296, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(54, 54, 54)
                    .addComponent(Print)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>
     
        private void PrintActionPerformed(java.awt.event.ActionEvent evt) {
             textPrint = text.getText();
             JFrame frame = new JFrame();
             JPanel drawArea = new JPanel();
             frame.add(drawArea);
             frame.setSize(500, 600);
             frame.setVisible(true);
     
     
     
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new Frame().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton Print;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea text;
        // End of variables declaration
    }

    So where to from here?
    I havent used the paintComponent before.

  7. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Another printing topic

    What is all that code for?
    Reread my suggestion in post#29. There are two classes: JFrame with a main() method and JPanel with a paintComponent() method. No more.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Best way to teach/learn code/java. (topic for fun)
    By JonLane in forum Java Theory & Questions
    Replies: 2
    Last Post: February 23rd, 2012, 08:54 PM
  2. Need Beginners resource for Filter And Listener Topic
    By ashishrajiit in forum Java Servlet
    Replies: 1
    Last Post: October 20th, 2011, 06:18 AM
  3. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  4. Off Topic: Happy Songs?
    By KevinWorkman in forum The Cafe
    Replies: 5
    Last Post: March 3rd, 2011, 09:46 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM