Painting graphics on JPanel

hi all,
below is my code,
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics2D;
import java.awt.Graphics.*;
public class Testing extends JFrame implements ActionListener
String x[] = { " ", "Material A", "Material B", "Material C"};
String y[] = { "                       ", "+5", "+4", "+3", "+2", "+1", " 0",
     "-1", "-2", "-3", "-2", "-5" };
private JButton Plot, End;
private Container c;
private JPanel centerPanel, northPanel, southPanel;
private JComboBox designMaterialx, materialRatingy;
String desMaterial = "";
public Testing()
super( "Welcome" );
     Container c = getContentPane();
     centerPanel = new JPanel()
          protected void paintComponent(Graphics g)
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D)g;
          g.setColor(Color.blue);
g.drawRect(120, 120, 20, 200);
g.drawString ("Material", 30, 220);
g.drawString ("Ratings", 30, 240);
g.drawString ("+5", 102, 130);
g.drawString ("0", 107, 220);
g.drawString ("-5", 102, 315);
          g.setColor(Color.red);
          g.drawRect(120, 321, 20, 200);
          g.drawString ("Design", 30, 410);
          g.drawString ("Materials", 30, 430);
          g2d.rotate(-Math.PI/2.0,250.0,250.0);
          g.setColor(Color.black);
          g.drawString(desMaterial, -15, 135);
          centerPanel.setBackground(Color.white);
     c.add( centerPanel, BorderLayout.CENTER);
     setVisible(true);
          northPanel = new JPanel();
          northPanel.add(new Label( "Please choose accordingly:" ));
     northPanel.setLayout (new FlowLayout(FlowLayout.CENTER, 20, 0));
designMaterialx = new JComboBox( x );
designMaterialx.setMaximumRowCount( 2 );
designMaterialx.setBorder(BorderFactory.createTitledBorder("Design Materials"));
northPanel.add( designMaterialx );
designMaterialx.setEditable(true);
materialRatingy = new JComboBox( y );
materialRatingy.setMaximumRowCount( 2 );
materialRatingy.setBorder(BorderFactory.createTitledBorder("Material Ratings"));
northPanel.add( materialRatingy );
c.add( northPanel, BorderLayout.NORTH );
setVisible( true );
southPanel = new JPanel();
Plot = new JButton( "Plot" );
End = new JButton( "End" );
southPanel.add( Plot );
southPanel.add( End );
c.add( southPanel, BorderLayout.SOUTH );
setVisible( true );
Plot.addActionListener(this);
End.addActionListener(this);
Plot.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
desMaterial = designMaterialx.getSelectedItem().toString();
centerPanel.repaint();
System.out.println(desMaterial);
pack();
public static void main( String args[] )
Testing app = new Testing();
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
public void actionPerformed(ActionEvent e)
     if(e.getSource()==Plot)
          System.out.println("Plot");
     else if(e.getSource()==End)
          System.exit(0);
at the moment when the plot jButton is pressed, the selected values of the design materials jComboBox will be painted on the centerPanel inside the red rectangle. upon subsequent selection of the design materials & pressing of the plot jbutton, i wish to add them next to the existing ones but i'm not sure how to do this. could anyone out there help me on this? any kind of help would be appreciated.
thanks in advance.
rolf

Here's a bunch of ideasimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class Test3 extends JFrame {
  MyGraph mg = new MyGraph();
  JTextField nameField = new JTextField("Sample2");
  JTextField valueField = new JTextField("100");
  public Test3() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = getContentPane();
    mg.addData(new MyData("Sample",50));
    content.add(mg, BorderLayout.CENTER);
    JPanel dataPanel = new JPanel();
    dataPanel.add(new JLabel("Name:"));
    dataPanel.add(nameField);
    dataPanel.add(new JLabel("Value:"));
    dataPanel.add(valueField);
    JButton jb = new JButton("Add");
    dataPanel.add(jb);
    jb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        mg.addData(new MyData(nameField.getText(),Integer.parseInt(valueField.getText())));
        mg.repaint();
    content.add(dataPanel, BorderLayout.NORTH);
    setSize(300,300);
  public static void main(String[] args) { new Test3().setVisible(true); }
class MyGraph extends JPanel {
  ArrayList al = new ArrayList();
  public void addData(MyData data) { al.add(data); }
  public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    super.paintComponent(g2d);
    g2d.setColor(Color.red);
    int height = getHeight(), width=getWidth();
    for (int i=0; i<al.size(); i++) {
      MyData md = (MyData)al.get(i);
      g2d.drawRect(i*20,height-md.getNum(),17,md.getNum());
    g2d.rotate(-Math.PI/2.0,width/2,height/2);
    g2d.setColor(Color.blue);
    for (int i=0; i<al.size(); i++) {
      MyData md = (MyData)al.get(i);
      g2d.drawString(md.getName(),30,i*20-15);
class MyData {
  String name;
  int num;
  public MyData(String name, int num) {
    this.name=name;
    this.num=num;
  public String getName() { return name; }
  public int getNum() { return num; }
}

Similar Messages

  • Scrolling a custom Component (e.g. JPanel) with overridden paint(Graphic g)

    Hi.
    I&#8217;m creating an application for modelling advanced electrical systems in a house. Until now I have focused on the custom canvas using Java2D to draw my model. I can move the model components around, draw lines between them, and so on.
    But now I want to implement a JScrollPane to be able to scroll a large model. I&#8217;m currently using a custom JPanel with a complete override of the paint(Graphic g) method.
    Screen-shot of what I want to scroll:
    http://pchome.grm.hia.no/~aalbre99/ScreenShot.png
    Just adding my custom JPanel to a JScrollPane will obviously not work, since the paint(Graphic g) method for the JPanel would not be used any more, since the JScrollPane now has to analyze which components inside the container (JPanel) to paint.
    So my question is therefore: How do you scroll a custom Component (e.g. JPanel) where the paint(Graphic g) method is totally overridden.
    I believe the I have to paint on a JViewport instructing the JScrollPane my self, but how? Or is there another solution to the problem?
    Thanks in advance for any suggestions.
    Aleksander.

    I�m currently using a custom JPanel with a complete override of the paint(Graphic g) method. Althought this isn't your problem, you should be overriding the paintComponent(..) method, not the paint(..) method.
    But now I want to implement a JScrollPane to be able to scroll a large model.When you create a custom component to do custom painting then you are responsible for determining the preferredSize of the component. So, you need to override the getPreferredSize(...) method to return the preferredSize of your component. Then scrolling will happen automatically when the component is added to a scrollPane.

  • Painting Graphics from an Array

    I have a GUI that displays the Towers of Hanoi solution in a JPanel. Everything actually works except for a slight repainting issue.
    Currently I am overriding the paint() function, I know that for my purpose using paintComponent() would probably be the better route, however that's not my issue...
    The images (disks) in my JPanel are not redrawing properly, and I have no idea why...
    Basically:
    - I call setup() to set the number of disks and the co ordinates they should be drawn. (store them in an array)
    - I call moveDisk() to change the co ordinates of each disk as they are being moved.
    - then drawDisk() is called to go through the array of disks and print them.
    currently paint() calls drawDisk() in order to display the current position of all the disks.
    It seems to work fine for the first 2 moves, then disks start disappearing until there is one (the last disk) left...BUT if I print the co ordinates of the disks they seem to be correct, just not drawing.
    could someone take a look at my code and see what is wrong?
    below is the meat of the code, minus the GUI:
        public class HanoiFrame extends javax.swing.JFrame {
        private static final int STATE_SETUP = 0;
        private static final int STATE_ANIMATE = 1;
        private static final int POLE_AREA_HEIGHT = 356;
        private static final int POLE_AREA_WIDTH = 205;
        private static final int DISK_ARC_HEIGHT = 15;
        private static final int DISK_ARC_WIDTH = 15;
        private static final int DISK_HEIGHT = 23;
        private static final int MAX_DISKS = 10;
        private static final int MAX_ANIMATION_DELAY = 30000;
        private static final int MAX_PHYSICAL_DELAY = 100000;
        private static final int MIN_DISKS = 1;
        private static final int MIN_ANIMATION_DELAY = 1;
        private static final int MIN_PHYSICAL_DELAY = 1;
        private static final int EMPTY_CELL_VALUE = -1;
        private static final int CELL_HEIGHT = 25;
        private static final int COLUMNS = 3;
        private static final int ROW = 11;
        private  static final int MILLISECONDS_PER_SECOND = 1000;
        private final static String newline = "\n";
        private static Disk[] disks;
        private static Disk newDisk;
        private static int[][] diskCells;
        public static  int startX = 2;
        public static  int startY = 340;
        public static int setDiskAmount = 0;
        public static int setAnimationDelay = 0;
        public static int  setPhysicalDelay = 0;
        public static int  moveNumber = 0;
        public static int intMovesLeft = 0;
        private boolean  buttonPressed = false;
        private double movesLeft = 0;
        private int windowState = 0;
        /** Creates new form HanoiFrame */
        public HanoiFrame() {
            initComponents();
            /* initialize the "poles" to hold a certain number of "cells */
            this.diskCells = new int[ROW][COLUMNS];
            for (int row = ROW - 1;row >= 0; row--) {
                for (int col = 0;col < COLUMNS ; col++) {
                    diskCells[row][col] = EMPTY_CELL_VALUE;
        private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            /* reset all the variable to original state */
            setDiskAmount = 0;
            setAnimationDelay = 0;
            setPhysicalDelay = 0;
            moveNumber = 0;
            movesLeft = 0;
            intMovesLeft = 0;
            buttonPressed = false;
            windowState = 0;
            this.startButton.setEnabled(true);
            this.diskAmount.setEditable(true);
            this.animationDelay.setEditable(true);
            this.physicalDelay.setEditable(true);
            /* reset all the cell vales to empty (this could be its own function) */
            for (int row = ROW - 1;row >= 0; row--) {
                for (int col = 0;col < COLUMNS ; col++) {
                    diskCells[row][col] = EMPTY_CELL_VALUE;
            /* reset the content of the TextFields and Area */
            this.diskAmount.setText("");
            this.animationDelay.setText("");
            this.physicalDelay.setText("");
            this.outTextArea.setText("");
            /* repaint the display panel */
            this.hanoiPanel.repaint();
        /* i have no idea why this is here...It was generated by Netbeans when I
         *made the reset button.  As you can see it made 2.  The one above it the same
         *except it contains code.  Since it was automatically generated
         *I cannot delete it.
        private void resetButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
        /* is executed when the start button is pressed.
         *also executes a field check and intializes key variables. Finally,
         *it executes the solution and generates the solve time
        private void startButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
            /* check if the program has already been run (without being reset) */
            if (!buttonPressed){
                /* check the fields to ensure the input is correct and useable */
                if (checkFields()){
                    /* give button a pressed status if all info valid */
                    buttonPressed = true;
                    windowState = 1;
                    /* disable the button */
                    this.startButton.setEnabled(false);
                    /* disable the fields */
                    this.diskAmount.setEditable(false);
                    this.animationDelay.setEditable(false);
                    this.physicalDelay.setEditable(false);
                    /* setup the disks on the starting pole */
                    setup();
                    /* initialize the number of moves required. 2 to the power of n minus 1 */
                    movesLeft = Math.pow(2, setDiskAmount) - 1;
                    /* convert the number to an integer */
                    intMovesLeft = (int)movesLeft;
                    /* set the physical delay */
                    setPhysicalDelay = Integer.parseInt(this.physicalDelay.getText());
                    /* create and start a new thread.  This is EXTREMELY important
                     *as it allows for GUI to be repainted while the soulution
                     *is animated */
                    SolveEngine s = new SolveEngine();
                    s.start();               
         * @param args the command line arguments
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new HanoiFrame().setVisible(true);
        /*returns the Animation panel area graphics
         *@return Animation area graphics
        public static Graphics getAnimationArea(){
            /* get the usable area of poleA */
            Container clientArea = hanoiPanel;
            //Container clientArea = this.getContentPane();
            /* get the graphics to the display panel*/
            Graphics gfx = clientArea.getGraphics();
            return gfx;
         *set up the requested amount of disks on the first pole
         *@param diskAmount the amount of disks entered by the user
        public void setup(){
            int numberOfDisks = setDiskAmount;
            this.disks = new Disk[numberOfDisks];
            int diskNumber = 0;
            int diskWidth = POLE_AREA_WIDTH - 4;
            int arcH = DISK_ARC_HEIGHT;
            int arcW = DISK_ARC_WIDTH;
            int x = startX;
            int y = startY;
            int row = 0;
            int col = 0;
            /* get the graphics to the display panel*/
            //Graphics gfx = getAnimationArea();
            /* set the color of the disks */
            //gfx.setColor(Color.MAGENTA);
            /* draw the specified number of disks */
            while (diskNumber < numberOfDisks){
                /* reduce the y position */
                y = startY - diskNumber * CELL_HEIGHT;
                /* draw the disk */
                //gfx.fillRoundRect(x, y, diskWidth, DISK_HEIGHT, arcH, arcW);
                /* create a new instance of disk */
                newDisk = new Disk(x, y, diskWidth);
                /* give the new disk an id */
                this.disks[diskNumber] = newDisk;
                /* add the id to the cell array of poleA */
                this.diskCells[row][col] = diskNumber;
                /* make the disk smaller and center */
                x = x + 8;
                diskWidth = diskWidth - 16;
                /* increase disk number */
                diskNumber++;
                /* move to the next row */
                row++;
            repaint();
         *move the disk from a source pole to the destination pole.
         *this should take the source col and destination col to determine where to draw the
         *disk.  It will also need to know to draw the disk in the first available cell
         *in a col
         *@param source the starting pole of the next move
         *@param destination the end pole of the next move
        public void moveDisk(int src, int dest){
            /* the lines below would not be necessary if I were to pass the Graphics
             *gfx object as a parameter.  I may have to use this option in order
             *to work around the current repainting issue */
            /* get the graphics to the display panel*/
            Graphics gfx = getAnimationArea();
            /* get the id of the disk to be moved */
            int disk = getDiskId(src);
            /* get the to and from rows */
            int sourceRow = getRow(src);
            int destinationRow = getRow(dest);
            /* set the co ordintates of the destination */
            int col = dest * POLE_AREA_WIDTH;
            int x = disks[disk].getStartX() + col;
            int y = startY - (destinationRow * CELL_HEIGHT);
            int width = disks[disk].getDiskWidth();
            disks[disk].setStartX(x);
            disks[disk].setStartY(y);
            disks[disk].setDiskWidth(width);
            //System.out.println("startX " + x);
            //System.out.println("startY " + y);
            //System.out.println("destination row " + destinationRow);
            //System.out.println("disk " + disk);
            //System.out.println("Width " + width);
            diskCells[sourceRow - 1][src] = EMPTY_CELL_VALUE;
            /* set the destination row to the disk id */
            diskCells[destinationRow][dest] = disk;
            drawDisk();
            repaint();
        public void drawDisk(){
            int diskNum = setDiskAmount -1;
            int i = 0;
            Graphics gfx = getAnimationArea();
            gfx.setColor(Color.MAGENTA);
            while(diskNum >= 0){
                //System.out.println("here is the disk IDs " + diskNum);
                //System.out.println("the startY during draw " + disks[diskNum].getStartY());
                gfx.fillRoundRect(disks[diskNum].getStartX(), disks[diskNum].getStartY(), disks[diskNum].getDiskWidth(), DISK_HEIGHT, DISK_ARC_WIDTH, DISK_ARC_HEIGHT);
                diskNum--;
        public void paint(Graphics gfx){
            if(windowState == 0){
                super.paint(gfx);
                setup();
            } else if (windowState == 1){
                super.paint(gfx);
                drawDisk();
         *returns the id of the disk
         *@param col the the designated column
         *@return the disk identification number
        public static int getDiskId(int col){
            int diskIdent = 0;
            /* initialize the row number to max -1, because the array start at 0 */
            int row = MAX_DISKS - 1;
            /* do a cell check while row is greater than 0
             *this is done so that is starts at the top row
             *and moves down
            while (row > EMPTY_CELL_VALUE){
                /* when the first cell that is not -1 is reached get the disk number
                 *that disk will be the smallest in the stack */
                if(diskCells[row][col] != -1){
                    //diskIdent = this.diskCells[row][col];
                    diskIdent =diskCells[row][col];
                    break;
                row--;
            return diskIdent;
         *returns the first available row
         *@param col the designated col
         *@return the first available row number
        public static int getRow(int col){
            int rowNumber = 0;
            /* cycle through the cells until it finds an empty one */
            while (diskCells[rowNumber][col] != -1){
                rowNumber++;
            return rowNumber;
         *calculate the total time it takes for the tower of hanoi game to be
         *solved, given the amount of disk and the amount of time it takes
         *to physically move a disk from one pole to the other.
        public static void totalTime(){
            int timeInSeconds = setPhysicalDelay * intMovesLeft;
            int i = 0;
            int seconds = 0;
            int minute = 0;
            int hour = 0;
            int day = 0;
            int month = 0;
            int year = 0;
            while (i < timeInSeconds){
                if(seconds > 60){
                    minute++;
                    seconds = 0;
                if(minute > 60){
                    hour++;
                    minute = 0;
                if(hour > 24){
                    day++;
                    hour = 0;
                if(day > 30){
                    month++;
                    day = 0;
                if(month > 12){
                    year++;
                    month = 0;
                seconds++;
                i++;
            updateMessageCenter("It would take:" + year + " years " + month + " months " + day +
                    " days " + hour + " hours " + minute + " minutes and " + seconds + " seconds, to move " + setDiskAmount +
                    " disks at " + setPhysicalDelay + " seconds per move");
       

    Sorry if my post was a little too cryptic...
    "How do you know? You have a problem you can't solve. Anything could be your issue." Agreed. However, using the process of elimination and debug statments I have been able to narrow the scope of the issue.
    "If you can't solve it by executing the code and adding debug statements how to you expect us to help."If I could solve but "executing code and adding debug statements" there would be no need to post the problem here.
    "We don't know exactly what you are attempting to do."Trying to paint my graphics to a JPanel based on a set of co ordinates.
    "We don't know why you are passing the Graphics object around."Why not? is it impossible to do it this way?
    "The big problem is in the alogorithm you use to determine the location of each component."Who said you couldn't read minds?
    "If you are using some kind of animation then I would use a Timer to schedule the moving of components. I don't even see how you are doing this in your code."Sorry I guess stating that I didn't post my entire code may have threw things off...I do use a timer in a seperate class. I will include it in the next post.
    "I can't tell if you are using Threads of not."guess you overlooked these lines of code:
                    /* create and start a new thread.  This is EXTREMELY important
                     *as it allows for GUI to be repainted while the soulution
                     *is animated */
                    SolveEngine s = new SolveEngine();
                    s.start();   Here is the code once again:
    public class HanoiFrame extends javax.swing.JFrame {
        private static final int STATE_SETUP = 0;
        private static final int STATE_ANIMATE = 1;
        private static final int POLE_AREA_HEIGHT = 356;
        private static final int POLE_AREA_WIDTH = 205;
        private static final int DISK_ARC_HEIGHT = 15;
        private static final int DISK_ARC_WIDTH = 15;
        private static final int DISK_HEIGHT = 23;
        private static final int MAX_DISKS = 10;
        private static final int MAX_ANIMATION_DELAY = 30000;
        private static final int MAX_PHYSICAL_DELAY = 100000;
        private static final int MIN_DISKS = 1;
        private static final int MIN_ANIMATION_DELAY = 1;
        private static final int MIN_PHYSICAL_DELAY = 1;
        private static final int EMPTY_CELL_VALUE = -1;
        private static final int CELL_HEIGHT = 25;
        private static final int COLUMNS = 3;
        private static final int ROW = 11;
        private  static final int MILLISECONDS_PER_SECOND = 1000;
        private final static String newline = "\n";
        private static Disk[] disks;
        private static Disk newDisk;
        private static int[][] diskCells;
        public static  int startX = 2;
        public static  int startY = 340;
        public static int setDiskAmount = 0;
        public static int setAnimationDelay = 0;
        public static int  setPhysicalDelay = 0;
        public static int  moveNumber = 0;
        public static int intMovesLeft = 0;
        private boolean  buttonPressed = false;
        private double movesLeft = 0;
        private int windowState = 0;
        /** Creates new form HanoiFrame */
        public HanoiFrame() {
            initComponents();
            /* initialize the "poles" to hold a certain number of "cells */
            this.diskCells = new int[ROW][COLUMNS];
            for (int row = ROW - 1;row >= 0; row--) {
                for (int col = 0;col < COLUMNS ; col++) {
                    diskCells[row][col] = EMPTY_CELL_VALUE;
        private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            /* reset all the variable to original state */
            setDiskAmount = 0;
            setAnimationDelay = 0;
            setPhysicalDelay = 0;
            moveNumber = 0;
            movesLeft = 0;
            intMovesLeft = 0;
            buttonPressed = false;
            windowState = 0;
            this.startButton.setEnabled(true);
            this.diskAmount.setEditable(true);
            this.animationDelay.setEditable(true);
            this.physicalDelay.setEditable(true);
            /* reset all the cell vales to empty (this could be its own function) */
            for (int row = ROW - 1;row >= 0; row--) {
                for (int col = 0;col < COLUMNS ; col++) {
                    diskCells[row][col] = EMPTY_CELL_VALUE;
            /* reset the content of the TextFields and Area */
            this.diskAmount.setText("");
            this.animationDelay.setText("");
            this.physicalDelay.setText("");
            this.outTextArea.setText("");
            /* repaint the display panel */
            this.hanoiPanel.repaint();
        /* i have no idea why this is here...It was generated by Netbeans when I
         *made the reset button.  As you can see it made 2.  The one above it the same
         *except it contains code.  Since it was automatically generated
         *I cannot delete it.
        private void resetButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
        /* is executed when the start button is pressed.
         *also executes a field check and intializes key variables. Finally,
         *it executes the solution and generates the solve time
        private void startButtonMouseReleased(java.awt.event.MouseEvent evt) {                                         
            /* check if the program has already been run (without being reset) */
            if (!buttonPressed){
                /* check the fields to ensure the input is correct and useable */
                if (checkFields()){
                    /* give button a pressed status if all info valid */
                    buttonPressed = true;
                    windowState = 1;
                    /* disable the button */
                    this.startButton.setEnabled(false);
                    /* disable the fields */
                    this.diskAmount.setEditable(false);
                    this.animationDelay.setEditable(false);
                    this.physicalDelay.setEditable(false);
                    /* setup the disks on the starting pole */
                    setup();
                    /* initialize the number of moves required. 2 to the power of n minus 1 */
                    movesLeft = Math.pow(2, setDiskAmount) - 1;
                    /* convert the number to an integer */
                    intMovesLeft = (int)movesLeft;
                    /* set the physical delay */
                    setPhysicalDelay = Integer.parseInt(this.physicalDelay.getText());
                    /* create and start a new thread.  This is EXTREMELY important
                     *as it allows for GUI to be repainted while the soulution
                     *is animated */
                    SolveEngine s = new SolveEngine();
                    s.start();
         *Check all the fields at once to ensure that they are valid
        private boolean checkFields(){
            String numberOfDisks = null;
            String animationDelay = null;
            String physicalDelay = null;
            numberOfDisks = this.diskAmount.getText();
            animationDelay = this.animationDelay.getText();
            physicalDelay = this.physicalDelay.getText();
            /* initiate my array of error messages */
            ArrayList errMsg = new ArrayList(0);
            /* check if the number of disks was left blank */
            if (numberOfDisks.equals("")){
                errMsg.add("Please enter the Number of Disks (MAX " + MAX_DISKS + ")");
            }else{
                /* check if the input given is valid */
                try {
                    /* parse the disk amount entered into an integer */
                    setDiskAmount = Integer.parseInt(numberOfDisks);
                    /* check the # of disks entered is greater than 0 and less than 10 */
                    if (setDiskAmount < MIN_DISKS){
                        errMsg.add("Number of Disks must be greater than " + MIN_DISKS);
                    } else if (setDiskAmount > MAX_DISKS){
                        errMsg.add("Number of Disks must be less than" + MAX_DISKS);
                } catch (NumberFormatException ex) {
                    errMsg.add("Number of Disks must be a Number");
            /* check if animation delay was left blank */
            if (animationDelay.equals("")){
                errMsg.add("Please enter disk Animation Delay");
            } else {
                /* check if the input given is valid */
                try {
                    /* parse the animation delay entered into an integer */
                    setAnimationDelay = Integer.parseInt(animationDelay);
                    /* check range of animation delay */
                    if (setAnimationDelay < MIN_ANIMATION_DELAY){
                        errMsg.add("Animation Delay must be greater than 0");
                    } else if (setAnimationDelay > MAX_ANIMATION_DELAY){
                        errMsg.add("Animation Delay must be less than " + MAX_ANIMATION_DELAY);
                } catch (NumberFormatException ex) {
                    errMsg.add("Animation Delay must be a Number");
            /* check if physical delay was left blank */
            if (physicalDelay.equals("")){
                errMsg.add("Please enter the Physical disk delay");
            } else {
                /* check if the input given is valid */
                try {
                    /* parse the physical delay entered into an integer */
                    setPhysicalDelay = Integer.parseInt(physicalDelay);
                    /* check check the range of the physical delay */
                    if (setPhysicalDelay < MIN_PHYSICAL_DELAY){
                        errMsg.add("Physical Delay must be greater than 0");
                    } else if (setPhysicalDelay > MAX_PHYSICAL_DELAY){
                        errMsg.add("Physical Delay must be less than " + MAX_PHYSICAL_DELAY);
                } catch (NumberFormatException ex) {
                    errMsg.add("Physical Delay must be a Number");
            /* if there is any problem, add the message to the popup and display */
            if (!errMsg.isEmpty()){
                JOptionPane.showMessageDialog(this, errMsg.toArray());
                return false;
            return true;
         * @param args the command line arguments
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new HanoiFrame().setVisible(true);
        /*returns the Animation panel area graphics
         *@return Animation area graphics
        public static Graphics getAnimationArea(){
            /* get the usable area of poleA */
            Container clientArea = hanoiPanel;
            //Container clientArea = this.getContentPane();
            /* get the graphics to the display panel*/
            Graphics gfx = clientArea.getGraphics();
            return gfx;
         *set up the requested amount of disks on the first pole
         *@param diskAmount the amount of disks entered by the user
        public void setup(){
            int numberOfDisks = setDiskAmount;
            this.disks = new Disk[numberOfDisks];
            int diskNumber = 0;
            int diskWidth = POLE_AREA_WIDTH - 4;
            int arcH = DISK_ARC_HEIGHT;
            int arcW = DISK_ARC_WIDTH;
            int x = startX;
            int y = startY;
            int row = 0;
            int col = 0;
            /* get the graphics to the display panel*/
            //Graphics gfx = getAnimationArea();
            /* set the color of the disks */
            //gfx.setColor(Color.MAGENTA);
            /* draw the specified number of disks */
            while (diskNumber < numberOfDisks){
                /* reduce the y position */
                y = startY - diskNumber * CELL_HEIGHT;
                /* draw the disk */
                //gfx.fillRoundRect(x, y, diskWidth, DISK_HEIGHT, arcH, arcW);
                /* create a new instance of disk */
                newDisk = new Disk(x, y, diskWidth);
                /* give the new disk an id */
                this.disks[diskNumber] = newDisk;
                /* add the id to the cell array of poleA */
                this.diskCells[row][col] = diskNumber;
                /* make the disk smaller and center */
                x = x + 8;
                diskWidth = diskWidth - 16;
                /* increase disk number */
                diskNumber++;
                /* move to the next row */
                row++;
            repaint();
         *move the disk from a source pole to the destination pole.
         *this should take the source col and destination col to determine where to draw the
         *disk.  It will also need to know to draw the disk in the first available cell
         *in a col
         *@param source the starting pole of the next move
         *@param destination the end pole of the next move
        public void moveDisk(int src, int dest){
            /* the lines below would not be necessary if I were to pass the Graphics
             *gfx object as a parameter.  I may have to use this option in order
             *to work around the current repainting issue */
            /* get the graphics to the display panel*/
            Graphics gfx = getAnimationArea();
            /* get the id of the disk to be moved */
            int disk = getDiskId(src);
            /* get the to and from rows */
            int sourceRow = getRow(src);
            int destinationRow = getRow(dest);
            /* set the co ordintates of the destination */
            int col = dest * POLE_AREA_WIDTH;
            int x = disks[disk].getStartX() + col;
            int y = startY - (destinationRow * CELL_HEIGHT);
            int width = disks[disk].getDiskWidth();
            disks[disk].setStartX(x);
            disks[disk].setStartY(y);
            disks[disk].setDiskWidth(width);
            //System.out.println("startX " + x);
            //System.out.println("startY " + y);
            //System.out.println("destination row " + destinationRow);
            //System.out.println("disk " + disk);
            //System.out.println("Width " + width);
            diskCells[sourceRow - 1][src] = EMPTY_CELL_VALUE;
            /* set the destination row to the disk id */
            diskCells[destinationRow][dest] = disk;
            drawDisk();
            repaint();
            String output = "";
            //must initialize row to max - 1 and increment 'til = 0
            //since the array position starts at 0
            //this will print the array from the top down
            for (int row = ROW - 1;row >= 0; row--) {
                for (int colm = 0;colm < COLUMNS ; colm++) {
                    output = output + diskCells[row][colm];
                output = output + "\n";
            System.out.println(output);
            System.out.println(newline);
        public void drawDisk(){
            int diskNum = setDiskAmount -1;
            int i = 0;
            Graphics gfx = getAnimationArea();
            gfx.setColor(Color.MAGENTA);
            while(diskNum >= 0){
                //System.out.println("here is the disk IDs " + diskNum);
                //System.out.println("the startY during draw " + disks[diskNum].getStartY());
                gfx.fillRoundRect(disks[diskNum].getStartX(), disks[diskNum].getStartY(), disks[diskNum].getDiskWidth(), DISK_HEIGHT, DISK_ARC_WIDTH, DISK_ARC_HEIGHT);
                diskNum--;
        public void paint(Graphics gfx){
            if(windowState == 0){
                super.paint(gfx);
                setup();
            } else if (windowState == 1){
                super.paint(gfx);
                drawDisk();
         *returns the id of the disk
         *@param col the the designated column
         *@return the disk identification number
        public static int getDiskId(int col){
            int diskIdent = 0;
            /* initialize the row number to max -1, because the array start at 0 */
            int row = MAX_DISKS - 1;
            /* do a cell check while row is greater than 0
             *this is done so that is starts at the top row
             *and moves down
            while (row > EMPTY_CELL_VALUE){
                /* when the first cell that is not -1 is reached get the disk number
                 *that disk will be the smallest in the stack */
                if(diskCells[row][col] != -1){
                    //diskIdent = this.diskCells[row][col];
                    diskIdent =diskCells[row][col];
                    break;
                row--;
            return diskIdent;
         *returns the first available row
         *@param col the designated col
         *@return the first available row number
        public static int getRow(int col){
            int rowNumber = 0;
            /* cycle through the cells until it finds an empty one */
            while (diskCells[rowNumber][col] != -1){
                rowNumber++;
            return rowNumber;
         *calculate the total time it takes for the tower of hanoi game to be
         *solved, given the amount of disk and the amount of time it takes
         *to physically move a disk from one pole to the other.
        public static void totalTime(){
            int timeInSeconds = setPhysicalDelay * intMovesLeft;
            int i = 0;
            int seconds = 0;
            int minute = 0;
            int hour = 0;
            int day = 0;
            int month = 0;
            int year = 0;
            while (i < timeInSeconds){
                if(seconds > 60){
                    minute++;
                    seconds = 0;
                if(minute > 60){
                    hour++;
                    minute = 0;
                if(hour > 24){
                    day++;
                    hour = 0;
                if(day > 30){
                    month++;
                    day = 0;
                if(month > 12){
                    year++;
                    month = 0;
                seconds++;
                i++;
            updateMessageCenter("It would take:" + year + " years " + month + " months " + day +
                    " days " + hour + " hours " + minute + " minutes and " + seconds + " seconds, to move " + setDiskAmount +
                    " disks at " + setPhysicalDelay + " seconds per move");
         *add the message to the diplay window
         *currently this is not necessary, but may be required to
         *dynamically display the contents of message into the text area
         *@param message the message to be added/appended the the text area
        public static void updateMessageCenter(String message){
            /* append text to the message */
            outTextArea.append(message + newline);
            /* ensure the the most recent line is viewable - auto scroll */
            outTextArea.setCaretPosition(outTextArea.getDocument().getLength());
        public class SolveEngine extends Thread {
            /** Creates a new instance of SolveEngine */
            public SolveEngine() {
            public void run(){
                /* start the engine/animation */
                solutionEngine(HanoiFrame.setDiskAmount,0, 2, 1);
                /* calculate and print the total time after the solution is complete */
                HanoiFrame.totalTime();
         *the recursive solution to the Towers of Hanoi problem
         *@param diskAmount the number of disks that need to be moved
         *@param start the identity of the starting column
         *@param end the identity of the ending column
         *@param temp the identity of the temporary column
            public void solutionEngine(int diskAmount,int start, int end, int temp){
                int disks = diskAmount;
                int startPole = start;
                int endPole = end;
                int tempPole = temp;
                try {
                    if ( disks == 1 ) {
                        HanoiFrame.moveNumber++;
                        HanoiFrame.updateMessageCenter("Moving disk from  "+ startPole + " to "+ endPole + " Move #: " + HanoiFrame.moveNumber + " of " + HanoiFrame.intMovesLeft);
                        /* delay the movement of the disk */
                        Thread.sleep(HanoiFrame.setAnimationDelay);
                        /* move the disk */
                        moveDisk(startPole, endPole);
                    } else {
                        solutionEngine( disks - 1, startPole, tempPole, endPole);
                        HanoiFrame.moveNumber++;
                        HanoiFrame.updateMessageCenter("Moving disk from  "+ startPole + " to "+ endPole + " Move #: " + HanoiFrame.moveNumber + " of " + HanoiFrame.intMovesLeft);
                        /* delay the movement of the disk */
                        Thread.sleep(HanoiFrame.setAnimationDelay);
                        moveDisk(startPole, endPole);
                        solutionEngine( disks - 1, tempPole, endPole, startPole);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
        }

  • Using paint() for a JPanel in a ScrollBar

    i have a class that extends JPanel and paints some graphics in it using the function paint(). like g.drawString.. i had to add an instantition of that class in the container of my main class within a scrollbar. It initially painted well but when i scroll, it gets distorted. its something like this
    public class PaintedPane extends JPanel
    public PaintedPane()
    paint()
    other codes here..
    public class MainClass extends JFrame
    public PaintedPane pane;
    public JScrollbar scroll;
    public MainClass()
    pane = new PaintedPane();
    scroll = new JScrollBar(pane);
    it seems that the pane wont update the part of the pane not initially exposed because of the scrollBar. how can i fix it? thanks!

    sorry for the wrong section.
    here is the code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class MainClass extends JFrame
         public TextDraw td;
         public JScrollPane pane;
         public Container c;
         public MainClass()
              c = getContentPane();
              c.setLayout(new FlowLayout());
              td = new TextDraw();
              td.setPreferredSize(new Dimension(300, 300));
              pane = new JScrollPane(td);
              pane.setPreferredSize(new Dimension(300, 200));
              c.add(pane);
              setSize(400, 400);
              show();
         public static void main(String args[])
              MainClass m = new MainClass();
              m.addWindowListener(
                   new WindowAdapter()
                        public void windClosing(WindowEvent e)
                             System.exit(0);
    }and another java file for the TextDraw
    import javax.swing.*;
    import java.awt.*;
    public class TextDraw extends JPanel
       public TextDraw()
             setBackground(Color.WHITE);           
                setSize(500,  500);
                show();
       public void paint(Graphics g)
              for(int x = 0; x < 20; x++)
                   g.drawString("Hello world!", 20, (x)*20);
                   g.fillRect(5, (x)*20, 10, 10);
    }i dont realy know what to do to fix this. And if i chang the values of the "setSize(400, 400)" of the MainClass to "setSize(300, 300)" it works fine. why is it like that? sorry im not very familiar with all the paint, paintComponent and graphics things. thanks!

  • How can i call the public void paint(Graphics g) method on click of button?

    Hello,
    I have done this before i know its got to do something with the repaint() method but can someone show me with and example and call the paint method using AWT in a frame not in an applet....
    Thank you very much.

    Thank You very Much.... You have cleared all my doubts.
    But my main objective is to display an image in an AWT Frame !!
    here is the code that i have been trying..
    import java.awt.*;
    import java.awt.event.*;
    public class check6 extends Panel
         Frame f;
         Image i;
         public check6()
              f = new Frame("Heading");
              f.addWindowListener(new WindowAdapter(){
                   public void windowClosing(WindowEvent e){
                        System.exit(0);
              final Button b1 = new Button("Submit");
              b1.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e){
                        repaint();
              i = getImage(getCodeBase(), "titanic.jpg");
              add(b1);
              setLayout(new FlowLayout(FlowLayout.LEFT));
              f.add(this);
              f.setSize(500,500);
              f.setVisible(true);
         public void paint(Graphics g)
              g.drawImage(i,10,10,this);
         public static void main(String args[])
              new check6();
    }

  • (How) can I turn off dithering in paint(Graphics g)?

    Is there a way to turn on/off dithering?

    public void paint(Graphics g) {
       Graphics2D g2 = (Graphics2D)g;
       g2.setRenderingHint(RenderingHints.KEY_DITHERING,
          RenderingHints.VALUE_DITHER_DISABLE);
    }VALUE_DITHER_DEFAULT or VALUE_DITHER_ENABLE

  • Paint component inside JPanel

    Hi guys,
    I will start with the original problem and get to the only option that seems possible before hardcoding everything.
    I need to draw something like a binary tree. Jtree doesn't fall into binary because it does not have the root in the center. <1;2;6;8;9>, where 6 is the root, and 1,2 are its left child's children and 8,9 are right child's children. Since I couldn't find any possible solution of customizing JTree to draw it in binary format, I am left with the following option that is a step towards hardcoding.
    Create a subclass of JPanel and paint inside it. The full hardcode method would be to draw everything right here, ie the lines and the boxes.
    But since I need to listen to box selections using mouse, I was hoping to draw the boxes using JPanel/JLabel and override their paint() method.
    So is there a way to paint components(JLabel/JPanel and lines) into a painted component (JPanel)? Note, the container JPanel is not using any layout. everything is being drawn using paint method.

    You are probably going to want to create smaller objects that handle their own painting. For example... Create an extension of JComponent that handles painting a single node. Then create the JPanel to add those to. You can even go as far as writing your own LayoutManager to handle laying the Components out in your binary layout. This is really the most effective way to do it, especially if you want to be able to detect clicks and what not. You can just add MouseListener's to the individual components than.
    Hope this helps,
    Josh Castagno
    http://www.jdc-software.com

  • Why paint(Graphics) is invoked?

    I see paint (Graphics g) method in many awt.* codes. In most cases the method is not explicitly called in, anyhow it invokes. In the code below - main() calls constructor Why_Paint() which has no reference to paint(Graphics g)?
    import java.awt.*;
    import java.awt.event.*;
    class Why_Paint extends Frame
         Why_Paint()
              setTitle("Why paint() is invoked? ");
              setSize(270, 160);
              setBackground(Color.cyan);
              addWindowListener(new WindowAdapter()
                   public void windowClosing(WindowEvent e)
                        System.exit(0);
              setVisible (true);
         public void paint (Graphics g)
             g.setColor(Color.pink);
              g.fillRect(45, 40, 180, 90);     
         public static void main (String args[])
              new Why_Paint();
    }

    Thanx for your response.
    Thus the automatic paint() method invocation is the cause of blinking screen in many java programs?
    I couldn't find any hints/tutorials of customizing automatic invocations (I guess many other methods are called automaticly - main() or init() for example) in my JavaDocs. Can anybody give me an idea of where to look for a link?

  • JPanel doesn't paint graphics

    Hi ! I've been writing a program that should graph a linear function, the problem is that I'm trying to draw a line in a JPanel but this doesn't work XD...The program has 2 windows. The first one is the windows from where i get data for the function, after having the data I press a button that I want to open a new window and draw the function...
    This is the part of the code in the button on the first window that certainly opens a new window
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
       JFrame frm_grafica = new JFrame("Grafica - B-Rabbit");
       frm_grafica.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Grafica graph = new Grafica();   
       frm_grafica.getContentPane().add(graph);
       frm_grafica.setSize(250,250);
       frm_grafica.setLocation(this.getLocation().x+this.getWidth()+5, this.getLocation().y);
       frm_grafica.setVisible(true);
    }but in the new window [that is located beside the first window] this is the code, that doesn't work...yes maybe there's something i don't do or something that i am passing over..
    public class Grafica extends JPanel{
        Grafica(){
            this.setBackground(Color.WHITE);
        protected void PaintComponent(Graphics g){
            super.paint(g);
            g.setColor(Color.BLACK);
            g.drawLine(50,50, 20, 20);
    }So Hope someone helps me...

    //protected void PaintComponent(Graphics g){
    protected void paintComponent(Graphics g){//<-- it's 2.00am, small p
            //super.paint(g);
            super.paintComponent(g);

  • Custom painting/ graphics in a panel

    Ten Duke Dollars for whoever gives the best high level solution for my problem.
    I'm writing a small application that displays music notation on screen and lets the user move the notes vertically to the desired locations on the staves. The user can then do various kinds of analysis on each example. The analysis stuff is done, but I'm having trouble finding an approach that will work for the graphics.
    My basic approach so far is to subclass JPanel and use it to handle all the custom painting. I'm planning to add all needed JPEG's to JLabels. Some of the images (e.g. treble and bass clef images) will always be in a fixed location, while others (note images) will move vertically when the user drags them. For the lines on the staves and the measure lines, I'm planning to use g.drawLine.
    The following questions occur to me:
    1. How will I prevent the note images from hiding the lines? Will I need to make the images transparent GIFs or something? Or can I set some layering property in the JPanel to build up a layered drawing?
    2. to detect mouse events, should I attach mouse listeners to my panel, or to each label that contains a note image? (I considered using Ellipse objects for the note heads and lines for the stems, but this will not give me a high enough quality of image.)
    3. I will probably need to use absolute positioning in the panel class rather than FlowLayout or whatever, but I'm having trouble getting rid of the layout manager. Can you give me a few lines of code to do this?
    4. Is my overall approach correct? Is there a better, easier way to accomplish what I'm trying to do?
    thanks,
    Eric

    >
    The following questions occur to me:
    1. How will I prevent the note images from hiding the
    lines? Will I need to make the images transparent GIFs
    or something? Or can I set some layering property in
    the JPanel to build up a layered drawing?If you are going to use images (probably BufferedImages), their Transparency will probably be BITMASK or TRANSLUSCENT.
    2. to detect mouse events, should I attach mouse
    listeners to my panel, or to each label that contains
    a note image? (I considered using Ellipse objects for
    the note heads and lines for the stems, but this will
    not give me a high enough quality of image.)I don't think using JLabel objects is a good idea. Instead of add labels to a panel, I would define a custom JComponent that did its own painting in paintComponent.
    >
    3. I will probably need to use absolute positioning in
    the panel class rather than FlowLayout or whatever,
    but I'm having trouble getting rid of the layout
    manager. Can you give me a few lines of code to do
    this?If you follow my last comment, your component isn't being used as a container, so this is not relevant.
    >
    4. Is my overall approach correct? Is there a better,
    easier way to accomplish what I'm trying to do?
    thanks,
    EricCheck out forum Java 2D. That's where this topic belongs. You also need to learn the 2D API. Search for some text book references in that forum.
    From the Imipolex G keyboard of...
    Lazlo Jamf

  • Disappearing Graphics in Jpanels!

    Hi all,
    I am new to java and have a slight problem. I have two forms, one is my main form and the other is used for a settings option for changing various aspects of my program such as colours etc.
    On my main form I have 2 jPanels - one that shows my main image (it's a graph that I've created and used BufferImage to then draw it on the jPanel. The other shows a smaller version of this using similar method but scale the original image to make it smaller.
    I have a function called drawStuff which draws all the main graphs, one called drawScales which draws all the axis labels and small graphs. I called upon these when I want them to be drawn. I don't use paint() or paintcomponent() etc.
    My problem is that whenever I open my settings menu, the settings form appears over my jpanel, like it should, but all of a sudden all the images on both jpanels dissapear! so when I exit the settings menu both panels are empty! I want them to stay the way they are until I redraw them!
    public void drawStuff()
      Graphics2D l = (Graphics2D)g;         
               if (window==0){
               if ((mpress=true)| (bpre==1)){
               bpre=0;
               h.setColor(Color.white);
               h.fillRect(0,0,661,352);
               h.setColor(Color.white);
               h.fillRect(0,0,661,352);      
               h.setColor(Color.black);
               h.drawLine(0,0,0,351); // draw graph axis
               h.drawLine(0,351,661,351);
               h.setColor(jPanel9.getBackground());
               h.fillRect(1,0,660,351);
               h.setColor(jPanel10.getBackground());
               int temp=scalep;
                while (1+scalep+swp<661){
                h.fillRect((1+scalep),0,swp,351);
                scalep=scalep+swp+swp;
               if (1+scalep<661){
                h.fillRect(scalep,0,660,351);  
               scalep=temp;
               GetCols(5);
               int psize=jSlider8.getValue();
               for (int o=1;o<nolin+1;o++){   
                    if (zdata[o][1][zomlev]==1){
                        zdata[o][1][zomlev+1]=0;
                         h.fillRect(660-test1[o][1],0+test1[o][2],psize,psize);
               g.drawImage(Buffer,75,30,this);
               xl=0;
               xr=0;
               yl=0;
               yr=0;
             if (mdrag==true){
                  g.drawImage(Buffer,75,30,this);
                  g.setColor(Color.black);
                  l.setColor (Color.black);
                  l.setStroke(dashed);
                 //bottom right
                 if ((selx1 < selx2) & (sely1 < sely2)){
                 //g.setColor(Color.black);     
                 //g.drawRect(selx1, sely1, selx2-selx1, sely2-sely1);
                  g.drawRect(selx1, sely1,selx2-selx1, sely2-sely1);
                  //l.draw(rectangle);
                 xl=selx1;
                 xr=selx2;
                 yl=sely1;
                 yr=sely2;
                 //bottom left
                 if ((selx1 > selx2) & (sely1 < sely2)){
                 g.drawRect(selx2, sely1,selx1-selx2, sely2-sely1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely1;
                 yr=sely2;
                 //top left
                 if ((selx1 > selx2) & (sely1 > sely2)){
                 g.drawRect(selx2, sely2,selx1-selx2, sely1-sely2);
                 xl=selx2;
                 xr=selx1;
                 yl=sely2;
                 yr=sely1;
                 //top right
                  if ((selx1 < selx2) & (sely1 > sely2)){
                 g.drawRect(selx1, sely2,selx2-selx1, sely1-sely2);
                 xl=selx1;
                 xr=selx2;
                 yl=sely2;
                 yr=sely1;
                 GetCols(4);
                 int psize=jSlider8.getValue();
                  for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((735-test1[o][1]>xl) & (735-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){               
                         g.fillRect(735-test1[o][1],30+test1[o][2],psize,psize);
                         zdata[o][1][zomlev+1]=1;
             sxl=xl;
             sxr=xr;
             syl=yl;
             syr=yr;
             l.setStroke(stroke);
               /*if ((nbox==1) | (red==0)){
               g.setColor(Color.white);
               g.fillRect(3,16, 764, 401);
               g.setColor(Color.black);
               g.drawLine(75,380,725,380); // draw graph axis
               g.drawLine(75,380,75,30);
               //timescale();
               //timetwofour();
               g.drawString(twofours,50,400);
               String minyS=String.valueOf(miny);
               g.drawString(minyS,10,380);
               g.drawString(twofoure,705,400);
               String maxyS=String.valueOf(maxy);
               g.drawString(maxyS,10,40);
               g.drawString("Peak Energy",5,205);
               g.drawString("Time of Signal",400,400);
           if (nbox==1){
                 g.setColor(Color.white); // clear previous box
                 g.fillRect(xl, yl, xr-xl+1, yr-yl+1);
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>xl) & (725-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 nbox=0;
                 xl=0;
                 xr=0;
                 yl=0;
                 yr=0;
             if (red==1){
               g.setColor(Color.black);
               g.drawLine(75,380,725,380); // draw graph axis
               g.drawLine(75,380,75,30);
                 //bottom right
                 if ((selx1 < selx2) & (sely1 < sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-3) & (725-test1[o][1]<sxr+3) & (30+test1[o][2]>syl-3) & (30+test1[o][2]<syr+3)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx1, sely1,selx2-selx1, sely2-sely1);
                 g.setColor(Color.white);
                 g.fillRect(selx1+1, sely1+1, selx2-selx1-1, sely2-sely1-1);
                 xl=selx1;
                 xr=selx2;
                 yl=sely1;
                 yr=sely2;
                 //bottom left
                 if ((selx1 > selx2) & (sely1 < sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (725-test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx2, sely1,selx1-selx2, sely2-sely1);
                 g.setColor(Color.white);
                 g.fillRect(selx2+1, sely1+1, selx1-selx2-1, sely2-sely1-1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely1;
                 yr=sely2;
                 //top left
                 if ((selx1 > selx2) & (sely1 > sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (30+test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx2, sely2,selx1-selx2, sely1-sely2);
                 g.setColor(Color.white);
                 g.fillRect(selx2+1, sely2+1, selx1-selx2-1, sely1-sely2-1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely2;
                 yr=sely1;
                 //top right
                  if ((selx1 < selx2) & (sely1 > sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (30+test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx1, sely2,selx2-selx1, sely1-sely2);
                 g.setColor(Color.white);
                 g.fillRect(selx1+1, sely2+1, selx2-selx1-1, sely1-sely2-1);
                 xl=selx1;
                 xr=selx2;
                 yl=sely2;
                 yr=sely1;
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>xl) & (725-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){
                         g.setColor(Color.blue);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
             sxl=xl;
             sxr=xr;
             syl=yl;
             syr=yr;
             if (window==0){
                 for (int o=1;o<nolin+1;o++){
                     if (red==0){
                         if (zdata[o][1][zomlev]==1){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                     //if (red==0){
                    // if (data[o][1]==1){
                    // if (((540-test1[o][1])<selx2) && ((540-test1[o][1])>selx1) && ((380-test1[o][2])>sely1) && ((380-test1[o][2])<sely2)){
                     //       g.setColor(new java.awt.Color(200, 200, 200));}
                 //   g.fillOval(540-test1[o][1],380-test1[o][2],2,2);   
          /*if (window==1){
               if ((nbox==1) | (red==0)){
               g.setColor(Color.white);
               g.fillRect(3,16, 764, 401);
               g.setColor(Color.black);
               g.drawLine(75,zeroy,725,zeroy); //  y zero axis
               g.drawLine(zerox,30,zerox,380); // x zero axis
               String minxS="0";
               g.drawString(minxS,zerox,400);
               String minyS="0";
               g.drawString(minyS,10,zeroy);
               g.drawString("Log 10 R",10,205);
               g.drawString("Difference in Time of Flight",400,400);
           if (nbox==1){
                 g.setColor(Color.white); // clear previous box
                 g.fillRect(xl, yl, xr-xl+1, yr-yl+1);
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>xl) & (725-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 nbox=0;
                 xl=0;
                 xr=0;
                 yl=0;
                 yr=0;
             if (red==1){
               g.setColor(Color.black);
               g.drawLine(75,zeroy,725,zeroy); //  y zero axis
               g.drawLine(zerox,30,zerox,380); // x zero axis
                 //bottom right
                 if ((selx1 < selx2) & (sely1 < sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-3) & (725-test1[o][1]<sxr+3) & (30+test1[o][2]>syl-3) & (30+test1[o][2]<syr+3)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx1, sely1,selx2-selx1, sely2-sely1);
                 g.setColor(Color.white);
                 g.fillRect(selx1+1, sely1+1, selx2-selx1-1, sely2-sely1-1);
                 xl=selx1;
                 xr=selx2;
                 yl=sely1;
                 yr=sely2;
                 //bottom left
                 if ((selx1 > selx2) & (sely1 < sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (725-test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx2, sely1,selx1-selx2, sely2-sely1);
                 g.setColor(Color.white);
                 g.fillRect(selx2+1, sely1+1, selx1-selx2-1, sely2-sely1-1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely1;
                 yr=sely2;
                 //top left
                 if ((selx1 > selx2) & (sely1 > sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (30+test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx2, sely2,selx1-selx2, sely1-sely2);
                 g.setColor(Color.white);
                 g.fillRect(selx2+1, sely2+1, selx1-selx2-1, sely1-sely2-1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely2;
                 yr=sely1;
                 //top right
                  if ((selx1 < selx2) & (sely1 > sely2)){
                 if (sxl>0){
                 g.setColor(Color.white);
                 g.fillRect(sxl-2, syl-2, sxr-sxl+4, syr-syl+4);//all
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>sxl-4) & (725-test1[o][1]<sxr+4) & (30+test1[o][2]>syl-4) & (30+test1[o][2]<syr+4)){
                         g.setColor(Color.lightGray);                
                         g.fillOval(725-test1[o][1],30+test1[o][2],2,2);
                 g.setColor(Color.black);
                 g.drawRect(selx1, sely2,selx2-selx1, sely1-sely2);
                 g.setColor(Color.white);
                 g.fillRect(selx1+1, sely2+1, selx2-selx1-1, sely1-sely2-1);
                 xl=selx1;
                 xr=selx2;
                 yl=sely2;
                 yr=sely1;
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((725-test1[o][1]>xl) & (725-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){
                         g.setColor(Color.red);
                         g.fillOval(725-zerox+test1[o][1],30+zeroy-test1[o][2],2,2);
                     if ((725-test2[o][1]>xl) & (725-test2[o][1]<xr) & (30+test2[o][2]>yl) & (30+test2[o][2]<yr)){
                         g.setColor(Color.blue);
                         g.fillOval(725-zerox+test2[o][1],30+zeroy-test2[o][2],2,2);
                    if ((725-test3[o][1]>xl) & (725-test3[o][1]<xr) & (30+test3[o][2]>yl) & (30+test3[o][2]<yr)){
                    g.setColor(Color.green);
                   g.fillOval(725-zerox+test3[o][1],30+zeroy-test3[o][2],2,2);   
             sxl=xl;
             sxr=xr;
             syl=yl;
             syr=yr;
             if (window==1){ 
             //plot points depending on choosen sets of data 
             if ((cho==1) | (cho==4) | (cho==5) | (cho==7)){
             for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){ 
                 g.setColor(Color.red);
                   g.fillOval(zerox+test1[o][1],zeroy-test1[o][2],2,2);   
             if ((cho==2) | (cho==4) | (cho==6) | (cho==7)){
             for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){ 
                 g.setColor(Color.blue);
                   g.fillOval(zerox+test2[o][1],zeroy-test2[o][2],2,2);   
             if ((cho==3) | (cho==5) | (cho==6) | (cho==7)){
             for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){
                 g.setColor(Color.green);
                   g.fillOval(zerox+test3[o][1],zeroy-test3[o][2],2,2);   
        // =========  START HERE FOR SENSOR PAIRS ===============
           if (window==1){
               if ((mpress=true)| (bpre==1)){
               bpre=0;   
               h.setColor(Color.white);
               h.fillRect(0,0,661,352);
               h.setColor(Color.black);
               h.setColor(jPanel9.getBackground());
               h.fillRect(0,0,661,352);
               h.setColor(jPanel10.getBackground());
               scale=0;
               int factor=0;
               if (jRadioButton13.isSelected()==true){
                    factor=5;
                if (jRadioButton14.isSelected()==true){
                    factor=10;
                if (jRadioButton15.isSelected()==true){
                    factor=30;
                if (jRadioButton16.isSelected()==true){
                    factor=60;
               sw=(int)(factor/divfx);
               while (1+scale+sw<661){
               h.fillRect((1+scale+zerox),0,sw,352);
               scale=scale+sw+sw;
               if (1+scale<661){
               h.fillRect((1+scale+zerox),0,661,352); 
               scale=zerox;
               while (scale-sw-sw>0){
               h.fillRect((scale-sw-sw),0,sw,352);
               scale=scale-sw-sw;
               if (scale-sw>0){
               h.fillRect(0,0,scale-sw,352); 
               //if (1+scale<661){
               //h.fillRect(scale,0,660,351);  
               h.setColor(Color.black);
               h.drawLine(0,zeroy,661,zeroy); //  y zero axis
               h.drawLine(zerox,0,zerox,381); // x zero axis
             int psize=jSlider1.getValue();
             if ((cho==1) | (cho==4) | (cho==5) | (cho==7)){
              GetCols(6);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){
                   zdata[o][1][zomlev+1]=0; 
                   h.fillRect(zerox+test1[o][1],zeroy-test1[o][2],psize,psize);   
             if ((cho==2) | (cho==4) | (cho==6) | (cho==7)){
                  GetCols(7);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){ 
                   zdata[o][1][zomlev+1]=0;
                   h.fillRect(zerox+test2[o][1],zeroy-test2[o][2],psize,psize);   
             if ((cho==3) | (cho==5) | (cho==6) | (cho==7)){
             GetCols(8);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){
                   zdata[o][1][zomlev+1]=0;
                   h.fillRect(zerox+test3[o][1],zeroy-test3[o][2],psize,psize);   
               xl=0;
               xr=0;
               yl=0;
               yr=0;
                g.drawImage(Buffer,75,30,this);
             if (mdrag==true){
               g.drawImage(Buffer,75,30,this);
                  g.setColor(Color.black);
                  l.setColor (Color.black);
                  l.setStroke(dashed);
                 //bottom right
                 if ((selx1 < selx2) & (sely1 < sely2)){
                 //g.setColor(Color.black);     
                 //g.drawRect(selx1, sely1, selx2-selx1, sely2-sely1);
                  g.drawRect(selx1, sely1,selx2-selx1, sely2-sely1);
                  //l.draw(rectangle);
                 xl=selx1;
                 xr=selx2;
                 yl=sely1;
                 yr=sely2;
                 //bottom left
                 if ((selx1 > selx2) & (sely1 < sely2)){
                 g.drawRect(selx2, sely1,selx1-selx2, sely2-sely1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely1;
                 yr=sely2;
                 //top left
                 if ((selx1 > selx2) & (sely1 > sely2)){
                 g.drawRect(selx2, sely2,selx1-selx2, sely1-sely2);
                 xl=selx2;
                 xr=selx1;
                 yl=sely2;
                 yr=sely1;
                 //top right
                  if ((selx1 < selx2) & (sely1 > sely2)){
                 g.drawRect(selx1, sely2,selx2-selx1, sely1-sely2);
                 xl=selx1;
                 xr=selx2;
                 yl=sely2;
                 yr=sely1;
             int psize=jSlider1.getValue();
             if ((cho==1) | (cho==4) | (cho==5) | (cho==7)){
              GetCols(9);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){
                     if ((75+zerox+test1[o][1]>xl) & (75+zerox+test1[o][1]<xr) & (30+zeroy-test1[o][2]>yl) & (30+zeroy-test1[o][2]<yr)){
                     g.fillRect(75+zerox+test1[o][1],30+zeroy-test1[o][2],psize,psize);   
                     zdata[o][1][zomlev+1]=1;
             if ((cho==2) | (cho==4) | (cho==6) | (cho==7)){
                  GetCols(12);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){ 
                 if ((75+zerox+test2[o][1]>xl) & (75+zerox+test2[o][1]<xr) & (30+zeroy-test2[o][2]>yl) & (30+zeroy-test2[o][2]<yr)){
                   g.fillRect(75+zerox+test2[o][1],30+zeroy-test2[o][2],psize,psize);   
               zdata[o][1][zomlev+1]=1;
             if ((cho==3) | (cho==5) | (cho==6) | (cho==7)){
             GetCols(13);
                 for (int o=1;o<nolin+1;o++){
                 if (zdata[o][1][zomlev]==1){
                 if ((75+zerox+test3[o][1]>xl) & (75+zerox+test3[o][1]<xr) & (30+zeroy-test3[o][2]>yl) & (30+zeroy-test3[o][2]<yr)){
                   g.fillRect(75+zerox+test3[o][1],30+zeroy-test3[o][2],psize,psize);   
                 zdata[o][1][zomlev+1]=1;
             sxl=xl;
             sxr=xr;
             syl=yl;
             syr=yr;
             l.setStroke(stroke);
           // =========  START HERE FOR PHASE ===============
           if (window==2){
               if ((mpress=true)| (bpre==1)){
               bpre=0;   
               h.setColor(Color.white);
               h.fillRect(0,0,661,352);
               h.setColor(jPanel9.getBackground());
               h.fillRect(1,0,165,351);
               h.fillRect(331,0,165,351);
               h.setColor(jPanel10.getBackground());
               h.fillRect(166,0,165,351);
               h.fillRect(496,0,165,351);
               h.setColor(Color.black);
               h.drawLine(0,0,0,351); // draw graph axis
               h.drawLine(0,351,661,351);
               GetCols(11);
               int psize=jSlider9.getValue();
                 for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                         zdata[o][1][zomlev+1]=0;
                         h.fillRect(660-test1[o][1],0+test1[o][2],psize,psize);
               xl=0;
               xr=0;
               yl=0;
               yr=0;
                g.drawImage(Buffer,75,30,this);
             if (mdrag==true){
               g.drawImage(Buffer,75,30,this);
                  g.setColor(Color.black);
                  l.setColor (Color.black);
                  l.setStroke(dashed);
                 //bottom right
                 if ((selx1 < selx2) & (sely1 < sely2)){
                 //g.setColor(Color.black);     
                 //g.drawRect(selx1, sely1, selx2-selx1, sely2-sely1);
                  g.drawRect(selx1, sely1,selx2-selx1, sely2-sely1);
                  //l.draw(rectangle);
                 xl=selx1;
                 xr=selx2;
                 yl=sely1;
                 yr=sely2;
                 //bottom left
                 if ((selx1 > selx2) & (sely1 < sely2)){
                 g.drawRect(selx2, sely1,selx1-selx2, sely2-sely1);
                 xl=selx2;
                 xr=selx1;
                 yl=sely1;
                 yr=sely2;
                 //top left
                 if ((selx1 > selx2) & (sely1 > sely2)){
                 g.drawRect(selx2, sely2,selx1-selx2, sely1-sely2);
                 xl=selx2;
                 xr=selx1;
                 yl=sely2;
                 yr=sely1;
                 //top right
                  if ((selx1 < selx2) & (sely1 > sely2)){
                 g.drawRect(selx1, sely2,selx2-selx1, sely1-sely2);
                 xl=selx1;
                 xr=selx2;
                 yl=sely2;
                 yr=sely1;
                 GetCols(10);
                 int psize=jSlider9.getValue();
                  for (int o=1;o<nolin+1;o++){
                     if (zdata[o][1][zomlev]==1){
                     if ((735-test1[o][1]>xl) & (735-test1[o][1]<xr) & (30+test1[o][2]>yl) & (30+test1[o][2]<yr)){               
                         g.fillRect(735-test1[o][1],30+test1[o][2],psize,psize);
                         zdata[o][1][zomlev+1]=1;
             sxl=xl;
             sxr=xr;
             syl=yl;
             syr=yr;
             l.setStroke(stroke);
    public void drawscales(){
        Font font1 = new Font("TW Cen MT", Font.BOLD,12);
        Font font2 = new Font("TW Cen MT",Font.PLAIN,10);
        Font font3 = new Font("TW Cen MT",Font.BOLD,10);
        g.setColor(Color.white);
        g.fillRect(2,2, 764, 409);
        g.setColor(Color.black); 
        if (window==0){
               g.setFont(font2);
               g.drawString(twofours,52,400);
               String minyS=String.valueOf(miny);
               g.drawString(minyS,50,383);
               g.drawString(twofoure,712,400);
               String maxyS=String.valueOf(may);
               g.drawString(maxyS,50,33);
               g.setFont(font1);
               g.drawString("Energy againt Time Graph",330,20);
               g.setFont(font3);
               g.drawString("Peak Energy",10,205);
               g.drawString("/ pJ",30,217);
               g.drawString("Time of Signal",370,400);  
               /*h.setColor(Color.black);
               h.drawLine(0,0,0,351); // draw graph axis
               h.drawLine(0,351,661,351);
               h.setColor(jPanel9.getBackground());
               h.fillRect(1,0,660,351);
               h.setColor(jPanel10.getBackground());
                while (1+scale+sw<661){
                h.fillRect((1+scale),0,sw,351);
                scale=scale+sw+sw;
               if (1+scale<661){
                h.fillRect(scale,0,660,351);  
               for (int o=1;o<nolin+1;o++){
                   GetCols(5);
                     if (zdata[o][1][zomlev]==1){
                         int psize=jSlider8.getValue();
                         h.fillRect(660-test1[o][1],0+test1[o][2],psize,psize);
               //g.drawImage(Buffer,75,30,this);
               sma.drawImage(smallpow,1,1,jPanel2);
               double ndf =((zmpmr-zmpml)/219);
               double ndyf=((zmpmt-zmpmb)/116);
               int wdp=(int)((dmax-dmix)/ndf);
               int htp=(int)((may-miy)/ndyf);
               int sx=(int)((dmix-zmpml)/ndf);
               int sy=(int)((zmpmt-may)/ndyf);
               sma.drawRect(1+sx,1+sy,wdp,htp);
        if (window==1){
               g.setColor(Color.black);
               g.setFont(font2);
               String minxS="0";
               g.drawString(minxS,zerox+72,395);
               String minyS="0";
               g.drawString(minyS,65,zeroy+34);
               g.setFont(font3);
               g.drawString("Log 10 R /",9,200);
               g.drawString("Ratio of",13,212);
               g.drawString("Energy",13,224);
               g.drawString("Difference in Time of Flight / Seconds",315,408);
               g.setFont(font1);
               g.drawString("Sensor Pairs Graph",350,20);
              /* h.setColor(Color.white);
               h.fillRect(0,0,661,352);
               h.setColor(Color.black);
               h.setColor(new java.awt.Color(240, 240, 255));
               h.fillRect(0,0,661,352);
               h.setColor(new java.awt.Color(220, 220, 255));
               scale=0;
               sw=(int)(10/divfx);
               while (1+scale+sw<661){
               h.fillRect((1+scale+zerox),0,sw,352);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

    Thanks,
    I have tried it using
    public void paintComponent(Graphics g){
    drawStuff();
    }in the main body of code and then call upon PaintComponent when I close the settings frame but it makes no difference, I still get the big white box as shown in Q3 - I've tried reading the problem solving section for swing graphics from sun but to no avail.
    Any advice on where paintcomponent should be placed / called?
    Any ideas would be great.
    Thanks,
    John

  • AWT/Swing difference Component.paint(Graphics g)

    In the following code, I try to paint some component into a VolatileImage, also tried with BufferedImage, and draw the Image on some other component. Anyways it works with java.awt.Panel doesn't with JPanel, does anyone have a clue why?
    Differences between Graphics and Graphics2D maybe?
    package sqltest;
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    public class AWTVC extends java.awt.Panel {
        public AWTVC(Component c) {
            this.c = c;
            //initComponents();
            Thread t = new Thread(){
                public void run(){
                    try{
                    Thread.sleep(2000);
                    bu=createVolatileImage(500,500);
                    while(true){
                        AWTVC.this.c.paint(bu.createGraphics());
                        getComponent(0).paint(bu.createGraphics());
                        AWTVC.this.repaint();
                        Thread.sleep(80);
                    }catch(Exception e){
                        e.printStackTrace();
            t.start();
            initComponents();
        private void initComponents() {
            setLayout(new java.awt.BorderLayout());
            addComponentListener(new java.awt.event.ComponentAdapter() {
                public void componentResized(java.awt.event.ComponentEvent evt) {
                    formComponentResized(evt);
        private void formComponentResized(java.awt.event.ComponentEvent evt) {
            bu=createVolatileImage(getWidth(), getHeight());
        final Component c;
        VolatileImage bu=null;
        public void update(Graphics g){
            g.drawImage(bu,0,0,getWidth(),getHeight(),null);
    }Any help would be greatly appreciated!
    Cheers

    Well thanx alot,
    Well I just asked out of curiosity, it works perfectly with the LIGHTWEIGHT_RENDERER, I was just curious to know why AWT components won't draw into Graphics2D (That seemed to be the problem). Well It works now so it doesn't really matter. Well I think the HeavyWeight Renderer is hardware accelerated, maybe that's the reason. Also since I'm using jdk 1.5 do you think setting the -Dsun.java2d.opengl=true property will improve performance. I have cpu cycles to spare, so I can't see the difference ;-)

  • Problem with Graphics and JPanel...

    I would like to draw a rectangle on a JPanel. Here is my code:
    JPanel panel = new JPanel();
    Graphics g = panel.getGraphics();
    g.drawRect(20,20,20,20);
    When I run my program, I have a NullPointerException...
    Do you any suggestions to fix the problem??
    thanks
    Pete

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    public class pete extends JFrame {
      public pete() {
        JPanel panel = new JPanel() {
          public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int midx = getSize().width / 2;
            int midy = getSize().height / 2;
            g2.draw(new Rectangle2D.Double(midx/2, midy/2, midx, midy));
        getContentPane().add(panel, "Center");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,300);
        setLocation(300,100);
        setVisible(true);
      public static void main(String[] args) {
        new pete();
    }

  • How do I paint graphics in a JFrame created by netbeans GUI builder?

    I hope it's ok to ask netbeans questions here. Frustrated with Eclipse I'm trying netbeans. I went through the tutorial for the UI builder and I have no trouble laying out the usual swing widgets like JPanels, JButtons, etc. What I haven't figured out is how to mix ordinary graphics like Graphics.draw() with these components.
    Crude example: 2 JPanels in a JFrame. The one on the left has a JSlider bar. The one on the right has a line or something that gets bigger or smaller according to the slider bar.
    If I weren't using the UI builder I'd create a new class that is an extension of JPanel, and override paintComponent() with my draw commands. If I did that to a JPanel I layed out with the UI builder it would get lumped in with the protected code that's created on the fly and probably erased.
    So how do I mix swing components with ordinary graphics without breaking the netbeans UI builder form?
    Thanks,
    Apchar

    This forum is a Java language forum. The NetBeans site has a mailing list. Nabble forums also has NB forums. The contents of both are crossposted. So, you really should post NB use questions to one of them. Post only Java language questions here.
    You can add custom code to the guarded (blue) code blocks. Open a component's property window and select the code button at the top. This allows you to insert code in numerous locations within the guarded blocks and retain the code.
    You can also click a component's property's ellipsis (...) button to bring up the Property Editor dialog box, then select "Custom Code" from the dropdown list at the top of the dialog.
    Note: the above instructions are for NB 6

  • Report Painter Graphics

    Hi, I would like to make a comparative graphic that shows two curves: one: actual cost, the other one: plan cost. per period in the current fiscal year. (PS module, project system)
    I have already done a comparative report using transactions CJE1, CJE5 , which uses the report painter functionality, but i do not know how to show a graphic for these curves at the same time. Is there anyone that may help me with this issue? Thanks in advance
    Antonio Cestari

    Hi,
    If you would like to insert graphics into your ABAP program look at transaction GRAL. There are some examples...
    Krzys

Maybe you are looking for