import java.awt.Point;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JPanel;

public class painttest extends JPanel {
    private int pointCount = 0;
    private Point[] points = new Point[1000];
    public painttest(){ addMouseMotionListener(new MouseMotionAdapter()
    {public void MouseDragged(MouseEvent event){
        if(pointCount<points.length)
        {points[ pointCount ] = event.getPoint(); // find point
    ++pointCount; // increment number of points in array
    repaint();
    }}});}
    public void paintComponent( Graphics g )
    {
     super.paintComponent( g ); // clears drawing area

     // draw all points in array
     for ( int i = 0; i < pointCount; i++ )
     g.fillOval( points[i].x,points[i].y , 4, 4 );
     } // end method paintComponent
     }

Hello.

This is my first class. I pretty much copied from the book word for word. I renamed some classes, such as the class method, but other than that, the names are note for note. Here is the initialization class:

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JLabel;


public class run {
    public static void main (String[] args){
        JFrame application=new JFrame("Paint");
        painttest paint= new painttest();

        application.add(paint, BorderLayout.CENTER);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setVisible(true);
        application.setSize(500,500);

        application.add(new JLabel("Profits"));


}}

Again, nothing special. I understand the basics of jframe, buttons, etc. . But I am wondering why this won't paint? That is what the book says. Perhaps the brackets or such? I had some issues with the brackets in this book, but it seems like everything is in place. Any thoughts? Here is the code from the book:

1 // Fig. 14.34: PaintPanel.java
2 // Using class MouseMotionAdapter.
3 import java.awt.Point;
4 import java.awt.Graphics;
5 import java.awt.event.MouseEvent;
6 import java.awt.event.MouseMotionAdapter;
7 import javax.swing.JPanel;
89
public class PaintPanel extends JPanel
10 {
11 private int pointCount = 0; // count number of points
12
13
14
15
16 // set up GUI and register mouse event handler
17 public PaintPanel()
18 {
19 // handle frame mouse motion event
20 addMouseMotionListener(
new MouseMotionAdapter(){
// store drag coordinates and repaint
public void mouseDragged( MouseEvent event )
{
if ( pointCount < points.length )
{
points[ pointCount ] = event.getPoint(); // find point
++pointCount; // increment number of points in array
repaint(); // repaint JFrame
} // end if
}
34 } // end anonymous inner class
35 ); // end call to addMouseMotionListener
36 } // end PaintPanel constructor
37
38 // draw ovals in a 4-by-4 bounding box at specified locations on window
39 public void paintComponent( Graphics g )
40 {
41 super.paintComponent( g ); // clears drawing area
42
43 // draw all points in array
44 for ( int i = 0; i < pointCount; i++ )
45 g.fillOval( points[ i ].x ,points[ i ].y, 4, 4 );
46 } // end method paintComponent
47 } // end class PaintPanel

That and the code snippet below are from Deitel's Java book.

1 // Fig. 14.35: Painter.java
2 // Testing PaintPanel.
3 import java.awt.BorderLayout;
4 import javax.swing.JFrame;
5 import javax.swing.JLabel;
67
public class Painter
8 {
9 public static void main( String[] args )
10 {
11 // create JFrame
12 JFrame application = new JFrame( "A simple paint program" );
13
14
15 application.add( paintPanel, BorderLayout.CENTER ); // in center
16
17 // create a label and place it in SOUTH of BorderLayout
18 application.add( new JLabel( "Drag the mouse to draw" ),
19 BorderLayout.SOUTH );
20
21 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.setSize( 400, 200 ); // set frame size
23 application.setVisible( true ); // display frame
24 } // end main
25 } // end class Painter

Recommended Answers

All 2 Replies

You changed adding the label. You add yours at the default location (CENTER), which is where the paint area was. The original code adds it at SOUTH so there is no clash.
ps: Next time please provide a lot more info to describe your problem, eg exactly what do you /don't you see and how does that differ from what you expected. That makes it much easier for people to know what they are looking for.

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.