im developing an GUI Application, using JSWing, i load XML file, then deserialize it, then i add all created object to the JPanel.
However, until i move the window, or click on the panel, this is how they looks like
After i move the window, they look correctly, so how to fix this issue <
I looked at this link
http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)
and it might be the answer, since in the constructor of the JComponent i use
setOpaque(true);
but im still not sure how to fix the issue since that part of documentation is very hard to understand (it somehow just does not make any sense to me :-D )
by the way, the painting itselfs goes something like this
for (NetObject o : objects) {
addNewObject(o);
}
and addNewObject (not whole code)
public void addNewObject(NetObject o) {
DraggableComponent object = new DraggableComponent(o, editorIndex); //Creates a draggableComponent
this.add(object);//Adds this component to main container
object.setOverbearing(true); //On click ,this panel gains lowest z-buffer
object.setSize(48, 48);
object.setLocation(o.x - 23, o.y - 23);
object.setBackground(Color.WHITE);
this.repaint(); //this = JPanel
}
and the overriden paintComponent code
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isOpaque()) {
if (object.type == 2) { //tarnsition
g.drawImage(transition, 0, 0, this);
} else if (object.type == 1) {
boolean test = g.drawImage(place, 0, 0, this); //place
g.drawString(object.loadTokens(), 3, 27); // 1,2,3,4...
}
}
}
i tried to call this.revalidate - after FOR EACH LOOP - didnt help, the only way that works is to move somehow with the window, strangely, this problem exists only # Windows, my collegue is developing this exact same application under Linux, and he does not experience ani graphical issues.
I know that there been an awfully lot of topics like this, but i honestly was not able to figure out the solution.
Thanks for the answer,
OSiRiS
The setBackground() API mentions that "It is up to the look and feel to honor this property, some may choose to ignore it." Set the graphics context's color explicitly in paintComponent() and invoke fillRect().
Related
I am completely new to processing, and I wanted to test it by creating a simple sketch that draws a rectangle, however, when i run the sketch, a window pops up with nothing on it. I tried filling it, putting an outline on it, and various other things, nothing happened. I don't think it's a problem with the code but a problem with the app itself, and i'm not sure how to fix it. I am using processing 3.5.4 on windows.
Code:
class Sketch {
void setup() {
size(100, 100);
}
void draw() {
rect(50, 50, 25, 25);
}
}
Expected Output: A square shows up on screen.
Output: Nothing shows up
<iframe src="https://drive.google.com/file/d/1gAQsX0hpAyH_iSbUXkyO87a8NpXscLEL/preview" width="640" height="480"></iframe>
Chiming in with the obvious (don't hurt me if I didn't get your question right):
draw() and setup() are already of Processing. In fact, those you wrote into a class won't be automatically executed, as Processing will be looking for it's own methods. That's why you get this:
Which is, incidentally, exactly the same result as if you ran an empty sketch.
To fix this, just take draw() and setup() out of the class, and Processing will then find and run them:
void setup() {
size(100, 100);
}
void draw() {
rect(50, 50, 25, 25);
}
Have fun!
I’ve trimmed down the code to only the relevant parts and posted it below. The code works fine. The video plays when you run it but it doesn’t have a seekbar.
public class Screen {
//JFrmae
private JFrame frame;
// Panel which I add the canvas to
private JPanel pVid = new JPanel();
// Canvas
Canvas canvas = new Canvas();
// Embedded Media Player
EmbeddedMediaPlayer emp;
/**
* Create the application.
*/
public Screen() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
//Frame
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Adding the panel to the frame
frame.getContentPane().add(pVid);
//Adding the canvas to the panel
pVid.add(canvas);
//Setting canvas size
canvas.setSize(715, 402);
//Loading the VLC native library
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "lib");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
//Initializing the media player
MediaPlayerFactory mpf = new MediaPlayerFactory();
//Misc
emp = mpf.newEmbeddedMediaPlayer(new Win32FullScreenStrategy(frame));
emp.setVideoSurface(mpf.newVideoSurface(canvas));
//Video file name and playing
String file = "video.mp4";
emp.prepareMedia(file);
emp.play();
//pack method
frame.pack();
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Screen window = new Screen();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
I’ve looked for an answer online for the last 4 days. Finally I decided to ask here. The official website for vlcj has pictures of a vlcj player they’ve made. There is a seekbar in those pictures. Link to the webpage which has the pics: http://capricasoftware.co.uk/#/projects/vlcj
They have a number of useful tutorials there but they don’t have any instructions for adding the seekbar.
Then I tried downloading their vlcj-player project from their GitHub page. It showed an error because it couldn’t resolve the “com.google.common.collect.ImmutableList” which is supposed to be imported. (At the moment I’m reading about ImmutableList and stuff and see if there’s a way to fix it.) Since I couldn’t figure that out yet, I looked for a class named seekbar or the like in their project. I couldn’t find any.
I also searched elsewhere online for the answer but I just couldn’t find it. I’d really appreciate any help. Thank you.
Edit:
(This edit is in response to the suggestion given to me by #caprica. Read their comment to this question and my reply to that in the comment to understand what I'm talking about here in this edit. I think it'll be useful for others in the future.)
All right, there must have been some problem with my Eclipse or computer. (I’ll type out how I fixed it at the end of this comment.) It’s working now. I’ll type out what I did step by step so that may be it’ll be useful to others in the future to download and install the project.
Download the project.
Import it as a Maven project. (Import > Maven > Existing Maven Project)
Now in Eclipse right click the imported project and select Run As > Maven Install
And that’s it. Now you can just run the project normally. If you don’t know how to run the project, do it like this. Right click the project and select Run As > Java Application and then Select VlcjPlayer – uk.co.caprica.vlcplayer.
Alternatively you can open the class where the main method is and run it. VlcjPlayer class is where the main method is located. The class is in the package uk.co.caprica.vlcplayer.
The problem I faced was that somehow all the necessary files didn’t get downloaded when I ran it as Maven Install. But it worked fine in another computer. Since I knew where the files are downloaded to, I just copied the folder from that PC and put it in the same place in my PC. The folder name is ‘repository’. It’s location is C:\Users\User Name\ .m2. Perhaps Eclipse in this PC has some problem. I’ll reinstall it later to avoid problems in the future.
And this may be useful, the VLC that’s installed in this PC is 64 bit. Not sure if that makes a difference but mentioning it just in case.
Now that the app is working fine I will see the code and see how the seekbar is made. Thanks a lot #caprica for telling me that I should import it as a Maven project. :)
The Basic Controls tutorial shows the essential approach: Add a panel of buttons to the frame and give each button an ActionListener that invokes the relevant media player command. As an example, this notional Rewind button would "skip backwards 10 seconds (-10,000 milliseconds)."
JPanel controlsPane = new JPanel();
JButton rewindButton = new JButton("Rewind");
rewindButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.getMediaPlayer().skip(-10000);
}
});
controlsPane.add(rewindButton);
frame.add(controlsPane, BorderLayout.SOUTH);
The software design is up to you, but you should at least be aware of
JToolBar, seen here and here.
Action, seen here and cited here.
Timer, seen here as a way to repeat an Action.
All right, guys. I’ve figured out how to do it. I’m not sure how it was done in the official Vlcj project but I’ve figured out my own simple way by learning from the official project.
It just takes a few lines of code. It’s very simple.
These are the steps you have to follow:
Create a JSlider.
To the JSlider, add a mouseMotionListener (‘mouseDragged’ to be exact).
Inside that put in the code which would update the video position based on
the change in the JSlider.
Create a Timer.
Put the code inside it to set the value of the JSlider based on the position
of the video.
And that’s it!
This is the code. It comes inside the initialize() method which you can see in the code I’ve given in the question. (And of course you'll also have to create the JSlider and add it to the panel. I haven't shown the code since it's simple.)
js.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
if (js.getValue() / 100 < 1) {
emp.setPosition((float) js.getValue() / 100);
}
}
});
Timer timer = new Timer(100, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
js.setValue(Math.round(emp.getPosition() * 100));
}
});
timer.start();
Some explanation.
The value you get when you use emp.getPosition always seems to be in decimals. It’s something like 0.1334344 at the start of the video and it’s something like 0.998988 at the end. But the value of JSlider is in int. From 0 to 100. So in the mouseMotionListener added to the JSlider I’ve converted the int value of the JSlider to float by dividing it by 100.
And in the action listener inside the timer I’ve multiplied the value of the video position by 100 and then rounded it off to make it an int value. So that value can be set in the JSlider to make it move in sync with the video.
I’m sure the code is rudimentary and there could be some best practices which I may not have followed. Sorry about that but I’m just getting into java by learning the stuff which I find interesting. Those who are good at java and have used such code in an actual project can comment below with how it can be improved.
I'm trying to resize an existing PDF button. I want to amend the label from "Print" to "Print Amended".
PushbuttonField button = form.getNewPushbuttonFromField("HoldButton");
Rectangle box = button.getBox();
box.setRight(box.getRight() + 72); // Increase width by 1"
button.setBox(box);
button.setText("Print Amended");
form.replacePushbuttonField("HoldButton", button.getField());
The above code successfully changes the label, but not the size. The end result is a button with no change in width, and the label "Print Amended" squished together.
Is it possible to resize an existing button in iText?
I tried your example and I was surprised that I could reproduce your problem.
I looked into the iText code and I see that it is explicitly forbidden to change the /T value. This makes sense: if you want to replace an existing button, you don't want to change its name.
However, for some reason we also explicitly forbid changing the /Rect value. See the code of the AcroFields class:
for (Object element : button.getKeys()) {
PdfName key = (PdfName)element;
if (key.equals(PdfName.T) || key.equals(PdfName.RECT))
continue;
if (key.equals(PdfName.FF))
values.put(key, button.get(key));
else
widgets.put(key, button.get(key));
merged.put(key, button.get(key));
markUsed(values);
markUsed(widgets);
}
I am not sure why we made this decision when we wrote this code. If I remove || key.equals(PdfName.RECT), then your code works as expected.
As we deliberately excluded changing the dimensions of the button, I am in doubt if this is a bug or if we intentionally added that code there. Reading your requirement, I am inclined to remove || key.equals(PdfName.RECT) from the official source code.
PS: I know that this doesn't answer your question, but it does explain why your code doesn't work in spite of the fact that it looks perfectly OK. As I explained: I'm really surprised that it doesn't work, because I'm responsible for the iText code...
PS 2: I've changed the code in the official trunk.
Try something like:
newButton1 = new JButton("Print Amended") {
{
setSize(150, 75);
setMaximumSize(getSize());
}
};
or:
Try to use setMaximumSize() method
button.setMaximumSize(new Dimension(100,100));
So I'm trying to add an icon into my program, but the textbook I'm reading explain how to only for Windows users. I would like to know how to add the icon. I have it on my program source folder and the code I have so far is something like this:
logo = new ImageIcon("~://resources//CherryBoom.png");
labelone = new JLabel("Fruit No.1 : ", logo, SwingConstants.LEFT);
JPanel panelone = new JPanel();
panelone.add(labelone, logo);
The icon still won't show on the windows panel, so I'm really lost here, and I don't know how can I get it to show into my program.
First of all, check the obvious solutions such as:
Have you done window.add(panelone);
Is the file in the correct spot/url is correct
Secondly, if you hate LayoutManagers like me, but still want to use javax.swing, you might try using drawString and drawImage methods in your panel's paintComponent(Graphics g) class. In detail:
You'll need to make your own JPanel:
public class MyPanel extends JPanel {
as well as override the method:
#Override
public void paintComponent(Graphics g) {
within the method, call this so the window can refresh itself and do other housekeeping things:
super.paintComponent(g);
then, use drawString and drawImage to draw these images in the place you would like them:
g.drawString("Fruit No. 1", x, y);
logo.paintIcon(this, g, x, y);
Whenever you change or draw an image, you'll also want to call in the main method:
panelone.repaint();
Hope this helps!
Java doesn't support expanding the "~" path directive.
Try this...
try {
File file = new File("~");
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
} catch (IOException exp) {
exp.printStackTrace();
}
I think you will find that it doesn't point to the users home folder.
Instead, you should be using System.getProperty("user.home")
logo = new ImageIcon(System.getProperty("user.home") + File.separator + "/resources/CherryBoom.png");
Now, having said that, I would strongly encourage you to use ImageIO over ImageIcon as you will get better feedback when something goes wrong.
Check out Reading/Loading an Image
I am looking for a simple solution for the issue that my GWT DialogBox can be dragged out of the screen. The host has the CSS rule overflow:hidden because I do not want any scrollbars to appear.
Obviously I need to attach somehow a listener to the dragging and prevent moves that would bring it outside. I can only see onMouseMove, beginDragging, endDragging methods in DialogBox.
"We" have worked around this issue in the following way:
#Override
protected void endDragging(MouseUpEvent event)
{
int genericMargin = 60;
int leftMargin = -(this.getOffsetWidth() - genericMargin);
int lowerMargin = Window.getClientHeight() - genericMargin;
int rightMargin = Window.getClientWidth() - genericMargin;
int upperMargin = 0;
if (this.getAbsoluteLeft() > rightMargin)
{this.setPopupPosition(rightMargin, this.getPopupTop()); }
if (this.getAbsoluteLeft() < leftMargin)
{ this.setPopupPosition(leftMargin, this.getPopupTop()); }
if (this.getAbsoluteTop() > lowerMargin)
{ this.setPopupPosition(this.getPopupLeft(), lowerMargin);}
if (this.getAbsoluteTop() < upperMargin)
{ this.setPopupPosition(this.getPopupLeft(), upperMargin);}
super.endDragging(event);
}
BTW it correctly works as it is! ;)
I would suggest trying gwtquery-dnd. I have been using the drag and drop plugin and it works great. It has an option to setContianment(Element elem) which is what you are looking for. Some other features is that it has snap so you can snap to other widgets if you wish to dock your dialog box somewhere. It also has the ability to specify a handle similar to the DialogBox header for dragging.
http://code.google.com/p/gwtquery-plugins/wiki/DragAndDropPluginForGWTDeveloppers
You can investigate com.google.gwt.user.client.ui.DialogBox source code and override all methods for your need. There are a couple of methods responsible for dragging there.
Not sure you can solve this problem in other ways. Otherwise you need to develop custom draggable popup panel which I am sure not a good solution.