I have created a customized BasicEditField with Border using Bitmap.Now while typing the text,it crosses the border of the BasicEditField.
This is my code
class customEditField1 extends EditField
{
Bitmap mBorder = null;
customEditField1(Bitmap borderBitmap)
{
mBorder = borderBitmap;
}
protected void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, mBorder.getWidth(),mBorder.getHeight(), mBorder, 0, 0);
super.paint(graphics);
}
}
I want to create a BasicEditField which should hide the previously entered text and displays the newly entered text and the typed text should be with in the border.It should not depends on the number of chars limit.And i want to Apply padding between the text and the border.
You can put BasicEditField into HorizontalFieldManager.
Don't forget to move border bitmap painting from BasicEditField to HorizontalFieldManager.
class ScrollEdit extends HorizontalFieldManager {
Bitmap mBorder = null;
public BasicEditField mEdit = null;
public ScrollEdit(Bitmap border) {
super(HORIZONTAL_SCROLL | NO_HORIZONTAL_SCROLLBAR);
mBorder = border;
mEdit = new EditField(BasicEditField.NO_NEWLINE);
add(mEdit);
}
protected void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, mBorder.getWidth(), mBorder.getHeight(),
mBorder, 0, 0);
super.paint(graphics);
}
}
But you will have to play around with layout and setExtent to size manager and edit correctly. My advice is to try it without border bitmap first.
See Scroll BasicEditField instead of wrap
Talking about the wrap, set padding to BasicEditField within manager or add white space in border bitmap...
Related
In PySide you can override the paintEvent() method of a QWidget to control how the widget is painted on the screen. Is there an equivalent for Node in JavaFX?
In context: I'm in need of a way to display a custom image format on the screen. Constantly converting my format and JavaFX's Image so I can display it in an ImageView is too slow for me, in addition to being messier.
I've taken a look at ImageView.java and Canvas.java, but no luck. ImageView is using css, and Canvas appears to be doing something with the deprecated impl_ methods, for which I've found no documentation on.
Thanks!
Generally, the paint mechanisms in JavaFX changed towards a more event-based approach. To follow the JavaFX way, you should probably look at Timeline or AnimationTimer and only update the display when the actual image data changes.
However, you could use the old Swing way in JavaFX, if you like:
public class MyPane extends Pane {
private final Canvas canvas;
public MyPane() {
canvas = new Canvas(getWidth(), getHeight());
getChildren().add(canvas);
widthProperty().addListener(e -> canvas.setWidth(getWidth()));
heightProperty().addListener(e -> canvas.setHeight(getHeight()));
}
#Override
protected void layoutChildren() {
super.layoutChildren();
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, getWidth(), getHeight());
gc.setFill(Color.RED);
gc.fillRect(10, 10, getWidth() - 20, getHeight() - 20);
// Paint your custom image here:
gc.drawImage(someImage, 0, 0);
}
}
The above code would be the equivalence of this Swing code:
public class MyPanel extends JPanel {
private static final long serialVersionUID = -969772195113348076L;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.clearRect(0, 0, getWidth(), getHeight());
g.setColor(java.awt.Color.RED);
g.fillRect(10, 10, getWidth() - 20, getHeight() - 20);
// Paint your custom image here:
g.drawImage(someImage, 0, 0, null);
}
}
I wanna make an application with "vertical entries" and I want that the backgroung keep static while you go up or down seeing the entries, I use that code:
VerticalFieldManager BGVFM = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH | VerticalFieldManager.USE_ALL_HEIGHT) {
public void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, Display.getWidth(), Display.getHeight(), fondo, 0, 0);
super.paint(graphics);
}
};
...
add(BGVFM)
But if I scroll, the image. It's possible to do what I want to do?
I am using a loop to invoke double buffering painting. This, together with overriding my only Panel's repaint method, is designed to pass complete control of repaint to my loop and only render when it necessary (i.e. some change was made in the GUI).
This is my rendering routine:
Log.write("renderer painting");
setNeedsRendering(false);
Graphics g = frame.getBufferStrategy().getDrawGraphics();
g.setFont(font);
g.setColor(Color.BLACK);
g.fillRect(0, 0, window.getWidth(),window.getHeight());
if(frame != null)
window.paint(g);
g.dispose();
frame.getBufferStrategy().show();
As you can see, it is pretty standard. I get the grpahics object from the buffer strategy (initialized to 2), make it all black and pass it to the paint method of my "window" object.
After window is done using the graphics object, I dispose of it and invoke show on the buffer strategy to display the contents of the virtual buffer.
It is important to note that window passes the graphics object to many other children components the populate the window and each one, in turn, uses the same instance of the graphics object to draw something onto the screen: text, shapes, or images.
My problem begins to show when the system is running and a large image is rendered. The image appears to be cut into seveeal pieces and drawn again and again (3-4 times) with different offsets inside of where the image is supposed to be rendered. See my attached images:
This is the original image:
alt text http://img109.imageshack.us/img109/8308/controller.png
This is what I get:
alt text http://img258.imageshack.us/img258/3248/probv.png
Note that in the second picture, I am rendering shapes over the picture - these are always at the correct position.
Any idea why this is happening?
If I save the image to file, as it is in memory, right before the call to g.drawImage(...) it is identical to the original.
Uh, you are using Swing ?
Normally Swing automatically renders the image, you can't switch it off. The repaint()
method is out of bounds because Swing has a very complicated rendering routine due to
method compatibility for AWT widgets and several optimizations, inclusive drawing only
when necessary !
If you want to use the High-Speed Drawing API, you use a component with a BufferStrategy
like JFrame and Window, use
setIgnoreRepaint(false);
to switch off Swing rendering, set up a drawing loop and paint the content itself.
Or you can use JOGL for OpenGL rendering. The method you are using seems completely
at odds with correct Java2D usage.
Here the correct use:
public final class FastDraw extends JFrame {
private static final transient double NANO = 1.0e-9;
private BufferStrategy bs;
private BufferedImage frontImg;
private BufferedImage backImg;
private int PIC_WIDTH,
PIC_HEIGHT;
private Timer timer;
public FastDraw() {
timer = new Timer(true);
JMenu menu = new JMenu("Dummy");
menu.add(new JMenuItem("Display me !"));
menu.add(new JMenuItem("Display me, too !"));
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
setJMenuBar(menuBar);
setIgnoreRepaint(true);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
super.windowClosing(evt);
timer.cancel();
dispose();
System.exit(0);
}
});
try {
backImg = javax.imageio.ImageIO.read(new File("MyView"));
frontImg = javax.imageio.ImageIO.read(new File("MyView"));
}
catch (IOException e) {
System.out.println(e.getMessage());
}
PIC_WIDTH = backImg.getWidth();
PIC_HEIGHT = backImg.getHeight();
setSize(PIC_WIDTH, PIC_HEIGHT);
createBufferStrategy(1); // Double buffering
bs = getBufferStrategy();
timer.schedule(new Drawer(),0,20);
}
public static void main(String[] args) {
new FastDraw();
}
private class Drawer extends TimerTask {
private VolatileImage img;
private int count = 0;
private double time = 0;
public void run() {
long begin = System.nanoTime();
Graphics2D g = (Graphics2D) bs.getDrawGraphics();
GraphicsConfiguration gc = g.getDeviceConfiguration();
if (img == null)
img = gc.createCompatibleVolatileImage(PIC_WIDTH, PIC_HEIGHT);
Graphics2D g2 = img.createGraphics();
// Zeichenschleife
do {
int valStatus = img.validate(gc);
if (valStatus == VolatileImage.IMAGE_OK)
g2.drawImage(backImg,0,0,null);
else {
g.drawImage(frontImg, 0, 0, null);
}
// volatile image is ready
g.drawImage(img,0,50,null);
bs.show();
} while (img.contentsLost());
time = NANO*(System.nanoTime()-begin);
count++;
if (count % 100 == 0)
System.out.println(1.0/time);
}
}
I want to "fix" the Background while only the ListFields scrolls.
Current Problem:
Scrolling Down (the surounding box should move with the List)
Corresponding code for the VerticalFieldManager
VerticalFieldManager _bottom_box = new VerticalFieldManager(Field.FIELD_HCENTER | Field.FIELD_VCENTER | VerticalFieldManager.VERTICAL_SCROLL | Field.USE_ALL_HEIGHT)
{
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(maxWidth, maxHeight);
setExtent(maxWidth - 6, maxHeight - 3);
}
protected void paint(Graphics graphics)
{
graphics.clear();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, (this.getWidth()), (this.getHeight()));
graphics.setColor(color_computacenter_light_blue);
graphics.drawRect(0, 0, (this.getWidth()), (this.getHeight()));
super.paint(graphics);
}
};
Any Ideas, how to fix this? Thanks
It's a little difficult to know without more info about the structure of that screen, but the root cause is something to do with the difference between the visible height on screen (given by getHeight()) and the virtual height. You're drawing to the virtual viewport with that paint method, so I think this tweak should fix things:
protected void paint(Graphics graphics)
{
graphics.clear();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, graphics.getClippingRect().y, (this.getWidth()), (this.getHeight()));
graphics.setColor(color_computacenter_light_blue);
graphics.drawRect(0, graphics.getClippingRect().y, (this.getWidth()), (this.getHeight()));
super.paint(graphics);
}
How do we set the background and font colors in a RichTextField? I tried to override the paint() method in addition to what has been described here, but when I scroll down in, the background gets erased or reset to a white background
In RIM 4.6 and greater you can use Background:
class ExRichTextField extends RichTextField {
int mTextColor;
public ExRichTextField(String text, int bgColor, int textColor) {
super(text);
mTextColor = textColor;
Background background = BackgroundFactory
.createSolidBackground(bgColor);
setBackground(background);
}
protected void paint(Graphics graphics) {
graphics.setColor(mTextColor);
super.paint(graphics);
}
}
For RIM 4.5 and lower use paint event to draw background youreself:
class ExRichTextField extends RichTextField {
int mTextColor;
int mBgColor;
public ExRichTextField(String text, int bgColor, int textColor) {
super(text);
mTextColor = textColor;
mBgColor = bgColor;
}
protected void paint(Graphics graphics) {
graphics.clear();
graphics.setColor(mBgColor);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(mTextColor);
super.paint(graphics);
}
}
RichTextField mes_=new RichTextField("texto de ejemplo",Field.NON_FOCUSABLE){
protected void paint(Graphics g){
g.setColor(0x00e52f64);
super.paint(g);
}
};
mes_.setBackground(BackgroundFactory.createSolidBackground(0xFFFADDDA));
The method incrustaded in the declaration its for changing the color of the font. The method called after created its for changing the background to a solid color.