|
Hi I'm trying to teach myself java. I have been going well till i ran accross chapter 5. GridWorld part 1. I am new to IntelliJ, and need some help importing bugrunner.java.
My question is should i start a new project then import the file?
Thanks in advance
David
|
|
|
|
|
|
A five for the effort, but alas still can't import the project. I guess i'll move on to chapter 6 and hope I don't miss much.
Thanks anyway.
David
|
|
|
|
|
David C# Hobbyist. wrote: still can't import the project. Import into what? Have you tried just creating a new project and using copy and paste to add the source code?
Veni, vidi, abiit domum
|
|
|
|
|
how to write code for decision tree classification(Data mining topic) in java?
|
|
|
|
|
You need to provide more details of exactly what your problem is. If you are just looking for general information to help you then try http://www.codeproject.com/KB/java/[^].
Veni, vidi, abiit domum
|
|
|
|
|
how to write while
2 power 2 + 4 power 2 + 6 power 2 + 8 power 2
public static void main(String[] args) {
int i=1;
int f=0;
while (i <=8){
f=f+i;
i++;
}System.out.println(Math.pow(f, 2));
}
}
|
|
|
|
|
int i=0;
int f=0;
while(i<8){
i=i+2;
f+=(Math.pow(i, 2));
}
System.out.println(f);
|
|
|
|
|
The points are stored in MySQL database in data-type of MEDIUM-TEXT, these examples of the points (coordinates)stored in this form(Starting two points are moveTo and each line is a curve)
458.41016 425.70843 427.74316 392.55343 403.93516 370.91243
399.48516 366.83843 398.54916 368.02743 397.41516 372.27043
394.75116 382.25643 392.96616 392.69543 391.09516 402.03043
390.35916 405.62343 389.79116 406.92443 392.62616 409.52743
406.00316 421.83343 442.19716 458.07143 444.89016 482.76843
431.76716 528.31343 393.39116 574.56743 350.22516 594.56743
316.63916 610.12643 278.88716 614.34043 242.18316 610.35243
232.12112,609.27843 228.38012 619.29143 238.47016 621.92243
274.01216 631.28543 320.32416 637.73643 356.57416 628.91043
420.03416 613.46343 456.48216 533.71643 457.61616 470.82943
all the above points have same Object-ID.
So, my question is How can i create GeneralPath objects to draw the shapes, I know there's a method curveTo() used to Add a curved segment, defined by three new points, to the path by drawing a Bézier curve, and How can i use all these points(coordinates) to draw curve on JFrame also i have so many others i need to combine all of them together based on Object-ID's ?
The program is :
import java.sql.*;
public class selectStmt {
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://127.0.0.1:3306/abo";
private static final String DB_USER = "username";
private static final String DB_PASSWORD = "pswd";
public selectStmt(){
try {
selectRecordsFromTable();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private static void selectRecordsFromTable() throws SQLException {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String selectSQL = "SELECT * FROM abo.pathitems where ObjectID=1";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(selectSQL);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(
DB_CONNECTION, DB_USER,DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
}
Thanks
|
|
|
|
|
Have you worked through the tutorials I suggested in answer to your previous questions on this same issue?
Veni, vidi, abiit domum
|
|
|
|
|
not really i didn't find time at all so much of other works, anyways i should spend time on that. Thank you
|
|
|
|
|
This is a three part problem. Don't mix database access code and business logic, that leads to confusing and platform-dependent code.
The three parts I see here are:
- Read the data out of the database
- Interpret the data in an appropriate form (e.g. split the string up and turn it into a double[])
- Draw shapes with the numeric arrays you retrieve.
I think it's reasonable to combine the first two, i.e. in your database reading loop (what you posted in the question) read the field, split it on space and call Double.parseDouble on each element (exception trapped of course). If you control the database format then you shouldn't be writing numbers as text, either have 6 columns if a path segment is always 6 numbers, or store it as a binary blob containing the array ... though I must admit I've stored lists of numbers like this before as well.
The database reading code should return data objects, in this case a List<double[]> or a List<PathSegment> where PathSegment encodes the six numbers. You should then use those data objects in calling drawing code.
Looking at this mess of a class though I think you want to start a bit slower and work your way through some language tutorials. You're mixing up static and instance state (why provide a constructor that only calls a static method?), not understanding the principle of separation of concerns, not following good practice re exception handling ... Writing a database-backed 3 tier system is quite difficult and if you don't have language practices worked out then you're going to keep breaking things.
|
|
|
|
|
I am looking for making a database program by myself, but I don't want to end up in a dead end for some reason, so I need ton of examples and even better the entire source code of a sample database program. Any good book for this?
I don't care about the theory. I just want to know how to make a db program and then focus on using design patterns to improve the code.
Also, I am looking for a good book on Java Swing so I end up knowing all the ins and outs of Java Swing. I want tons of examples.
|
|
|
|
|
Given your aspirations, you need to make a better effort than posting your requirements here. Go to your local bookshop and see what's on offer. Look at on-line bookstores such as Amazon, O'Reilly etc. Search Google. Work through the Java Tutorials[^]; and practice, practice, practice.
Veni, vidi, abiit domum
|
|
|
|
|
Please start with "o'Reilly - Learning Java".
And start the Swing-Thingy first. Databases are more abstract - you can do that later when you're hooked.
|
|
|
|
|
I am also understanding Java using
Java How To
-=Dietel=-
|
|
|
|
|
Hello! I Could Print Contents of Java JTable using Printer using the following Code:
MessageFormat header=new MessageFormat("Printing JTable Contents");
MessageFormat foot = new MessageFormat("Page(0,number,integer)");
try {
JTable1.print(JTable.PrintMode.NORMAL, header, foot);
}
catch (java.awt.print.PrinterException e) {
System.err.format("Error! Cannot Print. ", e.getMessage());
}
But, I Couldn't Print All Contents of Java JTextFields and JLabels Only on Form using Printer. Can Some One help me how to print all contents of JTextFields and their corresponding JLabels???? Thanks!!!
|
|
|
|
|
|
Hello! I was trying to print contents in JText Fields and their corresponding JLabels on paper using the following code:
Toolkit tkp = jPanel7.getToolkit();
PrintJob pjp = tkp.getPrintJob(this, null, null);
Graphics g = pjp.getGraphics();
jPanel7.printAll(g);
g.dispose();
pjp.end();
But it's not printing the contents; It only prints all visible components on the jPanel7.
Please try modifying the code so as to print contents of text fields and their corresponding labels only???
Thanks!!!
|
|
|
|
|
The documentation[^] statest that printAll will invoke print which will then cause the panel and all its constituent parts to be printed. I would suggest you use your debugger to try and discover why this is not working in your case.
Veni, vidi, abiit domum
|
|
|
|
|
Wow Great!!! Problem Solved! It's working properly now!!! What I did is that I included all components to be printed(which are jLabels) in one jPanel and components not required in an other jPanel; I retrieved the data in DB to the jLabels and I call the jPanel containing my jLabels using the code I posted as question.
Thanks!!!
|
|
|
|
|
Hello
I'm new to programming and I'm trying to make a java application that will "hear" (not record necessarily) the sound and display how loud is.I'm thinking of converting the sound recordings to numbers,so I can see the difference on the sound levels.I got this code and I added the "getLevel()" method,which returns the amplitude of the current recording,but it's returning -1 everytime.I guess I'm not using it properly. Any ideas how I must call this method?I have to deliver my project in a week,so any help will be much appreciated!
public class Capture extends JFrame {
protected boolean running;
ByteArrayOutputStream out;
public Capture() {
super("Capture Sound Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container content = getContentPane();
final JButton capture = new JButton("Capture");
final JButton stop = new JButton("Stop");
final JButton play = new JButton("Play");
capture.setEnabled(true);
stop.setEnabled(false);
play.setEnabled(false);
ActionListener captureListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
capture.setEnabled(false);
stop.setEnabled(true);
play.setEnabled(false);
captureAudio();
}
};
capture.addActionListener(captureListener);
content.add(capture, BorderLayout.NORTH);
ActionListener stopListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
capture.setEnabled(true);
stop.setEnabled(false);
play.setEnabled(true);
running = false;
}
};
stop.addActionListener(stopListener);
content.add(stop, BorderLayout.CENTER);
ActionListener playListener =
new ActionListener() {
public void actionPerformed(ActionEvent e) {
playAudio();
}
};
play.addActionListener(playListener);
content.add(play, BorderLayout.SOUTH);
}
private void captureAudio() {
try {
final AudioFormat format = getFormat();
DataLine.Info info = new DataLine.Info(
TargetDataLine.class, format);
final TargetDataLine line = (TargetDataLine)
AudioSystem.getLine(info);
line.open(format);
line.start();
Runnable runner = new Runnable() {
int bufferSize = (int)format.getSampleRate()
* format.getFrameSize();
byte buffer[] = new byte[bufferSize];
public void run() {
out = new ByteArrayOutputStream();
running = true;
try {
while (running) {
int count =
line.read(buffer, 0, buffer.length);
if (count > 0) {
out.write(buffer, 0, count);
System.out.println(line.getLevel());
}
}
out.close();
} catch (IOException e) {
System.err.println("I/O problems: " + e);
System.exit(-1);
}
}
};
Thread captureThread = new Thread(runner);
captureThread.start();
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-2);
}
}
private void playAudio() {
try {
byte audio[] = out.toByteArray();
InputStream input =
new ByteArrayInputStream(audio);
final AudioFormat format = getFormat();
final AudioInputStream ais =
new AudioInputStream(input, format,
audio.length / format.getFrameSize());
DataLine.Info info = new DataLine.Info(
SourceDataLine.class, format);
final SourceDataLine line = (SourceDataLine)
AudioSystem.getLine(info);
line.open(format);
line.start();
Runnable runner = new Runnable() {
int bufferSize = (int) format.getSampleRate()
* format.getFrameSize();
byte buffer[] = new byte[bufferSize];
public void run() {
try {
int count;
while ((count = ais.read(
buffer, 0, buffer.length)) != -1) {
if (count > 0) {
line.write(buffer, 0, count);
}
}
line.drain();
line.close();
} catch (IOException e) {
System.err.println("I/O problems: " + e);
System.exit(-3);
}
}
};
Thread playThread = new Thread(runner);
playThread.start();
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-4);
}
}
private AudioFormat getFormat() {
float sampleRate = 8000;
int sampleSizeInBits = 8;
int channels = 1;
boolean signed = true;
boolean bigEndian = true;
return new AudioFormat(sampleRate,
sampleSizeInBits, channels, signed, bigEndian);
}
@SuppressWarnings("deprecation")
public static void main(String args[]) {
JFrame frame = new Capture();
frame.pack();
frame.show();
}
}
modified 9-Dec-13 6:47am.
|
|
|
|
|
The documentation for getLevel [^] suggests that -1 means that no value can be obtained, or that it has not been set. It is difficult to suggest anything that would help solve this issue, but maybe you should use your debugger to try and collect some more information.
Veni, vidi, abiit domum
|
|
|
|
|
Do you have any suggestion on another way that I could get the amplitude of the sound?
|
|
|
|
|
I'm afraid not, I do not have experience of these classes. Did you write all this code yourself, and if so, what reference guides or tutorials did you use?
Veni, vidi, abiit domum
|
|
|
|