Hi,
I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to do this.
My tab delimited Input File looks like this:-
21 p 13e 0 62 1 580001 andrew -14.53 -13.95 0 0
21 p 13d 63 124 580002 1160001 andrew -13.95 -13.37 0 0
21 p 12g 311 364 2900000 3385714 john -11.63 -11.14 0 0
21 q 11.1a 1274 1321 12300000 12750000 peter -2.23 -1.78 0 0
My program code has to store this data like this:- -
g.drawRect(0,60,62,27);
-
g.setColor(Color.black);
-
g.fillRect(0,60,62,27);
-
g.drawRect(63,60,61,27);
-
g.setColor(Color.gray);
-
g.fillRect(63,60,61,27);
but curretly I am manually entering this data. I am reading the 4rth and 5th field in my tab delimited input file and taking the difference of 4rth and 5th field as my 3rd value. Suppose in (0,60,62,27) my 3 value 62 is 0 - 62 etc. My 60 and 27 are almost constant for the whole data in (0,60,62,27) except for my highlighter for loop below.
Now one more important thing. I have created a special rectangle in my for loop which has to do the following:-
1) If a user enters any value between 580001 - 1160001 from command line which is from 6th and 7th fields in input file my for loop below should highlight that area in the rectangles, i.e, 63 to 124 which is it should set(63,60,61,27 ) values for my for loop. Suppose:- -
int thickness = 5;
-
g.setColor(Color.red);
-
for (int i = 0; i < thickness; i++)
-
g.draw3DRect(63 - i, 60 - i, 61 2 i, 27 2 i, true);
This should highlight the second rectangular block.
Also whenever there is suppose andrew in the line print black in my g.setColor(Colo r.black) for that line / suppose john print gray in my g.setColor(Colo r.black).
Currently my code looks like this:- -
import java.awt.;
-
import javax.swing.;
-
import java.awt.Color;
-
import java.awt.event.WindowAdapter;
-
import java.awt.event.WindowEvent;
-
import javax.swing.JFrame;
-
-
public class Myprogram extends JPanel {
-
public void paintComponent(Graphics g) {
-
super.paintComponent(g);
-
-
g.drawRect(0,60,62,27);
-
g.setColor(Color.black);
-
g.fillRect(0,60,62,27);
-
g.drawRect(63,60,61,27);
-
g.setColor(Color.gray);
-
g.fillRect(63,60,61,27);
-
g.drawRect(125,60,61,27);
-
g.setColor(Color.gray);
-
g.fillRect(125,60,61,27);
-
-
int thickness = 5;
-
g.setColor(Color.red);
-
for (int i = 0; i < thickness; i++)
-
g.draw3DRect(63 - i, 60 - i, 61 2 i, 27 2 i, true);
-
-
}
-
-
public static void main(String[] args) {
-
JFrame frame = new JFrame();
-
frame.setTitle("My Java program");
-
frame.setSize(1000, 200);
-
frame.addWindowListener(new WindowAdapter() {
-
public void windowClosing(WindowEvent e) {
-
System.exit(0);
-
}
-
});
-
Container contentPane = frame.getContentPane();
-
contentPane.add(new Myprogram());
-
-
frame.show();
-
}
-
-
}
20 3751
Encapsulate all that logic (all the rules) into a nice class. The class represents a line from the text file. Here is an implementation of the color rules -
class MyClass {
-
String line;
-
-
MyClass(String lineFromFile) {
-
line = lineFromFile;
-
}
-
-
public Color getColor() {
-
if (line.contains("andrew")) {
-
return Color.BLACK;
-
} else if (line.contains("john")) {
-
return Color.GRAY;
-
} else {
-
//what should you return here?
-
}
-
}
-
-
}
-
Good idea :)
Thanks
cowboy
Hi can anybody help me to correct errors in this code. I have written where I am having problems inside the code. I have reached this far doing this code:- - import java.io.*;
-
import java.awt.*;
-
import javax.swing.*;
-
import java.awt.Color;
-
import javax.swing.JFrame;
-
import java.util.ArrayList;
-
import java.io.BufferedReader;
-
import java.util.HashMap;
-
import java.awt.geom.Rectangle2D;
-
-
-
public class myProgram extends JPanel {
-
-
private ArrayList<LineInfo> lines;
-
private HashMap<String,Color> hm = new HashMap<String,Color>();
-
private final String fileName = "file.txt";
-
-
public myProgram(){
-
lines = new ArrayList<LineInfo>();
-
this.setPreferredSize(new Dimension(400,400));
-
JFrame frame = new JFrame("FillRect");
-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
frame.pack();
-
frame.setVisible(true);
-
}
-
public void populateHashMap(){
-
Color red = Color.RED;
-
Color blue = Color.BLUE;
-
hm.put("Andrew",red);
-
hm.put("david",blue);
-
//hm.put("john",Color.Black);
-
//hm.put("peter",Color.White);
-
//hm.put("harry",Color.Gray);
-
}
-
-
@Override
-
public void paintComponent(Graphics g) {
-
super.paintComponent(g);
-
Graphics2D g2d = (Graphics2D) g;
-
g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
-
for(int i = 0;i<lines.size();i++){
-
g2d.setColor(lines.get(i).getColor());
-
g2d.fill(lines.get(i).getRect());
-
}
-
-
}
-
-
public void readFile()throws Exception {
-
String n = null;
-
try{
-
BufferedReader fh = new BufferedReader(new FileReader(fileName));
-
while(true){
-
n = fh.readLine();
-
if(n == null){
-
break;
-
}else{
-
System.out.println(n); //testing
-
String[] args = n.split("\t");
-
//Here I want to create new Rectangle here called say tempRect
-
//get color by using Color color = (Color)hm.get(args[x]); where x is the name index
-
//create bew LineInfo object LineInfo tempLineInfo = new LineInfo(tempRect,color);
-
//add to arraylist lines.add(tempLineInfo);
-
}//end of else
-
}//end while
-
fh.close();
-
}catch (FileNotFoundException e1) {
-
System.err.println("File not found: ");
-
}
-
}
-
-
-
public static void main (String[] args){
-
myProgram test = new myProgram();
-
test.populateHashMap();
-
try{
-
test.readFile();
-
}catch(Exception e){
-
System.err.println(e);
-
}
-
}
-
}//end of main class
-
-
-
-
class LineInfo{
-
private Rectangle2D.Double rect;
-
private Color color;
-
public LineInfo(Rectangle2D.Double rect, Color c){
-
this.rect = rect;
-
this.color = c;
-
}
-
public Rectangle2D getRect(){
-
return rect;
-
}
-
public Color getColor(){
-
return color;
-
}
-
}//end of class
-
My input file looks like this:- -
1 p 36.33b 0 11 1 1150001 austin 0.0 5.91 2 0.0019
-
1 p 36.33a 12 24 1150002 2300000 harry 5.91 8.75 2 0.0019
-
1 p 36.32c 25 36 2300000 3300000 austin 8.75 11.19 0 0
-
1 p 36.32b 37 49 3300001 4300000 austin 11.19 13.59 3 0.00285
-
1 p 36.32a 50 61 4300001 5300000 peter 13.59 15.97 10 0.00951
-
1 p 36.31b 62 72 5300000 6200000 peter 15.97 18.07 5 0.00476
-
1 p 36.31a 73 82 6200001 7100000 harry 18.07 20.15 8 0.00761
-
1 p 36.23b 83 93 7100000 8050000 austin 20.15 22.32 8 0.00761
-
1 p 36.23a 94 104 8050001 9000000 john 22.32 24.46 4 0.00381
-
1 p 36.22d 105 125 9000000 9825000 harry 24.46 26.3 5 0.00476
-
What errors are you getting? Where are you creating the LineInfo objects and adding them to the ArrayList?
This is my modified code. Please find the error message below:- - import java.io.*;
-
import java.awt.*;
-
import javax.swing.*;
-
import java.awt.Color;
-
import javax.swing.JFrame;
-
import java.util.ArrayList;
-
import java.io.BufferedReader;
-
import java.util.HashMap;
-
import java.awt.geom.Rectangle2D;
-
import java.awt.Rectangle;
-
-
-
public class kalia extends JPanel {
-
-
private ArrayList<LineInfo> lines;
-
private HashMap<String,Color> hm = new HashMap<String,Color>();
-
private final String fileName = "C:/InputFile.txt";
-
-
public kalia(){
-
lines = new ArrayList<LineInfo>();
-
this.setPreferredSize(new Dimension(1000,400));
-
JFrame frame = new JFrame("FillRect");
-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
frame.pack();
-
frame.setVisible(true);
-
}
-
public void populateHashMap(){
-
Color red = Color.RED;
-
Color blue = Color.BLUE;
-
hm.put("Andrew",red);
-
hm.put("david",blue);
-
//hm.put("john",Color.Black);
-
//hm.put("peter",Color.White);
-
//hm.put("harry",Color.Gray);
-
}
-
-
@Override
-
public void paintComponent(Graphics g) {
-
super.paintComponent(g);
-
Graphics2D g2d = (Graphics2D) g;
-
g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
-
for(int i = 0;i<lines.size();i++){
-
g2d.setColor(lines.get(i).getColor());
-
g2d.fill(lines.get(i).getRect());
-
}
-
-
}
-
-
public void readFile()throws Exception {
-
String n = null;
-
ArrayList<LineInfo> rects = new ArrayList<LineInfo>();
-
try{
-
BufferedReader fh = new BufferedReader(new FileReader(fileName));
-
while(true){
-
n = fh.readLine();
-
if(n == null){
-
break;
-
}else{
-
System.out.println(n); //testing
-
String f[] = n.split("\t");
-
-
int iscntop = Integer.parseInt(f[3]);
-
int iscnbot = Integer.parseInt(f[4]);
-
-
int width = iscntop - iscnbot;
-
-
Rectangle tempRect = new Rectangle(iscntop,60,width,27);
-
Color color = (Color)hm.get(n); //where x is the name index
-
LineInfo tempLineInfo = new LineInfo(tempRect,color);
-
lines.add(tempLineInfo);
-
}//end of else
-
}//end while
-
fh.close();
-
}catch (FileNotFoundException e1) {
-
System.err.println("File not found: ");
-
}
-
}
-
-
-
public static void main (String[] args){
-
kalia test = new kalia();
-
test.populateHashMap();
-
try{
-
test.readFile();
-
}catch(Exception e){
-
System.err.println(e);
-
}
-
}
-
}//end of main class
-
-
-
-
class LineInfo{
-
private Rectangle2D.Double rect;
-
private Color color;
-
public LineInfo(Rectangle2D.Double rect, Color c){
-
this.rect = rect;
-
this.color = c;
-
}
-
public Rectangle2D getRect(){
-
return rect;
-
}
-
public Color getColor(){
-
return color;
-
}
-
}//end of class
Error message is this :- -
symbol : constructor LineInfo(java.awt.Rectangle,java.awt.Color)
-
location: class mypackage.LineInfo
-
LineInfo tempLineInfo = new LineInfo(tempRect,color);
-
1 error
-
BUILD FAILED (total time: 1 second)
JosAH 11,448
Recognized Expert MVP
Read the error message: you don't have a constructor in your LineInfo class that takes a Rectangle and a Color as parameters.
kind regards,
Jos
I have written another program which is doing almost the same thing. It reads the drawrects() method but when it reaches the 2nd and 3rd method it shows null , null. I modified the input file by replacing colours instead on names ..andrew etc... Why is this null coming while reading 2nd and 3rd method. If you will run the program you will come to know what is happening.
Here is my sample input file:- -
1 p 36.33b 0 50 1 1150001 black 0.0 5.91 2 0.0019
-
1 p 36.33a 51 100 1150002 2300000 red 5.91 8.75 2 0.0019
-
1 p 36.32c 100 148 2300000 3300000 white 8.75 11.19 0 0
-
1 p 36.32b 149 196 3300001 4300000 gray 11.19 13.59 3 0.00285
-
1 p 36.32a 197 244 4300001 5300000 black 13.59 15.97 10 0.00951
-
1 p 36.31b 244 294 5300000 6200000 gray 15.97 18.07 5 0.00476
-
1 p 36.31a 295 344 6200001 7100000 blue 18.07 20.15 8 0.00761
-
1 p 36.23b 344 402 7100000 8050000 cyan 20.15 22.32 8 0.00761
-
1 p 36.23a 403 459 8050001 9000000 yellow 22.32 24.46 4 0.00381
-
-
I need help urgently :(
Thanks in advance
Cowboy
JosAH 11,448
Recognized Expert MVP
This is the basic structure of your cows class: -
class cows {
-
private ArrayList<cows> rects;
-
private ArrayList<cows> colors;
-
private ArrayList<cows> fills;
-
-
public ArrayList<cows> drawrects() {
-
// nothing here assigns an ArrayList to variable 'rects'
-
return rects;
-
}
-
-
public ArrayList<cows> fillrects() {
-
// nothing here assigns an ArrayList to variable 'fills'
-
return fills;
-
}
-
-
public ArrayList<cows> colorrects() {
-
// nothing here assigns an ArrayList to variable 'colors'
-
return colors;
-
}
-
}
-
IMHO, your code is a bit of a mess. Please design first before you want to implement anything.
kind regards,
Jos
I have corrected some of my mistakes which happened while pasting into ur forum and changing names of some methods. Anyway here is the corrected code. Can you now help me to get it right. I need help badly.
Thanks in advance
Cowboy
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Darsant |
last post by:
I'm currently reading 1-n number of binary files, each with 3 different
arrays of floats containing about 10,000 values a piece for a total of
about 30,000 values per file.
I'm looking for a way to load them all into memory. I've tried using
vector pushback with reserving, but it was horribly slow. The current
method I am using is upon opening the file and reading the number of
values, resizing the vectors (I have 3, one for each data...
|
by: Szaki |
last post by:
I use a BulkLoad to import file.xml to my base MS Server 2000.
To import this xml file I need schema file. Mayby you know how to do this
file mechanicy f.g. mayby somebody have some script in .net who generate
this schema.
for any help Thanks
======== My xml file =================================
<ROOT>
<Customers>
<CustomerId>5555</CustomerId>
|
by: sreenulanka |
last post by:
please help me
in my forum i have labels and textareas .iwant to add textarea components dyanamically when i am clicking the button.please help me
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.io.*;
import java.applet.*;
|
by: oll3i |
last post by:
i want to change the values in two columns
one colum is a combobox and the secons column is editable too
i want to get the value of that second column and the value of combobox
and sent that to another application
i have setValueAt(Object value,int row, int col) but it works only for combobox
when i enter some data in the second editable column the value i entered disappears
rows are a list data = new...
|
by: stevedub |
last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run my program the rates array does not get the information. I think my problem is where I am actuall calling the array index, but I am not sure how to do this. Here is my code:
/*
* MortFrame.java
*
* Created on February 24, 2008, 7:28 PM
*/...
| |
by: eddiewould |
last post by:
Hi, I want to write a custom widget which will act similarly to a JPanel (i.e it can contain other Components), but semantically it's not a kind of JPanel so it shouldn't extend from it.
Here is some sample code showing what I'm trying to achieve:
Effectively I expect to see a JLabel with the message "This is a test" as well as a JButton "foo".
What am I doing wrong? Is this possible? Also, I am aware it would be easier to just extend...
|
by: Akino877 |
last post by:
Hello,
I have a question regarding Java and Swing programming I wonder if I could ask the forum for some help.
I have a JPanel that has a couple of radio buttons and an "OK/Next" button on it. When a radio button wass selected and "OK/Next" was clicked on, I was able to write some code inside the "ActionPerformed" method to verify that the radio button was selected as intended. But that was only a quick test to see that things worked. ...
|
by: lilyumestar |
last post by:
I have project I have to do for class. We have to write 4 different .java files.
Project2.java
HouseGUI.java
House.java
HouseSorting.java
I already finish House.java and I need to work on both the Project2.java and HouseGUI.java Here are the requirements for those two.
The GUI
Create a class called HouseGUI which extends JFrame. It should display two text areas (JTextArea) in a grid layout (1 row, 2 columns). Your main program,...
|
by: AnuSumesh |
last post by:
Hi,
I want to read the text property of XML file.
My xml file is as follows:
<?xml version="1.0"?>
<Domain_Credentials>
<User> anu </User>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |