Steven Coddington

Greenhorn
+ Follow
since Oct 17, 2004
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Steven Coddington

thanks for the help. slider is now working, but 'setSnapToTicks(true)' is not. I can slide to anything in between. What am I doing wrong here?

10 years ago
whats wrong with the code? I can make a slider, but the listener is apparently not doing it's job.

here is the class with main method that makes a frame.

import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;


public class SliderTester extends JFrame{

public static void main(String[] args) {
JFrame myWindow = new JFrame();
myWindow.setSize(300,300);
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container c = myWindow.getContentPane();

SliderPanel s = new SliderPanel();
c.add(s, BorderLayout.SOUTH);
myWindow.setVisible(true);

}
}

and here is the class that makes the slider:

import java.awt.event.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.*;
import javax.swing.*;

public class SliderPanel extends JSlider{
// **** Instance variables for class SliderPanel *****/
JSlider s = new JSlider(0,10,5);
int sValue;

// **** Constructors for class SliderPanel ******/

public SliderPanel() {
s = new JSlider(0,10,5);
SliderListener l = new SliderListener();
s.addChangeListener(l);

}
// ***************** method to display output from slider
public void printState(){
sValue = s.getValue();
System.out.println(sValue);
JOptionPane.showMessageDialog(null, "Slider Value = " + sValue);
}
// ***************inner class change listener.
private class SliderListener implements ChangeListener{
public void stateChanged(ChangeEvent e){

printState(); // wait for a change in state, then print slider value

}
}
}

not only do i not get anything printed, but when i tried to use methods like setSnapToTicks(true) and setMajorTickSpacing(2); nothing happens. I do get a slider in the right spot in my frame, and can move the arrow, but nothing happens. Apparently the listener is not listening. And, why do ticks not appear?

Driving me crazy. Any help greatly appreciated.
10 years ago
beginner programmer having trouble with Scanner and delimiters.

Here is the code that does not work:

import java.util.Scanner;

public class ScannerTester {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.println("Enter a double ");
double d = s.nextDouble();
System.out.println("Your double is " + d);
System.out.println("enter some more text\n");
String thisString = s.nextLine();
System.out.println(thisString);
}
}

The problem is that there is no pause to input the string. The method apparently reads something without waiting for keyboard input. I tried changing the new Scanner line to

Scanner s = new Scanner(System.in).useDelimiter("\n");

without success.

Am I right that a delimiter other than default is needed? If so, which one?

Thanks
10 years ago
Is there a way to explicitly go up two class levels to access a method?. For example, I have classes Shape, Triangle extends Shape, and RightTriangle extends Triangle. If I have an instance of RightTriangle and want to use, for example the toString() method in Shape from a method in RightTriangle.

I tried super.super.toString() to no avail (error message using BlueJ was <identifier> expected). I cannot find the correct way in my books.

I can go up one step at a time, of course, but that seems less cool to me. If this is the only way, I will be sorely disappointed.

Thanks in advance for any help.
11 years ago
ok, there is no such method (that I could find). I want to make an image pixel by pixel for the cellular automata game of Life. I looked in the graphics class where drawLine and drawOval live, hoping to find drawPixel or setPixel, but could not find anything like that. I suppose I could draw a line from a point to the same point, but that seems wasteful.

Is there a setPixel method somewhere?
11 years ago
I don't know how to get information from JCombo box: This class does the GUI

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PersonListGUI extends JFrame implements ItemListener
{
// instance variables
private String name[]={"Fred", "Bill", "Ethel"};
private JComboBox nameList = null;
private JFrame frame = null;
//private DescriptionPanel dp = new DescriptionPanel();
/**
* Constructor for objects of class PersonListGUI
*/
public PersonListGUI()
{
frame = new JFrame();
frame.setTitle("A List of Salespeople");
nameList = new JComboBox(name);
frame.getContentPane().add(nameList);
frame.setSize(300,70);
frame.setVisible(true);
System.out.println("hello");
}
public void actionPerformed(){
System.out.println("action performed");
}
public void itemStateChanged(ItemEvent e){
if (e.getSource() instanceof JComboBox){
String s = (String)e.getItem();
int k = nameList.getSelectedIndex();
System.out.println("k = " + k);
}
}
public int indexReturner(){
int k = nameList.getSelectedIndex();
return k;
}
}

It works fine. The box is displayed and I can change the display. This class is meant to make an instance of the box and get the returned data.

import javax.swing.*;
import java.awt.event.*;
public class Tester
{

public void Tester()
{
PersonListGUI pl = new PersonListGUI();
int k = pl.indexReturner();
System.out.println (k);
return ;
}
}


It works fine on the first pass. I get the index after the GUI is created (0), but how do I get information after each change to the selected item. It must have something to do with the listener that I am not implementing.

Thanks
13 years ago
I am a beginner and just learning to use swing widgets. I want to make a color maker to see what various rgb combos look like. I want three sliders, with values from 0-255, get the values, and re-color a region so I can see the effect. I have the sliders working, but don't know how to get the value from the change listener to the panel drawer. Here is what I have so far.

Any help is greatly appreciated. Thanks.


/*
* SliderTest.java
*/

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JSlider;
import javax.swing.event.*;

public class SliderTest extends JFrame{
// Declare class variables
JFrame frame;
JLabel label;

// Creates a new instance of SliderTest
public static void main(String[] args) {
SliderTest gui = new SliderTest();
gui.go();
}

// Build a gui
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton colorButton = new JButton("Change Circle");

// Add three sliders to the a container

Container sliderContainer = getContentPane();
sliderContainer.setLayout(new GridLayout(3,1));

JSlider redSlider = new JSlider(0,0,255,127);
JSlider greenSlider = new JSlider(0,0,255,127);
JSlider blueSlider = new JSlider(0,0,255,127);

redSlider.addChangeListener(new RedChangeListener());

sliderContainer.add(redSlider);
sliderContainer.add(greenSlider);
sliderContainer.add(blueSlider);

MyDrawPanel drawPanel= new MyDrawPanel();

frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.NORTH, sliderContainer);

frame.setSize(800,700);
frame.setVisible(true);
}

// An inner class for the slider event
class RedChangeListener implements ChangeListener {
public void stateChanged(ChangeEvent e){
JSlider redSource = (JSlider)e.getSource();
if (!redSource.getValueIsAdjusting()) {
redValue = (int)redSource.getValue();
System.out.println (redValue);
}
}
}

// Make the Draw Panel an inner class
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {

int redValue = 127; /* This is where I would like to put the value from the slider.
int greenValue = 127;
int blueValue = 127;
Color newColor = new Color(redValue, greenValue, blueValue);

//erase old oval by repainting rectangle
g.fillRect(0, 0, this.getWidth(), this.getHeight());

g.setColor(newColor);
g.fillOval(70, 70, 100, 100);
}
public void stateChanged(javax.swing.event.ChangeEvent changeEvent) {
}

}
}
14 years ago
I need to do a deep copy of an object with both primative and String type variables. "clone" returns a copy with values for primatives and references for non-primative String ( I think). Is there an alternative to clone() that does deep, or can someone help me write an alternative.
14 years ago
A tie between "pi" and "being john malkovich"
14 years ago
Is there an easy way to draw a line segment (like line(x1,y1,s2,y2) only with an arrowhead on one end. I am trying to create an applet to help students visualize vectors.
14 years ago