|
The capture method is in your inner class called captureCanvasImage. Just having a
new captureCanvasImage() inside your event listener won't do.
I did the following and it works:
captureCanvasImage myCanvas = new captureCanvasImage();
myCanvas.capture();
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
oh, thanks. but it save blank black image. I want the drawing. how?
|
|
|
|
|
It is saving as a "blank" canvas in black because inside your capture method, you are creating a new Graphics object, and not using the one that you have in your main class that has the drawing...
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
how should i do to save the image?
|
|
|
|
|
From what I've read on other forums and looking around the 'net, it sounds like you might want to draw to a BufferedImage with the mouse button clicks/hold downs, and take the BufferedImage and draw it to the canvas.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
nope. i want to draw on the canvas using mouse listener, then i want to save what i have draw into a folder.
|
|
|
|
|
Yes, I understand that is what you are trying to do. I looked around on other forums with people trying the same idea you are working on, and it seems there is a funky dance between the ImageIO methods, Graphics object, and canvas. I'm checking it out with Netbeans, and I'll let you know what I come up with. Your problem with the program has me interested in figuring it out
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
okay, please help me.
|
|
|
|
|
Here is a solution using double buffering, which is what I think I was suggesting earlier. I could have just posted the individual sections that were changed, but I felt it would be better to just post it all
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.colorchooser.*;
import javax.swing.event.*;
import java.awt.geom.Line2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.imageio.ImageIO;
public class PaintIt extends JFrame implements ActionListener{
BufferedImage image = new BufferedImage(600, 500, BufferedImage.TYPE_INT_RGB);
JPanel buttonPanel = new JPanel();
Point lastPos = null;
Point startPos = null;
Point finishPos = null;
Graphics g;
Graphics imageG;
JButton save = new JButton("Save");
JButton cancel = new JButton("Cancel");
JButton clear = new JButton("Clear");
JButton proceed = new JButton("Proceed");
JPanel canvas = new JPanel();
int changer = 1;
String path="";
public PaintIt () {
setLocation(100,100);
setSize(600,500);
setTitle("ENCODE SECTION");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setBackground(Color.WHITE);
clear.addActionListener(this);
clear.setActionCommand("clear");
save.addActionListener(this);
save.setActionCommand("Save");
cancel.addActionListener(this);
cancel.setActionCommand("Cancel");
proceed.addActionListener(this);
proceed.setActionCommand("Proceed");
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(save);
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPanel.add(clear);
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPanel.add(cancel);
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPanel.add(proceed);
getContentPane().add(canvas, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
g = canvas.getGraphics();
g.setColor(Color.BLACK);
imageG = image.getGraphics();
imageG.setColor(Color.WHITE);
imageG.fillRect(0,0,600,500);
imageG.setColor(Color.BLACK);
canvas.addMouseMotionListener(new MouseMotionListener () {
public void mouseDragged (MouseEvent m) {
Point p = m.getPoint() ;
if (changer==1){
g.drawLine(lastPos.x, lastPos.y, p.x, p.y);
imageG.drawLine(lastPos.x, lastPos.y, p.x, p.y);
}
lastPos = p;
}
public void mouseMoved (MouseEvent m) {}
});
canvas.addMouseListener(new MouseListener () {
public void mouseClicked(MouseEvent e) {startPos = e.getPoint();}
public void mousePressed(MouseEvent e) {lastPos = e.getPoint();}
public void mouseReleased(MouseEvent e) {
lastPos = null;
finishPos = e.getPoint();
startPos = null;}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
});
}
public void actionPerformed(ActionEvent e) {
if("clear".equals(e.getActionCommand())) {
repaint();
}
if("Save".equals(e.getActionCommand())) {
captureCanvasImage myCanvasImage = new captureCanvasImage();
myCanvasImage.capture();
}
if("Cancel".equals(e.getActionCommand())) {
dispose();
}
if("Proceed".equals(e.getActionCommand())) {
}
}
class captureCanvasImage {
public void capture(){
try
{
ImageIO.write(image, "jpg", new File("panel.jpg"));
g.clearRect(0,0,600,500);
}
catch(IOException ioe)
{
System.out.println("Panel write help: " + ioe.getMessage());
}
}}
public static void main (String [] args) {
PaintIt p = new PaintIt();
p.setVisible(true);
}
}
""Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
oh..thank you
but it has some problem, when i save the picture, the canvas will disappear and after i click clear button it will appear back. an i draw back and save, the new image will overlap with the old one. how should save the image without overlap?
|
|
|
|
|
Remove the following line from the capture method:
g.clearRect(0,0,600,500);
In your actionPerformed event handler method, you will want to get rid of the repaint() method, and change it to:
g.clearRect(0,0,600,500);
imageG.setColor(Color.WHITE);
imageG.fillRect(0,0,600,500);
imageG.setColor(Color.BLACK);
This should just clear out the canvas and the double buffered image as well. Good luck to you on the remainder of your project
""Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
clear the canvas, and after i paint back, it save blank black image. how?
|
|
|
|
|
I modified the code in my previous post, it should be:
g.clearRect(0,0,600,500);
imageG.setColor(Color.WHITE);
imageG.fillRect(0,0,600,500);
imageG.setColor(Color.BLACK);
When you invoke the fillRect , it will fill the rectangle with whatever color is the color last set by setColor .
""Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
after one time save, and clear, the canvas will disappear too? how to maintain the canvas?
|
|
|
|
|
I have not read through all of the messages in this thread but I get the impression you are trying to mix the drawing and saving code in the same methods. You need to understand how to handle the different parts of the program and why they should be kept separate. Your draw (or paint) method should only be concerned with building an image on screen from some data held in memory. This could be an image file or a set of instructions which the program uses to construct the image on the canvas. That is the only thing the drawing code should be involved in. The saving code should take the information from memory and write it to backing store (disk or other device) without having any interaction with the image on the screen.
|
|
|
|
|
1. Problem:
You add an Actionlistener to your Save-Button but when button is hit, you just instantiate the class but do not call the capture-method.
So edit the call to:
if("Save".equals(e.getActionCommand())) {
System.out.println("action save hit!");
captureCanvasImage bla = new captureCanvasImage();
bla.capture();
|
|
|
|
|
Okay so I have an app widget and I am trying to make a web request and It just keeps failing. Here is the code:
public class IDEAAllotmentTrackerActivity extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
context.startService(new Intent(context, UpdateService.class));
}
public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
RemoteViews updateViews = null;
try {
updateViews = buildUpdate(this);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ComponentName thisWidget = new ComponentName(this, IDEAAllotmentTrackerActivity.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
public RemoteViews buildUpdate(Context context) throws ClientProtocolException, IOException{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet("http://www.google.com"));
StatusLine statusLine = response.getStatusLine();
String pageContent = "";
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
pageContent = out.toString();
} else{
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
RemoteViews updateViews = null;
updateViews = new RemoteViews(context.getPackageName(), R.layout.main);
updateViews.setTextViewText(R.id.message, pageContent);
return updateViews;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
}
Then my manifest looks like this:
<application android:label="IDEA allotment">
<receiver android:name=".IDEAAllotmentTrackerActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/stuff" />
</receiver>
<service android:name=".IDEAAllotmentTrackerActivity$UpdateService" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:targetSdkVersion="15" android:minSdkVersion="11"/>
</manifest>
Any sugestions.
|
|
|
|
|
I don't know much about android. But how did you decide that the request is failing? Did you end up with an error? If so what is it, post it here?
If you've never failed... You've never lived...
|
|
|
|
|
Since Android 4.0 (SDK 14), it is no longer allowed to have network communications in the UI thread.
The solution is to move the code where you download files into a background thread, for example with a Runnable. Make sure to keep your updateViews.setTextViewText() calls in the UI thread though, with for example a Handler.
modified 13-Sep-18 21:01pm.
|
|
|
|
|
Thanks a lot! Here is the code I used:
new Thread(new Runnable() {
public void run() {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.google.com/pxi");
ResponseHandler<String> resHandler = new BasicResponseHandler();
String page = "";
try {
page = httpClient.execute(httpGet, resHandler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
final String p = page;
Log.e("response" , page);
test.post(new Runnable() {
public void run() {
test.setText(p);
}
});
}
}).start();
Also this page I found helpful:
http://developer.android.com/guide/components/processes-and-threads.html[^]
|
|
|
|
|
I have a problem when i get value from another class, the error message show
"unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown"
This is my main code
package testtimer;
import testing.testing_1234;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
import java.util.Date;
import java.io.*;
import java.text.*;
import java.util.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.JTable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Iterator;
/**
*
* @author tan
*/
public class testing_page extends javax.swing.JFrame {
testing_1234 appsetting = new testing_1234();
String strc=appsetting.getcname();
public rxpooltesting() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new testing_page().setVisible(true);
}
});
}
}
This is my class:
package testing;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.util.Properties;
import java.io.FileNotFoundException;
public class readsetting {
private String PropertiesPath = "C:TestTimer/src/testtimer/setting.properties";
private String strcname;
public readsetting()
{
}
public readsetting(String strurl,String struname,String strpword,String strcname,String strxpool,
String stridcounter,String strtemppol,String strxlog,String strlogfile,String strDB,
String strservice)
{}
public String getcname() throws FileNotFoundException, IOException{
FileInputStream is = new FileInputStream( PropertiesPath );
Properties dbProps = new Properties();
dbProps.load(is);
strcname=dbProps.getProperty("classnames");
return strcname;
}
public void setcname(String cname){
this.strcname = cname;
}
}
Thanks.
|
|
|
|
|
Did you actually read the error message? getcname declares thrown exceptions, so you have to catch or declare them as thrown wherever you call it. That means you can't use getcname in a static initialiser, because there's no way to catch the exception.
|
|
|
|
|
I have some files dont know how to decrypt them i mean these are using to conduct exam, atleast i am unable to keep all the data for future preparation also
i am attached a rar file download This Link in it i kept a folder which contains six files 5 of the are encpyted from them one is question other four are options of that question and one of them the solution of the question
i need to decrpyt so please tell me the way to decrypt and to view them
|
|
|
|
|
I don't know why - but I felt fancy and wanted to see if our new virus detection is working (They said it's so much better...).
The files are complete crap. There is nothing in them. What kind of file are those supposed to be? They are HTML labeled, but I guess they should be something else!?
|
|
|
|
|
It contains no virus or any thing that harms the computer i just need some types of encryption and decryption that are used in tomcat jsp
I want to break it totally
|
|
|
|
|