<I have code that uses 2 images>
the program creates a window and then when u press play, it removes all components in the container. then, i add a NewPanel object. i add mouse listeners and all in all, you should see an image following your mouse.
BUT. its only seen after you press play and then resize the window. this leads me to conclude that theres a problem with repaint().
can someone please help?
thanks.
heres my code: (EnemyShip and MouseEventHandl er dont do anything yet) -
import javax.swing.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
public class Invasion01 {
-
static String buttonInt ="0";
-
static String area = "titlescreen";
-
static ImageIcon self = new ImageIcon("self.jpg");
-
static ImageIcon title = new ImageIcon("title.jpg");
-
static int mousePosX, mousePosY;
-
static JFrame main = new JFrame("Invasion");
-
static Ship you = new Ship(10, 10, 1, 0);
-
static Container pane = main.getContentPane();
-
static NewPanel panel1 = new NewPanel();
-
public static void main(String[] args) {
-
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
main.addMouseListener(new MouseEventHandler());
-
main.addMouseMotionListener(new MouseMotionEventHandler());
-
main.setExtendedState(JFrame.MAXIMIZED_BOTH);
-
ButtonHandler handler = new ButtonHandler();
-
addComponentsToPane(pane, handler);
-
main.repaint();
-
main.setSize(500, 500);
-
main.setBackground(Color.black);
-
main.setVisible(true);
-
-
}
-
public static void addComponentsToPane(Container pane, ButtonHandler handler) {
-
pane.setBackground(Color.black);
-
pane.setLayout(new BorderLayout(0,20));
-
JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
-
JPanel quitPanel = new JPanel();
-
quitPanel.setBackground(Color.black);
-
quitPanel.setLayout(new BoxLayout(quitPanel, BoxLayout.Y_AXIS));
-
quitPanel.add(titleLabel);
-
JButton play = new JButton("Play");
-
play.addActionListener(handler);
-
quitPanel.add(play);
-
pane.add(quitPanel);
-
JButton quit = new JButton("Quit");
-
quit.addActionListener(handler);
-
quitPanel.add(quit);
-
JPanel holderPanel = new JPanel();
-
holderPanel.add(quitPanel);
-
pane.add(holderPanel);
-
holderPanel.setBackground(Color.black);
-
}
-
-
public static void startGame() {
-
main.getContentPane().removeAll();
-
main.repaint();
-
main.addMouseListener(new MouseEventHandler());
-
main.addMouseMotionListener(new MouseMotionEventHandler());
-
main.getContentPane().add(panel1, BorderLayout.CENTER);
-
main.repaint();
-
}
-
-
}
-
class Ship {
-
public int health, maxHealth, shot, money, x ,y;
-
public Ship (int health, int maxHealth, int shot, int money) {
-
this.health = health;
-
this.maxHealth = maxHealth;
-
this.shot = shot;
-
this.money = money;
-
}
-
public int getX() {
-
return x;
-
}
-
public int getY() {
-
return y;
-
}
-
public void setXY(int x, int y) {
-
this.x = x;
-
this.y = y;
-
}
-
}
-
class EnemyShip {
-
public static int moveStage;
-
public int health, maxHealth, shot, probShot, x, y;
-
public EnemyShip (int health, int maxHealth, int shot, int probShot, int x, int y) {
-
this.health = health;
-
this.maxHealth = maxHealth;
-
this.shot = shot;
-
this.probShot = probShot;
-
this.x = x;
-
this.y = y;
-
}
-
}
-
class MouseEventHandler implements MouseListener {
-
public void mouseClicked(MouseEvent e) {
-
}
-
public void mouseEntered(MouseEvent e) {
-
}
-
public void mouseExited(MouseEvent e) {
-
}
-
public void mousePressed(MouseEvent e) {
-
}
-
public void mouseReleased(MouseEvent e) {
-
}
-
}
-
class MouseMotionEventHandler implements MouseMotionListener {
-
public void mouseMoved(MouseEvent e) {
-
Invasion01.mousePosX = e.getX()-33;
-
Invasion01.mousePosY = e.getY()-57;
-
Invasion01.you.setXY(Invasion01.mousePosX,Invasion01.mousePosY);
-
Invasion01.main.repaint();
-
Invasion01.panel1.repaint();
-
}
-
public void mouseDragged(MouseEvent e) {
-
Invasion01.mousePosX = e.getX()-33;
-
Invasion01.mousePosY = e.getY()-57;
-
Invasion01.you.setXY(Invasion01.mousePosX,Invasion01.mousePosY);
-
Invasion01.main.repaint();
-
Invasion01.panel1.repaint();
-
}
-
}
-
class ButtonHandler implements ActionListener {
-
public void actionPerformed(ActionEvent event) {
-
if(event.getActionCommand() == "Quit") {
-
System.exit(0);
-
}
-
else if (event.getActionCommand() == "Play") {
-
Invasion01.startGame();
-
}
-
}
-
}
-
class NewPanel extends JPanel {
-
public void update(Graphics g) {
-
paintComponent(g);
-
}
-
public void paintComponent(Graphics g){
-
Invasion01.self.paintIcon(this, g, Invasion01.you.getX(), Invasion01.you.getY());
-
}
-
}
-
0 1945 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Ike |
last post by:
I changing the size of frames dynamically in a page (I am restricted in that
I cannot perform a reload of the content of any of the frames). In MSIE,
when I resize the frames, thinks repaint perfectly. In NS 7 there are
artifacts left over from what previously occupied a given frame.
If I minimize the NS browser, then mazimize it again, the...
|
by: Joe A |
last post by:
I'm using Access 2002 on Windows XP PC, 500 megs ram, Front end/back
end app.
I have a simple form that draws a thermometer to indicate progress of
code that is running. The thermometer form sometimes stops advancing
(does not repaint) part way thru it's cycle. If you click the MS
Access window title bar during the cycle, the Access title...
|
by: Bob |
last post by:
Trying to repaint a form named fcon using a new filter:
'Repaint the form with the new data
Forms!fcon.FilterOn = False
Forms!fcon.Filter = "RecNo = " & currecno & " AND Currentvalue = -1"
Forms!fcon.FilterOn = True
Forms!fcon.Requery
Stop
|
by: CroDude |
last post by:
I've made a custom groupbox control. Inside, as one of it's members is
CSimpleGradient object. CSimpleGradient is a wrapper class for gradient
usage. Basically it looks like that:
public class CSimpleGradient
{
private float m_GradientAngle;
private Color m_ColorA;
private Color m_ColorB;
|
by: rodchar |
last post by:
Hey all,
I was wondering if there is a repaint counterpart in asp.net. I have a
literal that i want to show before going into a procedure but it's not
working. How do I refresh the screen before entering the procedure?
thanks in advance,
rodchar
| |
by: MLH |
last post by:
Setting form's Picture property to another file from within
VBA doesn't repaint the form with new bitmap - for instance...
Me.Picture = "c:\pics\MyNewPic.jpg"
I tried
Me.Repaint
afterward, to no avail. Ideas? Suggestions?
|
by: aaron.kempf |
last post by:
hey guys i have an ETL winform in VB 2005
and it's like-- off in la-la land.
i know it's doing things-- it's happily crunching away. but i'm
confused about what i need to do to make the main form refresh... or
repaint..
it's like.. it just looks hung (sorta like me lol jk)
and i just want something that LOOKS AND ACTS like a real...
|
by: Paul |
last post by:
I have a VB.NET form with a DataGrid. When I toggle to Excel (for
example) and then back to my application the repaint of the DataGrid is
really slow. You can see the repainting happening. When I toggle back
to Excel, it does not do that. The repaint is quick. I'm running this
within a Citrix environment. The scrolling of the DataGrid is...
|
by: drexlin |
last post by:
Hi,
I hope this is the correct place to post questions about visual c++ 6.0. If not, sorry.
Anyway, the program I am working on takes messages from another computer and prints them on the screen. So, whenever a new message comes in, all the previous ones move up and the new one gets added to the bottom. Now, if the operator is scrolling up to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |