hambonie's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
34 (0.03 per day)
Joined:
13-March 12
Profile Views:
2,536
Last Active:
User is offline Dec 06 2013 10:56 AM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Linux
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
Who Cares
Your Car:
LandRover
Dream Kudos:
0
Icon   hambonie has not set their status

Posts I've Made

  1. In Topic: How to display specific lables in a histogram?

    Posted 4 Apr 2013

    thanks guys. the fix was simple...for some reason i was having a mental block
       import javax.swing.*;
       import java.awt.*;
    
       public class Histogram extends JPanel{
       // Count the occurrences of 26 letters
          private int[] count;
       
       /** Set the count and display histogram */
          public void showHistogram(int[] count) {
             this.count = count;
             repaint();
          }
       
          @Override /** Paint the histogram */
          protected void paintComponent(Graphics g) {
             if (count == null) 
                return; // No display if count is null
          
             super.paintComponent(g);
          
          // Find the panel size and bar width and interval dynamically
             int width = getWidth();
             int height = getHeight();
             int interval = (width - 40) / count.length;
             int individualWidth = (int)(((width - 40) / 24) * 0.60);
          
          // Find the maximum count. The maximum count has the highest bar
             int maxCount = 0;
             for (int i = 0; i < count.length; i++) {
                if (maxCount < count[i])
                   maxCount = count[i];
             }
          
          // x is the start position for the first bar in the histogram
             int x = 30;
          
          // Draw a horizontal base line
             g.drawLine(10, height - 45, width - 10, height - 45);
             for (int i = 0; i < count.length; i++) {
             // Find the bar height
                int barHeight =
                   (int)(((double)count[i] / (double)maxCount) * (height - 55));
             
             // Display a bar (i.e., rectangle
                //System.out.println("Bar height" +barHeight);
                
                g.drawRect(x, height - 45 - barHeight, individualWidth,
                      barHeight);
             /////FIX///////
             // Display a letter under the base line
                if(barHeight != 0){
                   g.drawString((char)(65 + i) + "", x, height - 30);
                
                // Move x for displaying the next character
                   x += interval;
                }
                else {
                   System.out.println("Letter removed: "+ (char)(65+i));
                }
             }
          }
       
          @Override
          public Dimension getPreferredSize() {
             return new Dimension(300, 300);
          }
       }
    
    
  2. In Topic: Can't get 2 paint components to appear in the same jpanel

    Posted 28 Mar 2013

    View PostGregBrannon, on 28 March 2013 - 01:52 PM, said:

    These lines (I uncommented one of them and changed the class name):
            add(new DrawCar2());
            add(new DrawCar2());
    

    would add two identical cars to your container. You want to be able to add two different cars. You would accomplish that by creating separate instances:

    DrawCar2 car1 = new DrawCar2();
    DrawCar2 car2 = new DrawCar2();

    and then modifying the starting locations, speed, color, etc. of the two cars so that they both appear in different places, doing different things, maybe looking different. You would accomplish that by creating setters to change the initial conditions of the two (or more) instances or by including the differences in the constructor.

    Start by varying the initial positions of each car.

    Cute little exercise, and I think you could learn a lot by playing around with it.


    greg...that definitely sounds like a simpler approach.
  3. In Topic: Can't get 2 paint components to appear in the same jpanel

    Posted 28 Mar 2013

    this is my second try...(i used one timer) and i am still getting the same results:
    //java 2
    //16-15
    //by taddei
    //draw car
       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
    
       public class DrawCar2 extends JPanel {
          protected int x = 0;
          protected int y = 150;
          protected int z = 300;
          protected int c = 0;
       	
          protected int x2 = 20;
          protected int y2 = 150;
          protected int z2 = 300;
          protected int c2 = 0;
          
          public DrawCar2() {
          
          /*Timer timer1 = new Timer(200, 
                   new ActionListener(){
                      public void actionPerformed(ActionEvent e) {
                         x += 10;
                         c ++;
                         repaint();
                         System.out.println("x "+x);
                      }
                   });
             timer1.start();
          	*/
             Timer timer2 = new Timer(100, 
                   new ActionListener(){
                      public void actionPerformed(ActionEvent f) {
                      
                      	x += 10;
                         c ++;
                         repaint();
                         System.out.println("x "+x);
                      
                         x2 += 10;
                         c2 ++;
                         repaint();
                         System.out.println("x2 "+x2);
                      }
                   });
             timer2.start();
             
          }
                     
          public void paintComponent(Graphics g) {
             super.paintComponent(g);
          ///////car 1//////////////
             y = getHeight();
             z = getWidth();
             g.setColor(Color.WHITE);
             g.fillRect(0, 0, z, y);
             Polygon polygon = new Polygon();
             polygon.addPoint(x + 10, y - 21);
             polygon.addPoint(x + 20, y - 31);
             polygon.addPoint(x + 30, y - 31);
             polygon.addPoint(x + 40, y - 21);
               
             if (x < z - 40) {
                g.setColor(Color.BLACK);
                g.fillOval(x + 10, y - 11, 10, 10);
                g.fillOval(x + 30, y - 11, 10, 10);
                g.setColor(Color.GREEN);
                g.fillRect(x, y - 21, 50, 10);
                g.setColor(Color.GRAY);
                g.fillPolygon(polygon);
                g.setColor(Color.BLUE);
             }
             else
                x = 0;
          System.out.println("This is car 1");
          //////////car2///////////////////////////			
             y2 = getHeight();
             z2 = getWidth();
             g.setColor(Color.WHITE);
             g.fillRect(0, 0, z2, y2);
             Polygon polygon2 = new Polygon();
             polygon2.addPoint(x2 + 10, y2 - 21);
             polygon2.addPoint(x2 + 20, y2 - 31);
             polygon2.addPoint(x2 + 30, y2 - 31);
             polygon2.addPoint(x2 + 40, y2 - 21);
                
             if (x2 < z2 - 40) {
                g.setColor(Color.BLACK);
                g.fillOval(x2 + 10, y2 - 11, 10, 10);
                g.fillOval(x2 + 30, y2 - 11, 10, 10);
                g.setColor(Color.RED);
                g.fillRect(x2, y2 - 21, 50, 10);
                g.setColor(Color.GRAY);
                g.fillPolygon(polygon2);
                g.setColor(Color.RED);
             }
             else
                x2 = 0;
          System.out.println("This is car 2");
          
          		
          }
          
       }
    		
       
    	
    
    
    
  4. In Topic: Error when reading/writing boolean value with JOption*

    Posted 28 Feb 2013

    I got it. As it turns out i was looking in the wrong place for my solution. i had to add the following "super" to my TriangleNew class:

    public TriangleNew(double side1, double side2, double side3, String color, boolean filled) {
       super(color,filled);
    	 this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    
    


    and that allowed me to add the following to my test program:
    TriangleNew triangle = new TriangleNew(side1, side2, side3, color, filled);
    


    Thanks for the help!
  5. In Topic: Error when reading/writing boolean value with JOption*

    Posted 28 Feb 2013

    thanks...these corrections let the test program compile but i still am not displaying the correct boolean choice in the output
    ///will not read boolean chioce
          
             boolean filled = (JOptionPane.showConfirmDialog(null,
                "Is the triangle filled? ", "Please select",
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);
          
          
          
          
             TriangleNew triangle = new TriangleNew(side1, side2, side3);
          
          
          
          //will not display boolean choice or color
    			JOptionPane.showMessageDialog(null, "The area is: " + triangle.getArea()+
                "\nThe perimeter is: " + triangle.getPerimeter() + "\nThe color is: " +
                triangle.getColor() + "\nTriangle is filled: " + triangle.isFilled());
          
          
          }
       }
    
    
    

My Information

Member Title:
New D.I.C Head
Age:
38 years old
Birthday:
May 5, 1976
Gender:
Years Programming:
2
Programming Languages:
c
html
java

Contact Information

E-mail:
Private
Website URL:
Website URL  http://distrodisco.com

Friends

Comments

hambonie has no profile comments yet. Why not say hello?