awt

KeyListener not working

可紊 提交于 2019-11-27 08:51:02
问题 public class KL implements KeyListener { public static void main(String[] args) { final JPopupMenu popup = new JPopupMenu(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setVisible(true); } @Override public void keyPressed(KeyEvent arg0) { System.out.println(arg0.getKeyChar()); } @Override public void keyReleased(KeyEvent e) { System.out.println(e.getKeyChar()); } @Override public void keyTyped(KeyEvent e) { System.out.println(e.getKeyChar()); } } That

How to implement an infinite image loop?

半城伤御伤魂 提交于 2019-11-27 08:50:53
问题 I am currently working on a 2D shooter with moving background. The background is just a simple image. I want to implement that the image is moving endless from top to bottom. I thought about determining the part of the image that is at the bottom outside the visible part of the frame and paint it on the top again. @Override public void paint(Graphics go) { Graphics2D g = (Graphics2D) go; g.fillRect(0, 0, this.getWidth(), this.getHeight()); g.drawImage(this.imgBackground, 0, this.yBackground,

How to set present screensize in Java Swing?

*爱你&永不变心* 提交于 2019-11-27 08:36:06
问题 I have working with swing API for the first time to get the screen resolution for my win application. How to set present screensize in Java Swing? 回答1: The question depends. Do you want the full screen size or the usable screen size (minus things like the taskbar and the dock)? Do you want the default screen or a specific screen? Full Screen Size, any device... First you need to grab a reference to the current GraphicsEnvironment ... GraphicsEnvironment ge = GraphicsEnvironment

Add an actionListener to a JButton from another class

只愿长相守 提交于 2019-11-27 08:36:06
问题 so I have two classes testPanel and testFrame. All the buttons are in the testPanel class. I want to add ActionListeners to the Jbuttons in the testFrame class. How do I go about doing this? testPanel: public class testPanel extends JPanel{ JLabel codeLbl = new JLabel("Code"); JLabel titleLbl = new JLabel("Title"); JLabel priceLbl = new JLabel("Price"); JTextField codeTxt = new JTextField(20); JTextField titleTxt = new JTextField(20); JTextField priceTxt = new JTextField(20); JButton addBtn =

Draw varying size rectangle with different orientation using rectangle 2D

六月ゝ 毕业季﹏ 提交于 2019-11-27 08:10:35
问题 I want to draw a rectangle in a java application. I have used rectangle2d to draw a rectangle. I need the rectangle to vary size based on mouse drag. i.e. The size of the rectangle varies as I drag the mouse. I am currently able to draw only one type of the rectangle, i.e. When i drag the mouse towards down right of the screen. But i am unable to draw the other rectangles.for example. when the mouse is dragged top right of the screen. I am using a method called setRect that takes upper left x

How can I switch between jpanels?

ⅰ亾dé卋堺 提交于 2019-11-27 08:07:20
问题 I'm still very new to java programming, so please help me to correct any mistakes I might have overlooked or give tips on how to improve this program. Okay, so a lot of problems have been solved, and now I have a CardLayout, but I still have questions about how I should make my pipes show inside it. When I tried to add in my refresh rate timer and my speed timer, I have problems about how I need to declare and initialize boolean variables. Also, when I compile and run this game, I get files

How to distribute AWTUtilities

廉价感情. 提交于 2019-11-27 08:01:01
问题 I recently read the blog posts on Pushing Pixels that describe how to achieve native transparency and translucency with pure Java. The needed classes reside on com.sun.awt namely com.sun.awt.AWTUtilities. I was wondering how i could include the needed classes (not just this one) into my distro since the classes are available only when you have a jdk installed and you start the jvm through there. So the users of my program will not have the needed classes to run my program. Any ideas? 回答1:

Setting java.awt.headless=true programmatically

試著忘記壹切 提交于 2019-11-27 07:47:32
I'm trying to set java.awt.headless=true during the application startup but it appears like I'm too late and the non-headless mode has already started: static { System.setProperty("java.awt.headless", "true"); /* java.awt.GraphicsEnvironment.isHeadless() returns false */ } Is there another way set headless to true beside -Djava.awt.headless=true ? I would prefer not configure anything on the console. reto I was working with a main() in a class which statically loads different parts of JFreeChart in Constants (and other static code). Moving the static loading block to the top of the class

Drawing the Cannabis Curve

江枫思渺然 提交于 2019-11-27 07:42:26
问题 Inspired by Cannabis Equation from the Math site (it refers to the Wolfram Research Cannabis Curve) I was wondering.. How would we draw this curve using Java-2D? 回答1: import java.awt.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import javax.swing.*; import java.io.File; /** Draws the CannabisCurve http://mathworld.wolfram.com/CannabisCurve.html Inspired by a question asked at: http://math.stackexchange.com/q/618786/38356 */ class CannabisCurve

Porting AWT graphics code to Android

情到浓时终转凉″ 提交于 2019-11-27 07:39:56
问题 We would like to use some of our existing Java AWT graphics code on the Android platform. As far as I can tell, Android does not include any of the AWT classes -- no Graphics2D , Path2D , FontMetrics , etc. What is the best approach to port our drawing code to Android? Ideally, we would like to modify our code base to target both Android and generic Java. 回答1: The android platform supports a small subset of awt. By small, I mean it supports awt fonts. Going from java swing (are you really