awt

Java: Using graphics component within an ActionListener

这一生的挚爱 提交于 2019-12-01 01:45:59
I have two separate class and driver files, and in the class file I create the paint method: public void paint(Graphics g){ g.drawLine(...... .... //along with all of my other draw commands } Further down in the code, I create a JButton and within this button's action listener I don't know how to use a Graphics object to create more graphics in the JFrame. Should I be adding something to my driver to make this happen, or is there a way to use these graphics within my action listener? Thank you, and any help is appreciated. Paul Samsotha You need to draw everything within the paint method. The

How to generate multiple circle shapes and animate those shapes going down the frame

余生颓废 提交于 2019-12-01 01:36:10
I am having trouble generating multiple oval shapes when clicking the panel of the frame. What I want is that it will generate many oval shapes and those shapes will move downward. One of the requirement is to use two multi threading. However in my case, the program I created is that, it will only generate one oval shape and the position is randomly changing. Can anyone please help me one this. package ovalrandomcolors; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.List; import java.awt.Shape; import java.awt.event

How to make background work in custom JComponent?

点点圈 提交于 2019-12-01 01:26:14
问题 In the following example, I have a custom JComponent being drawn on green background, but it does not appear. Why does this happen? public class Test_Background { public static class JEllipse extends JComponent { private final Ellipse2D ellipse; public JEllipse(int width, int height) { ellipse = new Ellipse2D.Double(0, 0, width, height); setOpaque(true); setBackground(Color.GREEN); } @Override public Dimension getPreferredSize() { return new Dimension((int) ellipse.getBounds().getMaxX(), (int

Java (native) print dialog - change icon

与世无争的帅哥 提交于 2019-12-01 01:02:14
问题 I use PrinterJob.printDialog() to let the user select a printer and change various print settings. However the dialog is always displayed using the standard Java coffeecup icon and not the one from my main window (JFrame). How can I change the icon for that dialog? I'm using the following piece of code: PrinterJob pj = PrinterJob.getPrinterJob(); pj.printDialog(); // how do I change the icon for the dialog that is displayed here ... // process the selection from the dialog Normally a JDialog

Taking screenshot in Java (Robot, code working but not with external full screen application)

▼魔方 西西 提交于 2019-12-01 00:23:00
I am using the following code for taking a screenshot: Robot robot = new Robot(); BufferedImage image = robot.createScreenCapture(screenRectangle); ImageIO.write(image,"png", file); This code is running good. It takes screen shots of my desktop when I run the program, etc. However, when I try to run first some game, application in full screen , it doesn't work properly. It renders either black screen, either the same "initial" picture. Is this problem known, and how to fix this please? Second question : Is this possible to simulate some "Print Screen" key we can have on keybord in order to

KeyPressed event in java

半世苍凉 提交于 2019-12-01 00:01:46
I have just created a java tic-tac-toe game i would like to figure out how to run a method once the enter key is pressed during a certain condition an example is below... if(/*condition is met*/){ //keyListener } Depending on where you want to trap the "enter" key, you could use an ActionListener (on such components such as text components or buttons) or attach a key binding to you component public class MyPanel extends JPanel { public MyPanel() { InputMap im = getInputMap(WHEN_FOCUSED); ActionMap am = getActionMap(); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "onEnter"); am.put(

Adding actionListener to jCalendar

。_饼干妹妹 提交于 2019-11-30 23:34:23
How would I add an actionListener to the jDayChooser component of an existing jCalendar placed using netbeans? I would like to only trigger an event only when the day buttons are clicked. as the propertyChange in jCalendar listens to even the jMonthChooser and jYearChooser P.S. using toedter's jCalendar Alternatively, you can listen for the specific propertyName , "day" . JDayChooser jdc = new JDayChooser(); jdc.addPropertyChangeListener("day", new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent e) { System.out.println(e.getPropertyName()+ ": " + e

Why do I have to call GraphicsEnvorinment.registerFont() even if I my Font were created from file?

久未见 提交于 2019-11-30 23:13:21
I'm developing a web application which use JFreeChart to render chart. However, when server dose not have any Chinese font installed, JFreeChart dose not display Chinese character even if I have set the font. Then I write a small testing code and find out that add this line of code before drawing the chart can solve the problem. GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font); So my questions are - Why do I have to register font into JVM even if I create my font from File ? Dose that mean JFreeChart do not use the font I set directly ? When I deploy my program into server,

Java: Using graphics component within an ActionListener

ぐ巨炮叔叔 提交于 2019-11-30 21:09:35
问题 I have two separate class and driver files, and in the class file I create the paint method: public void paint(Graphics g){ g.drawLine(...... .... //along with all of my other draw commands } Further down in the code, I create a JButton and within this button's action listener I don't know how to use a Graphics object to create more graphics in the JFrame. Should I be adding something to my driver to make this happen, or is there a way to use these graphics within my action listener? Thank

Swing: How to make non-rectangular windows with soft borders?

半城伤御伤魂 提交于 2019-11-30 20:43:14
How could I make non-rectangular windows with soft borders in Java? Soft borders (also known as soft clipping) are borders without aliasing artifacts. I searched the web a lot and found several posts about translucent and/or non-rectangular windows. The topic "soft border" is confusing. It seems that the information I found deals with applying soft borders to component which are inside another Java components. But, can I, or can I not apply soft borders to custom shaped JWindow which is placed just on the desktop? I am primely referring to following post: http://today.java.net/pub/a/today/2008